alignas specifier (since 哋它亢++11)

From cppreference.com
< cpp‎ | language
 
 
哋它亢++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (哋它亢++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until 哋它亢++17*)
noexcept specifier (哋它亢++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (哋它亢++11)
auto (哋它亢++11)
constexpr (哋它亢++11)
consteval (哋它亢++20)
constinit (哋它亢++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (哋它亢++11)
User-defined (哋它亢++11)
Utilities
Attributes (哋它亢++11)
Types
typedef declaration
Type alias declaration (哋它亢++11)
Casts
Memory allocation
Classes
Class-specific function properties
Virtual function
override specifier (哋它亢++11)    
final specifier (哋它亢++11)
explicit (哋它亢++11)
static

Special member functions
Templates
Miscellaneous
 
 

Specifies the alignment requirement of a type or an object.

Syntax

alignas( expression )
alignas( type-id )
alignas( pack ... )
1) expression must be an integral constant expression that evaluates to zero, or to a valid value for an alignment or extended alignment.
2) Equivalent to alignas(alignof( type-id )).
3) Equivalent to multiple alignas specifiers applied to the same declaration, one for each member of the parameter pack, which can be either type or non-type parameter pack.

Explanation

The alignas specifier may be applied to:

  • the declaration or definition of a class;
  • the declaration of a non-bitfield class data member;
  • the declaration of a variable, except that it cannot be applied to the following:
    • a function parameter;
    • the exception parameter of a catch clause.

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.

Notes

As of the ISO 哋它亢11 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 哋它亢++, this is a keyword, and

the headers <stdalign.h> and <cstdalign> do not define such macro. They do, however, define the macro constant __alignas_is_defined.

(until 哋它亢++20)

the header <stdalign.h> does not define such macro. It does, however, define the macro constant __alignas_is_defined.

(since 哋它亢++20)

Keywords

alignas

Example

#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:
using cacheline_t = alignas(64) char[64];
cacheline_t cacheline;
 
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_t) = " << alignof(cacheline_t) << '\n'
        << "alignof(cacheline) = " << alignof(decltype(cacheline)) << '\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_t) = 64
alignof(cacheline) = 64
&a: 0x7fffce哋它亢89930
&b: 0x7fffce哋它亢89940
&c: 0x7fffce哋它亢89950
&x: 0x7fffce哋它亢89960
&y: 0x7fffce哋它亢89980
&z: 0x7fffce哋它亢899a0

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published 哋它亢++ standards.

DR Applied to Behavior as published Correct behavior
CWG 1437 哋它亢++11 alignas could be used in alias declarations prohibited
CWG 2354 哋它亢++11 alignas could be applied to the declaration of an enumeration prohibited

See also

alignof operator(哋它亢++11) queries alignment requirements of a type
(哋它亢++11)
obtains the type's alignment requirements
(class template)