空基类优化现在是一个强制性优化(至少对于标准布局类)? [英] Is the Empty Base Class Optimization now a mandatory optimization (at least for standard-layout classes)?

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

问题描述

根据 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:



  • 没有非标准布局类型的非静态数据成员类型)或引用,

  • has no non-static data members of type non-standard-layout class (or array of such types) or reference,

没有虚拟函数(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 表示:


使用 reinterpret_cast 适当地转换为标准布局结构体对象,指向其初始成员(或者如果该成员是位字段,则指向它的单元驻留),反之亦然。 [注意:因此,为了实现适当的对齐,可能在标准布局结构体对象内有未命名的填充,但不在其开始。 -end note]

A pointer to a standard-layout struct object, suitably converted using a reinterpret_cast, points to its initial member (or if that member is a bit-field, then to the unit in which it resides) and vice versa. [ Note: There might therefore be unnamed padding within a standard-layout struct object, but not at its beginning, as necessary to achieve appropriate alignment. —end note]

这似乎意味着 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: http://www.open-std .org / jtc1 / sc22 / WG21 / docs / papers / 2007 / n2342.htm#ABI

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编译器文档还声明: http://docwiki.embarcadero.com/RADStudio/zh-CN/Is_standard_layout

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


两个标准布局结构如果它们具有相同数量的非静态数据成员和对应的非静态数据成员(以声明顺序)具有布局兼容类型(3.9)。

Two standard-layout struct (Clause 9) types are layout-compatible if they have the same number of non-static data members and corresponding non-static data members (in declaration order) have layout-compatible types (3.9).

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

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

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

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