Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

c++ - Is the Empty Base Class Optimization now a mandatory optimization (at least for standard-layout classes)?

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,

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

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

  • has no non-standard-layout base classes,

  • 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.

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.

Furthermore, 9.2/19 states that:

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]

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

Another key point is [class.mem]/16

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...