如何使用标准::aligned_union
How to use std::aligned_union
在尝试学习如何使用std::aligned_union时,我找不到任何示例。 我的尝试遇到了我不知道如何解决的问题。
struct include
{
std::string file;
};
struct use
{
use(const std::string &from, const std::string &to) : from{ from }, to{ to }
{
}
std::string from;
std::string to;
};
std::aligned_union<sizeof(use), include, use>::type item;
*reinterpret_cast<use*>(&item_) = use{ from, to };
当我尝试在VC ++ 2013调试模式下运行程序时,我在memcpy(unsigned char * dst, unsigned char * src, unsigned long count)
中出现运行时错误。 我假设这就是VC++从临时实现赋值的方式。
我将如何更改此设置,以免遇到此问题?
aligned_union
类型为您提供了一个适合作为所需类存储的 POD 类型 - 它实际上不是该类型的对象。您仍然必须构造自己的对象:
#include <memory>
{
std::aligned_union<sizeof(use), include, use>::type storage;
use * p = new (static_cast<void*>(std::addressof(storage))) use(from, to);
// ...
p->~use();
}
扩展 Kerrek 的答案:我建议使用带有自定义删除器的unique_ptr
自动为您处理销毁。你可以在工厂里很好地包装所有东西(Live at Rextester):
struct placement_deleter {
template <typename T>
void operator () (T* ptr) const {
ptr->~T();
}
};
template <typename T, typename...Args>
std::unique_ptr<T, placement_deleter>
make_in_place(void* place, Args&&...args) {
return std::unique_ptr<T, placement_deleter>{
::new (place) T(std::forward<Args>(args)...)
};
}
int main() {
std::aligned_union<0, int, std::string>::type storage;
{
auto i = make_in_place<int>(&storage, 42);
std::cout << *i << 'n';
}
{
auto s = make_in_place<std::string>(&storage, "this is");
*s += " a test";
std::cout << *s << 'n';
}
}
我也在扩展Kerrek的答案。aligned_union
的可能实现(C++14)是:
template <std::size_t Len, class... Types>
struct aligned_union
{
static constexpr std::size_t alignment_value = std::max({alignof(Types)...});
struct type
{
alignas(alignment_value) char _s[std::max({Len, sizeof(Types)...})];
};
};
所以很明显:
- 你必须构建自己的对象(放置新)
type
是一种大小和对齐方式的 POD 类型,适合用作Types
中列出的任何类型的对象的未初始化存储(但实际上这些类型都不是)- 您可以使用 0 作为
Len
参数的值(我认为它适用于通用编程情况,例如 https://stackoverflow.com/a/27069379/3235496)
欲了解更多详情:
- N2140
- 鸡蛋变种 - 第一部分
相关文章:
- C++Union/Struct位域的实现和可移植性
- 如何通过多类"Union variable" (sfml) 使用轮询事件
- 错误:"Left of getValue must have class/struct/union"
- 编译器如何实现__declspec(align(x)) / __attribute__(aligned(x)))?
- C++Union,两个活跃成员,仅简历资格不同
- 如何以"union-like"方式更改C++向量的数据类型
- 如何控制或优化或删除或释放 UNION 中未使用的内存
- 对结构使用 %union
- 得到错误"left of '.toString' must have class/struct/union",即使 toString 应该有一个合适的对象
- [class.union]/3 注释中"Absent default member initializers, ..."的句子的目的是什么?
- 在Union UB中,写作超出了较小尺寸的成员的规模
- Sockaddr union and getaddrinfo()
- Union-Find方法性能,迭代与递归
- 使用Flatbuffer Union通过ZeroMQ发送不同的事件
- 为什么对Aligned STD ::数组的初始自动矢量载荷是标量?(g /clang )
- 如何从C 中的Inner Union访问类成员
- 获取"Use of undefined type"和"Must have class/struct/union"错误
- 我对set.union(C++)有问题
- 不断收到错误"member reference base type 'string [1000]' is not a structure or union"不知道如何解决?
- A类{..};typedef a a_t;Union u{...; a_t ai;...};不起作用