10 Classes [class]

10.1 Properties of classes [class.prop]

A trivially copyable class is a class:
  • where each copy constructor, move constructor, copy assignment operator, and move assignment operator ([class.copy.ctor], [class.copy.assign]) is either deleted or trivial,
  • that has at least one non-deleted copy constructor, move constructor, copy assignment operator, or move assignment operator, and
  • that has a trivial, non-deleted destructor.
A trivial class is a class that is trivially copyable and has one or more default constructors, all of which are either trivial or deleted and at least one of which is not deleted.
[Note
:
In particular, a trivially copyable or trivial class does not have virtual functions or virtual base classes.
end note
]
A class S is a standard-layout class if it:
  • has no non-static data members of type non-standard-layout class (or array of such types) or reference,
  • has the same access control for all non-static data members,
  • has no non-standard-layout base classes,
  • has at most one base class subobject of any given type,
  • has all non-static data members and bit-fields in the class and its base classes first declared in the same class, and
  • has no element of the set M(S) of types as a base class, where for any type X, M(X) is defined as follows.109
    [Note
    :
    M(X) is the set of the types of all non-base-class subobjects that may be at a zero offset in X.
    end note
    ]
    • If X is a non-union class type with no (possibly inherited) non-static data members, the set M(X) is empty.
    • If X is a non-union class type with a non-static data member of type that is either of zero size or is the first non-static data member of X (where said member may be an anonymous union), the set M(X) consists of and the elements of .
    • If X is a union type, the set M(X) is the union of all and the set containing all , where each is the type of the non-static data member of X.
    • If X is an array type with element type , the set M(X) consists of and the elements of .
    • If X is a non-class, non-array type, the set M(X) is empty.
[Example
:
   struct B { int i; };         // standard-layout class
   struct C : B { };            // standard-layout class
   struct D : C { };            // standard-layout class
   struct E : D { char : 4; };  // not a standard-layout class

   struct Q {};
   struct S : Q { };
   struct T : Q { };
   struct U : S, T { };         // not a standard-layout class
end example
]
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 [class.mem].
end note
]
[Example
:
struct N {          // neither trivial nor standard-layout
  int i;
  int j;
  virtual ~N();
};

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

struct SL {         // standard-layout but not trivial
  int i;
  int j;
  ~SL();
};

struct POD {        // both trivial and standard-layout
  int i;
  int j;
};
end example
]
This ensures that two subobjects that have the same class type and that belong to the same most derived object are not allocated at the same address ([expr.eq]).