This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 113d. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-03-20


1672. Layout compatibility with multiple empty bases

Section: 11.4  [class.mem]     Status: CD4     Submitter: Richard Smith     Date: 2013-04-27

[Moved to DR at the November, 2014 meeting.]

The layout compatibility rules of 11.4 [class.mem] paragraph 16 are phrased only in terms of non-static data members, ignoring the existence of base classes:

Two standard-layout struct (Clause 11 [class]) 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 (6.8 [basic.types]).

However, this means that in an example like

  struct empty {};
  struct A { char a; };
  struct also_empty : empty {};
  struct C : empty, also_empty { char c; };
  union U {
    struct X { A a1, a2; } x;
    struct Y { C c1, c2; } y;
  } u;

u.x.a2.a and u.y.c2.c must have the same address, even though sizeof(A) would typically be 1 and sizeof(B) would need to be at least 2 to give the empty subobjects different addresses.

Proposed resolution (October, 2014):

Change Clause 11 [class] paragraph 7 as indicated and add the following as a new paragraph:

A class S is a standard-layout class is a class that if it:

M(X) is defined as follows:

[Note: M(X) is the set of the types of all non-base-class subobjects that are guaranteed in a standard-layout class to be at a zero offset in X. —end note]

(See also the related changes in the resolution for issue 1813.)