简体   繁体   中英

Why is this struct not standard-layout?

A piece of code is worth a thousands 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; 
}

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

[...]
* 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
[...]

Both the most-derived class and its base have non-static data members in your case. So it's not standard layout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM