本文介绍了空基类优化现在是一个强制性优化(至少对于标准布局类)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 C ++ 11 9.1 / 7(draft n3376), standard-layout p>

According to C++11 9.1/7 (draft n3376), a standard-layout class is a class that:

没有虚拟函数(10.3),没有虚拟基类(10.1),

has no virtual functions (10.3) and no virtual base classes (10.1),

对所有非静态数据成员具有相同的访问控制(条款11),

has the same access control (Clause11) for all non-static data members,

没有非标准布局基类,

在大多数派生类中没有非静态数据成员,最多一个基类具有非静态数据成员,或没有基类与非静态数据成员相同,

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

没有与第一个非静态数据成员相同类型的基类。

has no base classes of the same type as the first non-static data member.

这说明一个空类是一个 standard-layout 并且以空类作为基础的另一个类也是一个 standard-layout 类,只要该类的第一个非静态数据成员与基类的类型不同。

it follows that an empty class is a standard-layout class; and that another class with an empty class as a base is also a standard-layout class provided the first non-static data member of such class is not of the same type as the base.

此外, 9.2 / 19 表示:

这似乎意味着 Empty Base Class Optimization 至少对于标准布局类。我的观点是,如果空基本优化不是强制的,那么 standard-layout 类的布局不是标准的,而是取决于实现是否实现所述优化。我的推理是正确的,还是我错过了什么?

This seems to imply that the Empty Base Class Optimization is now a mandatory optimization, at least for standard-layout classes. My point is that if the empty base optimization isn't mandated, then the layout of a standard-layout class would not be standard but rather depend on whether the implementation implements or not said optimization. Is my reasoning correct, or am I missing something?

推荐答案

是的,你是正确的, PODs revisitedproposals:

Yes, you're correct, that was pointed out in the "PODs revisited" proposals: http://www.open-std.org/jtc1/sc22/WG21/docs/papers/2007/n2342.htm#ABI

Embarcadero编译器文档还声明:

The Embarcadero compiler docs also state it: http://docwiki.embarcadero.com/RADStudio/en/Is_standard_layout

另一个关键点是[class.mem] / 16

Another key point is [class.mem]/16

请注意,只有数据成员会影响布局兼容性,而不是基类,因此这两个标准布局类与布局兼容:

Note that only data members affect layout compatibility, not base classes, so these two standard layout classes are layout-compatible:

struct empty { };
struct stdlayout1 : empty { int i; };

struct stdlayout2 { int j; };

这篇关于空基类优化现在是一个强制性优化(至少对于标准布局类)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 12:10