是否有一个编译时函数/宏来确定c++ 0x结构体是否为POD
Is there a compile-time func/macro to determine if a C++0x struct is POD?
我希望有一个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在头文件<type_traits>
中引入了一个类型特征库用于这种自省,并且有一个is_pod
类型特征。我相信您会将其与static_assert
结合使用,如下所示:
static_assert(std::is_pod<A>::value, "A must be a POD type.");
我使用的是ISO草案N3092,所以有可能这是过时的。我去查一下最新的草案,确认一下。
EDIT:根据最新的草案(N3242),这仍然有效。看来这就是解决办法了!
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 检查输入是否不是整数或数字
- 是否可以初始化不可复制类型的成员变量(或基类)
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 此代码是否违反一个定义规则
- 是否需要删除包含对象的"pair"?
- 是否可以从int转换为enum类类型
- 无论条件是否为true,if总是在c++中执行
- 如何找到大小'x'数组是否完全填充,在C++?
- 检查值是否在集合p1和p2中,但不在p3中
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 检查 std::shared_ptr<> 的当前底层类型是否为 T
- 在c++中检查长方体是否尽可能快地重叠(无迭代)
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- C/C++预处理器是否可以检测一些编译器选项
- 是否可以用"iostream"包装现有的TCP/OOpenSSL会话