如何使用标准::aligned_union

How to use std::aligned_union

本文关键字:union aligned 标准 何使用      更新时间:2023-10-16

在尝试学习如何使用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
  • 鸡蛋变种 - 第一部分