0

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
  6. 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.

Adrian
  • 10,246
  • 4
  • 44
  • 110
  • I'm confused about what the requirement "has all non-static data members and bit-fields in the class and its base classes first declared in the same class" means – Nathan Pierson Oct 03 '22 at 21:32
  • Note that you are missing a point: number 6 starts with "given the class as S"... – Nelfeal Oct 03 '22 at 21:51
  • @Nelfeal, it was there, I just forgot to separate the line into two lines. Thx for pointing it out. – Adrian Oct 04 '22 at 00:26
  • I made a couple edits to that cppreference page for human readability, let's see if they stick – Cubbi Oct 04 '22 at 15:23

1 Answers1

3

The cppreference description is a little confusing.

The standard (search "standard-layout class is") says:

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.

You can see from the part I emphasized that a class with a non-static data member and a base class that itself has a non-static data member is not standard-layout.

I suspect this point is described as follows in cppreference:

has all non-static data members and bit-fields in the class and its base classes first declared in the same class

The point is that you must be able to cast the address of a standard-layout class object to a pointer to its first member, and back. It boils down to compatibility between C++ and C.

See also: Why is C++11's POD "standard layout" definition the way it is?.

Nelfeal
  • 12,593
  • 1
  • 20
  • 39
  • Doesn't the following [Demo](https://godbolt.org/z/G6oaohGfe) have at most 1 base class with non-static data members? – Adrian Oct 04 '22 at 00:45
  • 1
    @Adrian Indeed it has *at most one base class with non-static data members*, but it doesn't have *no non-static data members in the most derived class*. The complete sentence basically says "only one class in the inheritance hierarchy can have non-static data members". – Nelfeal Oct 04 '22 at 02:56
  • Ugh! The wording is just horrible. :( – Adrian Oct 04 '22 at 03:39
  • @Adrian Well yes, standardese must formal, precise, consistant, complete, ... Just like legalese: that's partly why lawyers exist. – Nelfeal Oct 04 '22 at 11:10
  • Still, saying that only one class in the hierarchy can have non-static data members is far clearer and shouldn't be misinterpreted. even in a legalize perspective. – Adrian Oct 04 '22 at 12:45
  • @Adrian Except, 0 can have members and you are also fine. Your definition requires defining "heirarchy", which I suspect the standard does not. – Yakk - Adam Nevraumont Oct 04 '22 at 13:31