Skip to content

Commit 44e66ae

Browse files
TylerMSFTTylerMSFT
authored andcommitted
edit pass
1 parent 819324a commit 44e66ae

3 files changed

Lines changed: 35 additions & 27 deletions

File tree

‎docs/cpp/align-cpp.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ helpviewer_keywords: ["align __declspec keyword", "__declspec keyword [C++], ali
77
---
88
# `align` (C++)
99

10-
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).
1111

1212
**Microsoft Specific**
1313

‎docs/cpp/alignas-specifier.md‎

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
---
2-
description: "Learn more about: alignas Specifier"
3-
title: "alignas Specifier"
4-
ms.date: "09/30/2023"
2+
description: "Learn more about: alignas specifier"
3+
title: "alignas specifier"
4+
ms.date: 10/31/2023
55
f1_keywords: ["alignas"]
6-
helpviewer_keywords: ["alignas [C++]", "alignment of structures", "__alignof keyword [C++]", "alignof [C++]", "types [C++], alignment requirements"]
6+
helpviewer_keywords: ["alignas [C++]", "__alignof keyword [C++]", "alignof [C++]", "types [C++], alignment requirements"]
77
---
8-
# `alignas` Specifier
8+
# `alignas` specifier
99

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. For more information, see [Alignment](../cpp/alignment-cpp-declarations.md).
10+
The **`alignas`** specifier changes the alignment of a type or object in memory.
1111

1212
## Syntax
1313

1414
```cpp
15-
alignas( type-id )
16-
alignas( expression )
17-
alignas( pack... )
15+
alignas(expression)
16+
alignas(type-id)
17+
alignas(pack...)
1818
```
1919

2020
## Remarks
2121

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:
2329

2430
```cpp
2531
struct alignas(8) S1
@@ -30,7 +36,10 @@ struct alignas(8) S1
3036
static_assert(alignof(S1) == 8, "alignof(S1) should be 8");
3137
```
3238

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`:
3443

3544
```cpp
3645
class alignas(4) alignas(16) C1 {};
@@ -52,7 +61,7 @@ static_assert(alignof(C1) == 16, "alignof(C1) should be 16");
5261
static_assert(alignof(U1) == alignof(U2), "alignof(U1) should be equivalent to alignof(U2)");
5362
```
5463
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:
5665
5766
```cpp
5867
struct alignas(double) S2
@@ -63,9 +72,7 @@ struct alignas(double) S2
6372
static_assert(alignof(S2) == alignof(double), "alignof(S2) should be equivalent to alignof(double)");
6473
```
6574

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.
6976

7077
```cpp
7178
template <typename... Ts>
@@ -75,29 +82,31 @@ class alignas(Ts...) C2
7582
};
7683

7784
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");
8086
static_assert(alignof(C2<short, int>) == 4, "alignof(C2<short, int>) should be 4");
8187
static_assert(alignof(C2<int, float, double>) == 8, "alignof(C2<int, float, double>) should be 8");
8288
```
8389

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.
8592

8693
```cpp
8794
// Declaration of `C3`
8895
class alignas(16) C3;
8996

90-
// Definition of `C3` (with differing alignment requirement)
91-
class alignas(32) C3 {}; // C2023, 'C3': Alignment (32) different from prior declaration (16)
97+
// Definition of `C3` with differing alignment value
98+
class alignas(32) C3 {}; // Error: C2023 'C3': Alignment (32) different from prior declaration (16)
9299

93100
int main()
94101
{
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
96103
}
97104
```
98105

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).
100-
101106
## See also
102107

103-
[`#pragma pack`](../preprocessor/pack.md)
108+
[`#pragma pack`](../preprocessor/pack.md)\
109+
[Alignment](../cpp/alignment-cpp-declarations.md)\
110+
[`alignof`](alignof-operator.md)\
111+
[C2023](../error-messages/compiler-errors-1/compiler-error-c2023.md)\
112+
[C4359](../error-messages/compiler-warnings/compiler-warning-level-3-c4359.md)

‎docs/cpp/alignment-cpp-declarations.md‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
---
22
title: "Alignment"
33
description: "How data alignment is specified in modern C++."
4-
ms.date: "12/11/2019"
5-
ms.assetid: a986d510-ccb8-41f8-b905-433df9183485
4+
ms.date: 10/31/2023
65
---
76
# Alignment
87

0 commit comments

Comments
 (0)