You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 44e66ae
Browse filesBrowse the repository at this point in the historyBrowse files
In Visual Studio 2015 and later, use the C++11 standard [**`alignas`** specifier](../cpp/alignas-specifier.md) to control alignment. For more information, see [Alignment](../cpp/alignment-cpp-declarations.md).
10
+
In Visual Studio 2015 and later, use [**`alignas`** specifier](../cpp/alignas-specifier.md) (C++ 11) to control alignment. For more information, see [Alignment](../cpp/alignment-cpp-declarations.md).
The **`alignas`** specifier changes the alignment of a type or object. You should prefer usage of **`alignas`** over [`__declspec(align(#))`](align-cpp.md) for code portability. For more information, see [Alignment](../cpp/alignment-cpp-declarations.md).
10
+
The **`alignas`** specifier changes the alignment of a type or object in memory.
11
11
12
12
## Syntax
13
13
14
14
```cpp
15
-
alignas( type-id )
16
-
alignas( expression )
17
-
alignas(pack...)
15
+
alignas(expression)
16
+
alignas(type-id)
17
+
alignas(pack...)
18
18
```
19
19
20
20
## Remarks
21
21
22
-
You can use **`alignas`** specifier on a **`struct`**, **`class`**, **`union`**, or variable declaration. For `alignas( expression )`, the expression must be an integral constant expression that are powers of 2 (1, 2, 4, 8, 16, 32, 64...) or equal to `0`. All other expressions are ill-formed. A common use-case would be to control the alignment of a user-defined type as follows:
22
+
You can use **`alignas`** specifier on a **`struct`**, **`class`**, **`union`**, or variable declaration.
23
+
24
+
For `alignas(expression)`, the expression must be an integral constant expression that is `0` or a power of 2 (1, 2, 4, 8, 16, 32, 64...). All other expressions are ill-formed.
25
+
26
+
Prefer **`alignas`** over [`__declspec(align(#))`](align-cpp.md) for code portability.
27
+
28
+
A common use of `alignas` is to control the alignment of a user-defined type as shown in the following example:
23
29
24
30
```cpp
25
31
structalignas(8) S1
@@ -30,7 +36,10 @@ struct alignas(8) S1
30
36
static_assert(alignof(S1) == 8, "alignof(S1) should be 8");
31
37
```
32
38
33
-
See [`alignof`](alignof-operator.md) for more infomation about how to get the alignment value. When multiple **`alignas`** target the same declaration, the strictest alignment requirement will be taken (largest value). When the integral constant expression is `0`, the **`alignas`** will be ignored.
39
+
When multiple **`alignas`** are applied to the same declaration, the one with the largest value is used.\
40
+
When the **`alignas`** value is `0`, **`alignas`** is ignored.\
41
+
42
+
The following example shows how to use `alignas`:
34
43
35
44
```cpp
36
45
classalignas(4) alignas(16) C1 {};
@@ -52,7 +61,7 @@ static_assert(alignof(C1) == 16, "alignof(C1) should be 16");
52
61
static_assert(alignof(U1) == alignof(U2), "alignof(U1) should be equivalent to alignof(U2)");
53
62
```
54
63
55
-
Instead of using an integral constant for the alignment value, you may also choose to supply a type:
64
+
Instead of using an integral constant for the alignment value, you can supply a type. The type's default alignment is used as the alignment value, as shown in the following example:
56
65
57
66
```cpp
58
67
struct alignas(double) S2
@@ -63,9 +72,7 @@ struct alignas(double) S2
63
72
static_assert(alignof(S2) == alignof(double), "alignof(S2) should be equivalent to alignof(double)");
64
73
```
65
74
66
-
As seen from the [`static_assert`](static-assert.md), `alignas( type-id )` is analogous to `alignas(alignof( type ))` (identical to using an integral constant expression of the result).
67
-
68
-
A template parameter pack can also be used (`alignas ( pack... )`). It functions on the same concept where only the strictest alignment is effective.
75
+
A template parameter pack (`alignas ( pack... )`) can be used as the alignment value. Only the strictest alignment is used.
69
76
70
77
```cpp
71
78
template <typename... Ts>
@@ -75,29 +82,31 @@ class alignas(Ts...) C2
75
82
};
76
83
77
84
static_assert(alignof(C2<>) == 1, "alignof(C2<>) should be 1");
78
-
// Empty angle brackets can be omitted since C++17
79
-
// static_assert(alignof(C2) == 1, "alignof(C2) should be 1");
85
+
// Empty angle brackets can be omitted since C++17,: static_assert(alignof(C2) == 1, "alignof(C2) should be 1");
80
86
static_assert(alignof(C2<short, int>) == 4, "alignof(C2<short, int>) should be 4");
81
87
static_assert(alignof(C2<int, float, double>) == 8, "alignof(C2<int, float, double>) should be 8");
82
88
```
83
89
84
-
The resulting alignment (largest) of all the **`alignas`** (if more than 1) cannot be less than the natural alignment (without having any **`alignas`** specifier). Declaration and definition of user-defined types must match in alignment requirement.
90
+
If multiple **`alignas`** are applied, the resulting alignment is the largest of all the **`alignas`** values, and can't be less than the natural alignment of the type it is applied to.
91
+
The declaration and definition of user-defined types must have the same alignment value.
85
92
86
93
```cpp
87
94
// Declaration of `C3`
88
95
classalignas(16) C3;
89
96
90
-
// Definition of `C3` (with differing alignment requirement)
91
-
classalignas(32) C3 {}; // C2023, 'C3': Alignment (32) different from prior declaration (16)
97
+
// Definition of `C3` with differing alignment value
98
+
classalignas(32) C3 {}; //Error: C2023 'C3': Alignment (32) different from prior declaration (16)
92
99
93
100
intmain()
94
101
{
95
-
alignas(2) int x; // ill-formed, since the natural alignment of `int` is 4
102
+
alignas(2) int x; // ill-formed because the natural alignment of int is 4
96
103
}
97
104
```
98
105
99
-
For more information, see error [C2023](../error-messages/compiler-errors-1/compiler-error-c2023.md) and warning [C4359](../error-messages/compiler-warnings/compiler-warning-level-3-c4359.md).
0 commit comments