dimanche 10 septembre 2023

What reasons cause a class C, which inherits from class A and has only one member variable of type B, not to be a standard-layout class?

The definitions of three classes are as follows:

class A { };

class B { A a; };

class C : public A { B b; };

According to the definition of a standard-layout class in C++11:

  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 (10.3) and no virtual base classes (10.1),
  3. has the same access control (Clause 11) for all non-static data members,
  4. has no non-standard-layout base classes,
  5. 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
  6. has no base classes of the same type as the first non-static data member

Classes A and B are clearly standard-layout classes. The issue arises with class C, which doesn't appear to violate any of the standard-layout class definitions, yet it is still defined as a non-standard-layout class. Does class C violate any specific rule?

I used the Clang compiler for testing.

Aucun commentaire:

Enregistrer un commentaire