|
| 1 | +--- |
| 2 | +description: "Learn more about: alignas Specifier" |
| 3 | +title: "alignas Specifier" |
| 4 | +ms.date: "09/30/2023" |
| 5 | +f1_keywords: ["alignas"] |
| 6 | +helpviewer_keywords: ["alignas [C++]", "alignment of structures", "__alignof keyword [C++]", "alignof [C++]", "types [C++], alignment requirements"] |
| 7 | +--- |
| 8 | +# `alignas` Specifier |
| 9 | + |
| 10 | +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. |
| 11 | + |
| 12 | +## Syntax |
| 13 | + |
| 14 | +```cpp |
| 15 | +alignas( type-id ) |
| 16 | +alignas( expression ) |
| 17 | +alignas( pack... ) |
| 18 | +``` |
| 19 | + |
| 20 | +## Remarks |
| 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: |
| 23 | + |
| 24 | +```cpp |
| 25 | +struct alignas(8) S1 |
| 26 | +{ |
| 27 | + int x; |
| 28 | +}; |
| 29 | + |
| 30 | +static_assert(alignof(S1) == 8, "alignof(S1) should be 8"); |
| 31 | +``` |
| 32 | + |
| 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. |
| 34 | + |
| 35 | +```cpp |
| 36 | +class alignas(4) alignas(16) C1 {}; |
| 37 | + |
| 38 | +// `alignas(0)` ignored |
| 39 | +union alignas(0) U1 |
| 40 | +{ |
| 41 | + int i; |
| 42 | + float f; |
| 43 | +}; |
| 44 | + |
| 45 | +union U2 |
| 46 | +{ |
| 47 | + int i; |
| 48 | + float f; |
| 49 | +}; |
| 50 | + |
| 51 | +static_assert(alignof(C1) == 16, "alignof(C1) should be 16"); |
| 52 | +static_assert(alignof(U1) == alignof(U2), "alignof(U1) should be equivalent to alignof(U2)"); |
| 53 | +``` |
| 54 | +
|
| 55 | +Instead of using an integral constant for the alignment value, you may also choose to supply a type: |
| 56 | +
|
| 57 | +```cpp |
| 58 | +struct alignas(double) S2 |
| 59 | +{ |
| 60 | + int x; |
| 61 | +}; |
| 62 | +
|
| 63 | +static_assert(alignof(S2) == alignof(double), "alignof(S2) should be equivalent to alignof(double)"); |
| 64 | +``` |
| 65 | + |
| 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. |
| 69 | + |
| 70 | +```cpp |
| 71 | +template <typename... Ts> |
| 72 | +class alignas(Ts...) C2 |
| 73 | +{ |
| 74 | + char c; |
| 75 | +}; |
| 76 | + |
| 77 | +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"); |
| 80 | +static_assert(alignof(C2<short, int>) == 4, "alignof(C2<short, int>) should be 4"); |
| 81 | +static_assert(alignof(C2<int, float, double>) == 8, "alignof(C2<int, float, double>) should be 8"); |
| 82 | +``` |
| 83 | + |
| 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. |
| 85 | + |
| 86 | +```cpp |
| 87 | +// Declaration of `C3` |
| 88 | +class alignas(16) C3; |
| 89 | + |
| 90 | +// Definition of `C3` (with differing alignment requirement) |
| 91 | +class alignas(32) C3 {}; // C2023, 'C3': Alignment (32) different from prior declaration (16) |
| 92 | + |
| 93 | +int main() |
| 94 | +{ |
| 95 | + alignas(2) int x; // ill-formed, since the natural alignment of `int` is 4 |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 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