为什么这个结构不是标准布局? [英] Why is this struct not standard-layout?

查看:60
本文介绍了为什么这个结构不是标准布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段代码值得一千个单词。

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


推荐答案

来自标准布局的定义类(第9类,第7段)

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.

这篇关于为什么这个结构不是标准布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆