This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 119a. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.
2025-12-20
[Accepted as a DR at the November, 2022 meeting.]
Consider:
struct A { int i; char c; }; struct B { int i; alignas(8) char c; }; union U { A a; B b; };On a lot of platforms,A andB do not have thesame layout, yet 11.4.1 [class.mem.general] paragraph 23 does notconsider differences in alignment in the rules for "common initialsequence":
The common initial sequence of two standard-layout struct(11.2 [class.prop]) types is the longest sequence ofnon-static data members and bit-fields in declaration order, startingwith the first such entity in each of the structs, such thatcorresponding entities have layout-compatible types(6.9 [basic.types]), either both entities are declared with theno_unique_address attribute (9.13.11 [dcl.attr.nouniqueaddr]) orneither is, and either both entities are bit-fields with the samewidth or neither is a bit-field.
In the following example,
struct S0 { alignas(16) char x[128]; int i; }; struct alignas(16) S1 { char x[128]; int i; };S0 andS1 have the same alignment, yet per thesuggested rules below, they will not be layout-compatible.
Suggested resolution [SUPERSEDED]:
Change in 11.4.1 [class.mem.general] paragraphs 23-25 as follows (alsoadd bullets):
The common initial sequence of two standard-layout struct(11.2 [class.prop]) types is the longest sequence ofnon-static data members and bit-fields in declaration order, startingwith the first such entity in each of the structs, such that
- corresponding entities have layout-compatible types(6.9 [basic.types]),
- either both entities havealignment-specifiers thatspecify equivalent alignment or neither entity hasanalignment-specifier (9.13.2 [dcl.align]),
- either both entities are declared with theno_unique_address attribute (9.13.11 [dcl.attr.nouniqueaddr]) orneither is, and
- either both entities are bit-fields with the same width or neitheris a bit-field.
[...]
Two standard-layout struct (11.2 [class.prop]) types arelayout-compatible classes if their common initial sequence comprisesall members and bit-fields of both classes (6.9 [basic.types])and either both types are declared withalignment-specifiers that specify equivalent alignment orneither type has analignment-specifier.
Two standard-layout unions are layout-compatible if they have thesame number of non-static data members andcorresponding non-staticdata members (in any order)
- have layout-compatible types (6.9.1 [basic.types.general])and
- either bothhavealignment-specifiers that specify equivalent alignment orneither has analignment-specifier.
Proposed resolution (approved by CWG telecon 2022-08-26):
Change in 11.4.1 [class.mem.general] paragraphs 23-25 as follows (alsoadd bullets):
The common initial sequence of two standard-layout struct(11.2 [class.prop]) types is the longest sequence ofnon-static data members and bit-fields in declaration order, startingwith the first such entity in each of the structs, such that
- corresponding entities have layout-compatible types(6.9 [basic.types]),
- corresponding entities have the same alignment requirements (6.8.3 [basic.align]),
- either both entities are declared with theno_unique_address attribute (9.13.11 [dcl.attr.nouniqueaddr]) orneither is, and
- either both entities are bit-fields with the same width or neitheris a bit-field.
[...]