此常见问题解答涉及聚合和POD,并涵盖以下材料:
通俗地说,琐碎类型,标准布局类型和POD之间有什么区别?
具体来说,我想确定是否new T与new T()任何模板参数不同T.哪种类型的性状is_trivial,is_standard_layout而且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." ); …Run Code Online (Sandbox Code Playgroud) 以下是否符合C++ 11标准(= default在类的定义之外)?
// In header file
class Test
{
public:
Test();
~Test();
};
// In cpp file
Test::Test() = default;
Test::~Test() = default;
Run Code Online (Sandbox Code Playgroud)