103 lines
6.4 KiB
Markdown
103 lines
6.4 KiB
Markdown
[dcl.align]
|
||
|
||
# 9 Declarations [[dcl]](./#dcl)
|
||
|
||
## 9.13 Attributes [[dcl.attr]](dcl.attr#dcl.align)
|
||
|
||
### 9.13.2 Alignment specifier [dcl.align]
|
||
|
||
[1](#1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L9569)
|
||
|
||
An [*alignment-specifier*](dcl.attr.grammar#nt:alignment-specifier "9.13.1 Attribute syntax and semantics [dcl.attr.grammar]") may be applied to a variable
|
||
or to a class data member, but it shall not be applied to a bit-field, a function
|
||
parameter, or an [*exception-declaration*](except.pre#nt:exception-declaration "14.1 Preamble [except.pre]") ([[except.handle]](except.handle "14.4 Handling an exception"))[.](#1.sentence-1)
|
||
|
||
An [*alignment-specifier*](dcl.attr.grammar#nt:alignment-specifier "9.13.1 Attribute syntax and semantics [dcl.attr.grammar]") may also be applied to the declaration
|
||
of a class (in an[*elaborated-type-specifier*](dcl.type.elab#nt:elaborated-type-specifier "9.2.9.5 Elaborated type specifiers [dcl.type.elab]") ([[dcl.type.elab]](dcl.type.elab "9.2.9.5 Elaborated type specifiers")) or[*class-head*](class.pre#nt:class-head "11.1 Preamble [class.pre]") ([[class]](class "11 Classes")), respectively)[.](#1.sentence-2)
|
||
|
||
An [*alignment-specifier*](dcl.attr.grammar#nt:alignment-specifier "9.13.1 Attribute syntax and semantics [dcl.attr.grammar]") with an ellipsis is a pack expansion ([[temp.variadic]](temp.variadic "13.7.4 Variadic templates"))[.](#1.sentence-3)
|
||
|
||
[2](#2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L9580)
|
||
|
||
When the [*alignment-specifier*](dcl.attr.grammar#nt:alignment-specifier "9.13.1 Attribute syntax and semantics [dcl.attr.grammar]") is of the formalignas( [*constant-expression*](expr.const#nt:constant-expression "7.7 Constant expressions [expr.const]") ):
|
||
|
||
- [(2.1)](#2.1)
|
||
|
||
the [*constant-expression*](expr.const#nt:constant-expression "7.7 Constant expressions [expr.const]") shall be an integral constant expression
|
||
|
||
- [(2.2)](#2.2)
|
||
|
||
if the constant expression does not evaluate to an alignment
|
||
value ([[basic.align]](basic.align "6.8.3 Alignment")), or evaluates to an extended alignment and
|
||
the implementation does not support that alignment in the context of the
|
||
declaration, the program is ill-formed[.](#2.sentence-1)
|
||
|
||
[3](#3)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L9592)
|
||
|
||
An [*alignment-specifier*](dcl.attr.grammar#nt:alignment-specifier "9.13.1 Attribute syntax and semantics [dcl.attr.grammar]") of the formalignas( [*type-id*](dcl.name#nt:type-id "9.3.2 Type names [dcl.name]") ) has the same
|
||
effect as alignas([alignof](expr.alignof "7.6.2.6 Alignof [expr.alignof]")( [*type-id*](dcl.name#nt:type-id "9.3.2 Type names [dcl.name]") ))[.](#3.sentence-1)
|
||
|
||
[4](#4)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L9597)
|
||
|
||
The alignment requirement of an entity is the strictest nonzero alignment
|
||
specified by its [*alignment-specifier*](dcl.attr.grammar#nt:alignment-specifier "9.13.1 Attribute syntax and semantics [dcl.attr.grammar]")*s*, if any;
|
||
otherwise, the [*alignment-specifier*](dcl.attr.grammar#nt:alignment-specifier "9.13.1 Attribute syntax and semantics [dcl.attr.grammar]")*s* have no effect[.](#4.sentence-1)
|
||
|
||
[5](#5)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L9602)
|
||
|
||
The combined effect of all [*alignment-specifier*](dcl.attr.grammar#nt:alignment-specifier "9.13.1 Attribute syntax and semantics [dcl.attr.grammar]")*s* 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*](dcl.attr.grammar#nt:alignment-specifier "9.13.1 Attribute syntax and semantics [dcl.attr.grammar]")*s* appertaining to that entity
|
||
were omitted[.](#5.sentence-1)
|
||
|
||
[*Example [1](#example-1)*: struct alignas(8) S {};struct alignas(1) U { S s;}; // error: U specifies an alignment that is less strict than if the alignas(1) were omitted. â *end example*]
|
||
|
||
[6](#6)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L9617)
|
||
|
||
If the defining declaration of an entity has an[*alignment-specifier*](dcl.attr.grammar#nt:alignment-specifier "9.13.1 Attribute syntax and semantics [dcl.attr.grammar]"), any non-defining
|
||
declaration of that entity shall either specify equivalent alignment or have no[*alignment-specifier*](dcl.attr.grammar#nt:alignment-specifier "9.13.1 Attribute syntax and semantics [dcl.attr.grammar]")[.](#6.sentence-1)
|
||
|
||
Conversely, if any declaration of an entity has an[*alignment-specifier*](dcl.attr.grammar#nt:alignment-specifier "9.13.1 Attribute syntax and semantics [dcl.attr.grammar]"),
|
||
every defining
|
||
declaration of that entity shall specify an equivalent alignment[.](#6.sentence-2)
|
||
|
||
No diagnostic is required if declarations of an entity have
|
||
different [*alignment-specifier*](dcl.attr.grammar#nt:alignment-specifier "9.13.1 Attribute syntax and semantics [dcl.attr.grammar]")*s* in different translation units[.](#6.sentence-3)
|
||
|
||
[*Example [2](#example-2)*: // Translation unit #1:struct S { int x; } s, *p = &s;
|
||
|
||
// Translation unit #2:struct alignas(16) S; // ill-formed, no diagnostic required: definition of S lacks alignmentextern S* p; â *end example*]
|
||
|
||
[7](#7)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L9640)
|
||
|
||
[*Example [3](#example-3)*:
|
||
|
||
An aligned buffer with an alignment requirement
|
||
of A and holding N elements of type T can 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[.](#7.sentence-2)
|
||
|
||
â *end example*]
|
||
|
||
[8](#8)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/declarations.tex#L9653)
|
||
|
||
[*Example [4](#example-4)*: alignas(double) void f(); // error: alignment applied to functionalignas(double) unsigned char c[sizeof(double)]; // array of characters, suitably aligned for a doubleextern unsigned char c[sizeof(double)]; // no alignas necessaryalignas(float)extern unsigned char c[sizeof(double)]; // error: different alignment in declaration â *end example*]
|