是否有编译时func /宏来确定一个C ++ 0x结构是否是POD? [英] Is there a compile-time func/macro to determine if a C++0x struct is POD?
问题描述
我想要一个C ++ 0x static_assert 来测试给定的结构类型为 POD (至防止其他程序员无意中用新成员破坏它)。即
I'd like to have a C++0x static_assert that tests whether a given struct type is POD (to prevent other programmers from inadvertently breaking it with new members). ie,
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 code>宏或内在的,我可以在这里使用?我找不到一个在任何C + + 0x docs,但当然网络的信息在0x仍然是相当碎片。
Is there some kind of is_pod_type()
macro or intrinsic that I can use here? I couldn't find one in any C++0x docs, but of course the web's info on 0x is still rather fragmentary.
推荐答案
p> C ++ 0x在头部< type_traits>
中引入了一个类型traits库用于这种内省,并且有一个 is_pod
type trait。我相信你会结合使用它
static_assert
如下:
C++0x introduces a type traits library in the header <type_traits>
for this sort of introspection, and there is an is_pod
type trait. I believe that you would use it in conjunction with static_assert
as follows:
static_assert(std::is_pod<A>::value, "A must be a POD type.");
我使用ISO草稿N3092来处理,所以有可能是过时的。
I'm using ISO draft N3092 for this, so there's a chance that this is out of date. I'll go look this up in the most recent draft to confirm it.
EDIT :根据最近的草稿( N3242 ),这仍然有效。看起来这是做到的方式!
EDIT: According to the most recent draft (N3242) this is still valid. Looks like this is the way to do it!
这篇关于是否有编译时func /宏来确定一个C ++ 0x结构是否是POD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!