Title
Common initial sequence should consider over-alignment
Status
c++23
Section
11.4.1 [class.mem.general]
Submitter
Brian Bi

Created on 2022-05-03.00:00:00 last changed 16 months ago

Messages

Date: 2022-07-16.08:34:33

Note from CWG telecon 2022-07-15

Needs more review.

Date: 2022-08-26.21:45:07

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.

[...]

Date: 2022-09-25.18:08:42

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.

Date: 2022-11-15.00:00:00

[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:43adminsetstatus: open -> c++23
2023-07-16 13:00:43adminsetstatus: drwp -> open
2023-02-18 18:43:04adminsetstatus: dr -> drwp
2022-11-25 05:14:04adminsetstatus: ready -> dr
2022-08-26 21:45:07adminsetstatus: review -> ready
2022-07-16 08:34:33adminsetmessages: + msg6886
2022-07-16 08:34:33adminsetstatus: open -> review
2022-07-15 16:16:37adminsetmessages: + msg6884
2022-05-03 07:16:46adminsetmessages: + msg6823
2022-05-03 00:00:00admincreate