C++: Understanding Aggregate, POD, (with Trivial Class and Standard Layout )

Strongly recommended:

    http://stackoverflow.com/questions/4178175/what-are-aggregates-and-pods-and-how-why-are-they-special/4178176#4178176

 

先贴一个关于Aggregate的定义(from C++03  standard ):

An aggregate is an array or a class (clause 9) with no user-declared constructors (12.1), no private or protected non-static data members (clause 11), no base classes (clause 10), and no virtual functions (10.3)

Aggregate有什么用? 之所以能够用brace-initializer {} 来对变量进行初始化,就是由于它是Aggregate的(当然如果你的类有 std::initializer_list<> ,那么你也可以用 {} 来初始化)。具体见上文链接。

 

POD的定义(同样来自C++03 Standard):

A POD-struct is an aggregate class that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-defined copy assignment operator and no user-defined destructor. Similarly, a POD-union is an aggregate union that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-defined copy assignment operator and no user-defined destructor. A POD class is a class that is either a POD-struct or a POD-union.

POD有什么用? 之所以能够用 memcpy() , memset() 等直接操作内存的函数来处理对象(比如将一个POD所在的那块内存完整地拷贝到另外一个地方,然后再拷贝回来,C++标准保证其中内容不会发生变化),就是由于它是POD。

POD一定是Aggregate,POD是Aggregate的子集

在最新的C++11标准中这些定义都有一些轻微的变化,不过变化不大。

 

关于Trivial Class 和 Standard Layout 这些也可以再上面的链接中找到答案。

 

 

 

 

 

 

 

 

 

 

 

:)

posted @ 2016-04-10 20:55  walkerlala  阅读(239)  评论(0编辑  收藏  举报