[dcl.align] (original) (raw)

9 Declarations [dcl.dcl]

9.12 Attributes [dcl.attr]

9.12.2 Alignment specifier [dcl.align]

When the alignment-specifier is of the formalignas( constant-expression ):

The combined effect of all alignment-specifiers in a declaration shall not specify an alignment that is less strict than the alignment that would be required for the entity being declared if all alignment-specifier_s_appertaining to that entity were omitted.

[Example 1: struct alignas(8) S {};struct alignas(1) U { S s;}; — _end example_]

If the defining declaration of an entity has analignment-specifier, any non-defining declaration of that entity shall either specify equivalent alignment or have noalignment-specifier.

Conversely, if any declaration of an entity has analignment-specifier, every defining declaration of that entity shall specify an equivalent alignment.

No diagnostic is required if declarations of an entity have different alignment-specifier_s_in different translation units.

[Example 2: struct S { int x; } s, *p = &s;struct alignas(16) S; extern S* p; — _end example_]

[Example 3:

An aligned buffer with an alignment requirement of A and holding N elements of type Tcan be declared as:alignas(T) alignas(A) T buffer[N];

Specifying alignas(T) ensures that the final requested alignment will not be weaker than alignof(T), and therefore the program will not be ill-formed.

— _end example_]

[Example 4: alignas(double) void f(); alignas(double) unsigned char c[sizeof(double)]; extern unsigned char c[sizeof(double)]; alignas(float) extern unsigned char c[sizeof(double)]; — _end example_]