Cra*_*rks 22 c++ static-assert type-traits c++11
我想要一个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." );
}
Run Code Online (Sandbox Code Playgroud)
is_pod_type()我可以在这里使用某种宏观或内在吗?我在任何C++ 0x文档中找不到一个,但当然,关于0x的网络信息仍然相当零碎.
tem*_*def 27
C++ 0x在头部引入了一个类型特征库,<type_traits>用于这种内省,并且有一个is_pod类型特征.我相信你会将它与static_assert如下结合使用:
static_assert(std::is_pod<A>::value, "A must be a POD type.");
Run Code Online (Sandbox Code Playgroud)
我正在使用ISO草案N3092,所以有可能这已经过时了.我会在最近的草案中查看这个以确认它.
编辑:根据最新的草案(N3242),这仍然有效.看起来这是这样做的方法!
| 归档时间: |
|
| 查看次数: |
3265 次 |
| 最近记录: |