Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 467
K-ballo Avatar asked May 28 '12 18:05

K-ballo


People also ask

What is the use of empty class in C++?

Empty class: It is a class that does not contain any data members (e.g. int a, float b, char c, and string d, etc.) However, an empty class may contain member functions.

What does standard layout mean?

Standard-layout types can have user-defined special member functions. In addition, standard layout types have these characteristics: no virtual functions or virtual base classes. all non-static data members have the same access control. all non-static members of class type are standard-layout.


1 Answers

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; }; 
like image 155
Jonathan Wakely Avatar answered Sep 30 '22 12:09

Jonathan Wakely