-
Notifications
You must be signed in to change notification settings - Fork 8
Description
Full name of submitter (unless configured in github; will be published with the issue): Jiang An
Reference (section label): [basic.align]
Link to reflector thread (if any):
Issue description:
(from cplusplus/draft#8787)
Currently, the standard wording doesn't directly specify the alignment requirement of a non-static data member.
Consider the following example.
struct S1 { int n; };
struct S2 { alignas(int) int n; };It's unclear whether the alignment requirement of the n member of S1 is same as that of its type. If not so, then alignas(int) in S2 strengthens alignment requirements, and S1 and S2 are not layout-incompatible.
Also, consider the following trickier example.
struct W1 { char c; }; // assume that alignof(W1) == 1
struct alignas(2) W2 { char c; };
struct alignas(4) S3 { W1 w; };
struct alignas(4) S4 { W2 w; };In this example, for each S3 or S4 object o, its o.w member subobject is indirectly required to be located at an address required by alignas(4), because o.w must have the same address as o. Can we infer that such a w member has a stricter alignment requirement? I don't think so, but the current wording doesn't clearly rejects such reading.
Suggested resolution:
Modify [basic.align] as indicated:
- [...] An object type imposes an alignment requirement on every object or non-static data member of that type; stricter alignment can be requested using the alignment specifier ([dcl.align]). [...]
[Note ?: It is possible for a member subobject to have stricter restrictions on its locatable address due to the alignment requirement of its enclosing class object. However, such restrictions do not affect the alignment requirement of the corresponding non-static data member. — end note]