alignas
specifier (since C++11)
Specifies the alignment requirement of a type or an object.
alignas( expression ) | ||
alignas( type-id ) | ||
alignas( pack ... ) |
alignas(alignof(type-id))
.The alignas
specifier may be applied to:
The object or the type declared by such a declaration will have its alignment requirement equal to the strictest (largest) non-zero expression of all alignas
specifiers used in the declaration, unless it would weaken the natural alignment of the type.
If the strictest (largest) alignas
on a declaration is weaker than the alignment it would have without any alignas
specifiers (that is, weaker than its natural alignment or weaker than alignas
on another declaration of the same object or type), the program is ill-formed:
struct alignas(8) S {}; struct alignas(1) U { S s; }; // error: alignment of U would have been 8 without alignas(1)
Invalid non-zero alignments, such as alignas(3)
are ill-formed.
Valid non-zero alignments that are weaker than another alignas
on the same declaration are ignored.
alignas(0)
is always ignored.
As of the ISO C11 standard, the C language has the _Alignas
keyword and defines alignas
as a preprocessor macro expanding to the keyword in the header <stdalign.h>
.
In C++, this is a keyword, and.
the headers | (until C++20) |
the header | (since C++20) |
#include <iostream> // every object of type struct_float will be aligned to alignof(float) boundary // (usually 4): struct alignas(float) struct_float { // your definition here }; // every object of type sse_t will be aligned to 32-byte boundary: struct alignas(32) sse_t { float sse_data[4]; }; // the array "cacheline" will be aligned to 64-byte boundary: alignas(64) char cacheline[64]; int main() { struct default_aligned { float data[4]; } a, b, c; sse_t x, y, z; std::cout << "alignof(struct_float) = " << alignof(struct_float) << '\n' << "sizeof(sse_t) = " << sizeof(sse_t) << '\n' << "alignof(sse_t) = " << alignof(sse_t) << '\n' << "alignof(cacheline) = " << alignof(alignas(64) char[64]) << '\n' << std::hex << std::showbase << "&a: " << &a << '\n' << "&b: " << &b << '\n' << "&c: " << &c << '\n' << "&x: " << &x << '\n' << "&y: " << &y << '\n' << "&z: " << &z << '\n'; }
Possible output:
alignof(struct_float) = 4 sizeof(sse_t) = 32 alignof(sse_t) = 32 alignof(cacheline) = 64 &a: 0x7fffcec89930 &b: 0x7fffcec89940 &c: 0x7fffcec89950 &x: 0x7fffcec89960 &y: 0x7fffcec89980 &z: 0x7fffcec899a0
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 1437 | C++11 | alignas could be used in alias declarations | prohibited |
CWG 2354 | C++11 | alignas could be applied to the declaration of an enumeration | prohibited |
alignof operator(C++11) | queries alignment requirements of a type |
(C++11) | obtains the type's alignment requirements (class template) |
C documentation for _Alignas, alignas |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/language/alignas