[ACCEPTED]-trivial vs. standard layout vs. POD-typetraits

Accepted answer
Score: 78

I don't think it can be done in truly layman's 27 terms, at least without a lot of extra explanation. One 26 important point is static vs. dynamic initialization, but 25 explaining that to a layman would be several 24 pages in itself...

PODs were (mis-)defined 23 in C++98. There are really two separate 22 intents involved, neither expressed very 21 well: 1) that if you compile a C struct 20 declaration in C++, what you get should 19 be equivalent to what you had in C. 2) A 18 POD will only ever need/use static (not 17 dynamic) initialization.

C++0x/11 drops the 16 "POD" designation (almost) entirely, in 15 favor of "trivial" and "standard layout". Standard 14 layout is intended to capture the first 13 intent -- creating something with a layout 12 the same as you'd get in C. Trivial is intended 11 to capture the support for static initialization.

Since 10 new T vs. new T() deals with initialization, you probably want 9 is_trivial.

I'm not sure about compiler magic being 8 required. My immediate reaction would be 7 probably yes, but knowing some of the things 6 people have done with TMP, I have a hard 5 time being certain somebody couldn't do 4 this too...

Edit: for examples, perhaps it's 3 best to just quote the examples from N3290:

struct N { // neither trivial nor standard-layout
   int i;
   int j;
    virtual ~N();

struct T { // trivial but not standard-layout
    int i;
    int j;

struct SL { // standard-layout but not trivial
    int i;
    int j;

struct POD { // both trivial and standard-layout
    int i;
    int j;

As 2 you can undoubtedly guess, POD is also a POD 1 struct.

Score: 8

For POD types new T() is value-initialization(will 4 value-initialize all members) ,and new T will 3 not initialize the members (default-initialization). For 2 differences between different forms of initialization 1 see this question. Bottom line: you need is_pod.

Score: 4

Layout is how the members of an object of class, struct 51 or a union is arranged in the memory. This 50 may be contigious or not.Quite often the 49 language specifies the layout , but if there 48 are something like virtual function ,virtual 47 base class etc , then the complier is free 46 to choose the layout and that may not be 45 contigiuos. This leads to several issues 44 that we cannot serialize the object suitably 43 or pass to programs written in other languages 42 like C or functions like memcopy because 41 we cannot copy the data reliably that it 40 is not in contigious locations.

Inorder to 39 enable the compilers and c++ programs to 38 support the above mentioned operations c++ has 37 introduced 3 categories for the simple structures 36 and classes.


A class or struct is trivial 35 if it follows the rules:

  • no virtual function or virtual base class
  • no user-defined constructor/operator/destructor
  • base class should be trivial
  • all the class members should be trivial

If a class is trivial 34 then its layout is contigious but there 33 may be padding accordingly and the compiler 32 is free to choose the order of the members 31 in the layout. So even though we can memcopy 30 the object it is not reliable if we copy 29 that object to C program.We can have different 28 access specifiers in the same class itself 27 and if use a parameterized constructor obviously 26 we have to specify the default contructor. But 25 if you want to keep the class trivial then 24 you should explicitly make the constructor 23 default.The constructors should be public.


  • No virtual functions and virtual base class
  • All the non-static members should have same access specifiers
  • All non-static members should be of standard layout
  • All base classes should be of standard layout
  • All the members of the base class should be static
  • The type of the base class and first non-static member of the class should not be the same

Standard 22 layout is well defined and it can be memcopied 21 reliably and passed to C programs suitably. Also 20 standard layout functions can have user-defined 19 special member funtions like constructor 18 and destructor.

POD(Plain old data)

If a class or structure is 17 both trivial and standard layout , then 16 it is said to be POD.Each member is stored 15 in the order as specified when the object 14 is declared. POD classes should have POD 13 non-static data members.POD classes can 12 be copied or passed to C programs reliably.

C++ program 11 with a class which is trivial , standard 10 layout and therefore POD.

class xyz
    int a;
    int b;
    xyz() = default;
    xyz(int x, int y) :a(x), b(y) {}
int main() {
    std::cout << std::is_trivial<xyz>() << std::endl;//true
    std::cout << std::is_standard_layout<xyz>() << std::endl;//true
    std::cout << std::is_pod<xyz>() << std::endl;//true

Literal types

For a literal type 9 the layout can be determined at the time 8 of compilation .Examples of literal types 7 are void,scalar types such as int , float 6 etc,references,Arrays of void, scalar types 5 or references and A class that has a trivial 4 destructor, and one or more constexpr constructors 3 that are not move or copy constructors. Additionally, all 2 its non-static data members and base classes 1 must be literal types and not volatile

More Related questions