Why is this structure not a standard layout?

A piece of code is worth a thousand words.

#include <iostream> #include <type_traits> using namespace std; struct A { int a; }; struct B : A { int b; }; int main() { cout << is_standard_layout<B>::value << endl; // output false! WHY? return 0; } 
+4
source share
1 answer

From the definition of standard layout classes (ยง9 Classes, paragraph 7)

[...]
* either does not have non-static data members in the derived class and no more than one base class with non-static data members, or does not have base classes with non-static data elements and
[...]

Both the derived class itself and its base contain non-static data elements in your case. So this is not a standard layout.

+7
source

All Articles