What are Aggregates and PODs and how/why are they special?
[C++ / C][NOTE] The Lost Art of C Structure Packing by Eric S. Raymond
Aggregates: (least set)
An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1),
no brace-or-equal-initializers for non-static data members (9.2),
no private or protected non-static data members (Clause 11),
no base classes (Clause 10),
and no virtual functions (10.3).
POD:
A POD struct is a non-union class that is both a trivial class and a standard-layout class,
and has no non-static data members of type non-POD struct,
non-POD union (or array of such types).
Similarly, a POD union is a union that is both a trivial class and a standard layout class,
and has no non-static data members of type non-POD struct,
non-POD union (or array of such types).
A POD class is a class that is either a POD struct or a POD union.
Trivial class:
A trivially copyable class is a class that:
— has no non-trivial copy constructors (12.8),
— has no non-trivial move constructors (12.8),
— has no non-trivial copy assignment operators (13.5.3, 12.8),
— has no non-trivial move assignment operators (13.5.3, 12.8), and
— has a trivial destructor (12.4).
A trivial class is a class that has a trivial default constructor (12.1) and is trivially copyable.
[ Note: In particular, a trivially copyable or trivial class does not have virtual functions or virtual base classes.—end note ]
Non-trivial means:
A copy/move constructor for class X is trivial if it is not user-provided and if
— class X has no virtual functions (10.3) and no virtual base classes (10.1), and
— the constructor selected to copy/move each direct base class subobject is trivial, and
— for each non-static data member of X that is of class type (or array thereof), the constructor selected to copy/move that member is trivial;
otherwise the copy/move constructor is non-trivial.
Standard-layout:
A standard-layout class is a class that:
— has no non-static data members of type non-standard-layout class (or array of such types) or reference,
— has no virtual functions (10.3) and no virtual base classes (10.1),
— has the same access control (Clause 11) for all non-static data members,
— has no non-standard-layout base classes,
— either has no non-static data members in the most derived class and at most one base class with non-static data members,
or has no base classes with non-static data members, and
— has no base classes of the same type as the first non-static data member.
A standard-layout struct is a standard-layout class defined with the class-key struct or the class-key class.
A standard-layout union is a standard-layout class defined with the class-key union.
[ Note: Standard-layout classes are useful for communicating with code written in other programming languages. Their layout is specified in 9.2.—end note ]
---------------
API:
template <typename T> struct std::is_pod;
template <typename T> struct std::is_trivial;
template <typename T> struct std::is_trivially_copyable;
template <typename T> struct std::is_standard_layout;
----------------
Uniform and Value-initialization
Below compiles in C++14, not C++11
struct B
{
int b_ {1};
};
B b{2};
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.