C++14 标准布局类型可以对字段使用 alignas 吗?
Can C++14 standard-layout types use `alignas` for fields?
我想使用模板来简化具有非平凡类型的联合的构造。以下似乎 "work" 在实践中,但在技术上不符合规范:
template<typename T> struct union_entry {
void (*destructor_)(void *); // how to destroy type T when active
T value_;
};
union U {
union_entry<A> a;
union_entry<B> b;
// ... some constructor and destructor...
};
问题是(根据 N4141)只有当两个结构都是标准布局类型时,您才能访问联合中两个结构的公共初始序列(即 destructor_
字段)--at至少根据 9.5.1 中的非规范性注释。根据 9.0.7,标准布局类型不能有任何非标准布局的非静态数据成员。因此,如果 A 或 B 不是标准布局,则在错误的联合中访问 destructor_
将变得非法。
一个漏洞似乎是通过将 value_
变成 alignas(T) char[sizeof(T)]
来制作 union_entry
标准布局。 9.0.7 似乎没有排除使用 alignas
。因此,我的问题是:以下是否是任何类型的标准布局类型 T
? 因此可以将 value_
转换为 T&
模拟前面的示例,同时仍然允许 destructor_
在非活动 union_entry
?
中使用
template<typename T> struct union_entry {
void (*destructor_)(void *);
alignas(T) char value_[sizeof(T)];
}
在 clang-3.8.1 和 g++-6.2.1 中,std::is_standard_layout
建议 union_entry<T>
是标准布局,即使 T
不是。这是我想如何使用此技术的完整工作示例:
#include <cassert>
#include <iostream>
#include <new>
#include <string>
using namespace std;
template<typename T> struct union_entry {
void (*destructor_)(void *);
alignas(T) char value_[sizeof(T)];
union_entry() : destructor_(nullptr) {}
~union_entry() {} // Just to cause error in unions w/o destructors
void select() {
if (destructor_)
destructor_(this);
destructor_ = destroy_helper;
new (static_cast<void *>(value_)) T{};
}
T &get() {
assert(destructor_ == destroy_helper);
return *reinterpret_cast<T *>(value_);
}
private:
static void destroy_helper(void *_p) {
union_entry *p = static_cast<union_entry *>(_p);
p->get().~T();
p->destructor_ = nullptr;
}
};
union U {
union_entry<int> i;
union_entry<string> s;
U() : i() {}
~U() { if (i.destructor_) i.destructor_(this); }
};
int
main()
{
U u;
u.i.select();
u.i.get() = 5;
cout << u.i.get() << endl;
u.s.select();
u.s.get() = "hello";
cout << u.s.get() << endl;
// Notice that the string in u.s is destroyed by calling
// u.i.destructor_, not u.s.destructor_
}
感谢@Arvid,他向我指出了 std::aligned_storage
,我相信在标准的第 20.10.7.6 节中有一个明确的(尽管是非规范的)答案(我认为它与 N4141 相同).
首先,Table57说的aligned_storage
》成员typedef type
应为 POD 类型 ...",其中 9.0.10 明确 "A POD struct is a non-union class that is both a trivial class and a standard-layout class."
接下来20.10.7.6.1给出了一个不规范的示例实现:
template <std::size_t Len, std::size_t Alignment>
struct aligned_storage {
typedef struct {
alignas(Alignment) unsigned char __data[Len];
} type;
};
很明显 alignas
的使用不会阻止类型成为标准布局。
我想使用模板来简化具有非平凡类型的联合的构造。以下似乎 "work" 在实践中,但在技术上不符合规范:
template<typename T> struct union_entry {
void (*destructor_)(void *); // how to destroy type T when active
T value_;
};
union U {
union_entry<A> a;
union_entry<B> b;
// ... some constructor and destructor...
};
问题是(根据 N4141)只有当两个结构都是标准布局类型时,您才能访问联合中两个结构的公共初始序列(即 destructor_
字段)--at至少根据 9.5.1 中的非规范性注释。根据 9.0.7,标准布局类型不能有任何非标准布局的非静态数据成员。因此,如果 A 或 B 不是标准布局,则在错误的联合中访问 destructor_
将变得非法。
一个漏洞似乎是通过将 value_
变成 alignas(T) char[sizeof(T)]
来制作 union_entry
标准布局。 9.0.7 似乎没有排除使用 alignas
。因此,我的问题是:以下是否是任何类型的标准布局类型 T
? 因此可以将 value_
转换为 T&
模拟前面的示例,同时仍然允许 destructor_
在非活动 union_entry
?
template<typename T> struct union_entry {
void (*destructor_)(void *);
alignas(T) char value_[sizeof(T)];
}
在 clang-3.8.1 和 g++-6.2.1 中,std::is_standard_layout
建议 union_entry<T>
是标准布局,即使 T
不是。这是我想如何使用此技术的完整工作示例:
#include <cassert>
#include <iostream>
#include <new>
#include <string>
using namespace std;
template<typename T> struct union_entry {
void (*destructor_)(void *);
alignas(T) char value_[sizeof(T)];
union_entry() : destructor_(nullptr) {}
~union_entry() {} // Just to cause error in unions w/o destructors
void select() {
if (destructor_)
destructor_(this);
destructor_ = destroy_helper;
new (static_cast<void *>(value_)) T{};
}
T &get() {
assert(destructor_ == destroy_helper);
return *reinterpret_cast<T *>(value_);
}
private:
static void destroy_helper(void *_p) {
union_entry *p = static_cast<union_entry *>(_p);
p->get().~T();
p->destructor_ = nullptr;
}
};
union U {
union_entry<int> i;
union_entry<string> s;
U() : i() {}
~U() { if (i.destructor_) i.destructor_(this); }
};
int
main()
{
U u;
u.i.select();
u.i.get() = 5;
cout << u.i.get() << endl;
u.s.select();
u.s.get() = "hello";
cout << u.s.get() << endl;
// Notice that the string in u.s is destroyed by calling
// u.i.destructor_, not u.s.destructor_
}
感谢@Arvid,他向我指出了 std::aligned_storage
,我相信在标准的第 20.10.7.6 节中有一个明确的(尽管是非规范的)答案(我认为它与 N4141 相同).
首先,Table57说的aligned_storage
》成员typedef type
应为 POD 类型 ...",其中 9.0.10 明确 "A POD struct is a non-union class that is both a trivial class and a standard-layout class."
接下来20.10.7.6.1给出了一个不规范的示例实现:
template <std::size_t Len, std::size_t Alignment>
struct aligned_storage {
typedef struct {
alignas(Alignment) unsigned char __data[Len];
} type;
};
很明显 alignas
的使用不会阻止类型成为标准布局。