cplusplus / CWG

Core Working Group
23 stars 7 forks source link

[class.mem.general] p23 does not impose the sequence must start with the first non-static data member #448

Closed xmh0511 closed 11 months ago

xmh0511 commented 11 months ago

Full name of submitter (unless configured in github; will be published with the issue): Jim X

The common initial sequence of two standard-layout struct ([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 ([basic.types]),
  • corresponding entities have the same alignment requirements ([basic.align]),
  • either both entities are declared with the no_unique_address attribute ([dcl.attr.nouniqueaddr]) or neither is, and
  • either both entities are bit-fields with the same width or neither is a bit-field.

Given the formal example

struct A { int a; char b; };
// ....
struct E { unsigned int e; char b; };

The comment says

The common initial sequence of A and E is empty.

However, according to the rule, aren't the second non-static data members of A and E the first **such entities? From this read, the common sequence of A and E starts with their second members and only comprises them.

Suggested Resolution

The intent should mean the common sequence must start with the first non-static data member of these classes instead of the first such entity.

frederick-vs-ja commented 11 months ago

The first such entity may be an unnamed bit-field, which is not a non-static data member.

E.g. given

struct A { int : 23; char c; };
struct B { int b : 23; char c; };

A and B are layout-compatible.

t3nsor commented 11 months ago

I'm not seeing anything that's unclear about the current wording. In "first such entity", the word "such" refers to the just-described class of entities, namely non-static data members and bit-fields, so "first such entity" means "first non-static data member or bit-field", which is exactly what it should mean.