Created on 2022-05-03.00:00:00 last changed 16 months ago
Note from CWG telecon 2022-07-15
Needs more review.
Proposed resolution (approved by CWG telecon 2022-08-26):
Change in 11.4.1 [class.mem.general] paragraphs 23-25 as follows (also add bullets):
The common initial sequence of two standard-layout struct (11.2 [class.prop]) types is the longest sequence of non-static data members and bit-fields in declaration order, starting with the first such entity in each of the structs, such that
- corresponding entities have layout-compatible types (6.8 [basic.types]),
- corresponding entities have the same alignment requirements (6.7.6 [basic.align]),
- either both entities are declared with the no_unique_address attribute (9.12.12 [dcl.attr.nouniqueaddr]) or neither is, and
- either both entities are bit-fields with the same width or neither is a bit-field.
[...]
Suggested resolution [SUPERSEDED]:
Change in 11.4.1 [class.mem.general] paragraphs 23-25 as follows (also add bullets):
The common initial sequence of two standard-layout struct (11.2 [class.prop]) types is the longest sequence of non-static data members and bit-fields in declaration order, starting with the first such entity in each of the structs, such that
- corresponding entities have layout-compatible types (6.8 [basic.types]),
- either both entities have alignment-specifiers that specify equivalent alignment or neither entity has an alignment-specifier (9.12.2 [dcl.align]),
- either both entities are declared with the no_unique_address attribute (9.12.12 [dcl.attr.nouniqueaddr]) or neither is, and
- either both entities are bit-fields with the same width or neither is a bit-field.
[...]
Two standard-layout struct (11.2 [class.prop]) types are layout-compatible classes if their common initial sequence comprises all members and bit-fields of both classes (6.8 [basic.types]) and either both types are declared with alignment-specifiers that specify equivalent alignment or neither type has an alignment-specifier.
Two standard-layout unions are layout-compatible if they have the same number of non-static data members and corresponding non-static data members (in any order)
- have layout-compatible types (6.8.1 [basic.types.general]) and
- either both have alignment-specifiers that specify equivalent alignment or neither has an alignment-specifier.
[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 and B do not have the same layout, yet 11.4.1 [class.mem.general] paragraph 23 does not consider differences in alignment in the rules for "common initial sequence":
The common initial sequence of two standard-layout struct (11.2 [class.prop]) types is the longest sequence of non-static data members and bit-fields in declaration order, starting with the first such entity in each of the structs, such that corresponding entities have layout-compatible types (6.8 [basic.types]), either both entities are declared with the no_unique_address attribute (9.12.12 [dcl.attr.nouniqueaddr]) or neither is, and either both entities are bit-fields with the same width 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 and S1 have the same alignment, yet per the suggested rules below, they will not be layout-compatible.
History | |||
---|---|---|---|
Date | User | Action | Args |
2023-07-16 13:00:43 | admin | set | status: open -> c++23 |
2023-07-16 13:00:43 | admin | set | status: drwp -> open |
2023-02-18 18:43:04 | admin | set | status: dr -> drwp |
2022-11-25 05:14:04 | admin | set | status: ready -> dr |
2022-08-26 21:45:07 | admin | set | status: review -> ready |
2022-07-16 08:34:33 | admin | set | messages: + msg6886 |
2022-07-16 08:34:33 | admin | set | status: open -> review |
2022-07-15 16:16:37 | admin | set | messages: + msg6884 |
2022-05-03 07:16:46 | admin | set | messages: + msg6823 |
2022-05-03 00:00:00 | admin | create |