【发布时间】:2011-11-02 09:42:56
【问题描述】:
我想要一个 C++0x static_assert 来测试给定的结构类型是否为 POD(以防止其他程序员无意中用新成员破坏它)。即,
struct A // is a POD type
{
int x,y,z;
}
struct B // is not a POD type (has a nondefault ctor)
{
int x,y,z;
B( int _x, int _y, int _z ) : x(_x), y(_y), z(_z) {}
}
void CompileTimeAsserts()
{
static_assert( is_pod_type( A ) , "This assert should not fire." );
static_assert( is_pod_type( B ) , "This assert will fire and scold whoever added a ctor to the POD type." );
}
我可以在这里使用某种is_pod_type() 宏或内在函数吗?我在任何 C++0x 文档中都找不到,但当然,网络上有关 0x 的信息仍然相当零碎。
【问题讨论】:
-
请注意,在 C++0x 中,结构 B 不是 POD,因为它没有普通默认构造函数(请参阅 N3242 中的 9.0.10 和 9.0.6)。我不确定究竟什么才是微不足道的默认构造函数(参见 12.1.5),但我怀疑添加
B() = default;可能会将 struct B 变成 C++0x POD。
标签: c++ typetraits static-assert c++11