lundi 3 octobre 2022

Confused about std::is_standard_layout type trait

Standard-layout class describes a standard layout class that:

  1. has no non-static data members of type non-standard-layout class (or array of such types) or reference,
  2. has no virtual functions and no virtual base classes,
  3. has the same access control for all non-static data members,
  4. has no non-standard-layout base classes,
  5. has all non-static data members and bit-fields in the class and its base classes first declared in the same class, and given the class as S, has no element of the set M(S) of types as a base class, where M(X) for a type X is defined as:
    1. If X is a non-union class type with no (possibly inherited) non-static data members, the set M(X) is empty.
    2. If X is a non-union class type whose first non-static data member has type X0 (where said member may be an anonymous union), the set M(X) consists of X0 and the elements of M(X0).
    3. If X is a union type, the set M(X) is the union of all M(Ui) and the set containing all Ui, where each Ui is the type of the ith non-static data member of X.
    4. If X is an array type with element type Xe, the set M(X) consists of Xe and the elements of M(Xe).
    5. If X is a non-class, non-array type, the set M(X) is empty.

(numbered for convivence)

However, the following fails:

#include <type_traits>

struct A {
    int a;
};
struct B : A {
    int b;
};

static_assert(std::is_standard_layout<A>::value, "not standard layout"); // succeeds
static_assert(std::is_standard_layout<B>::value, "not standard layout"); // fails

Demo

I see that 1-4 are true, so is one of points under 5 false? I find the points under 5 a little confusing.

Aucun commentaire:

Enregistrer un commentaire