Why is this struct not standard-layout?

我的梦境 提交于 2020-02-23 11:33:24

问题


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; 
}

回答1:


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.



来源:https://stackoverflow.com/questions/13900421/why-is-this-struct-not-standard-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!