alignas specifier (since C++11)

Specifies the alignment requirement of a type or an object.

# Notes

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 <stdalign.h> and do not define such macro. They do, however, define the macro constant __alignas_is_defined.

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

# 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];
};
 
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'
        << std::hex << std::showbase
        << "&a: " << &a << "\n"
           "&b: " << &b << "\n"
           "&c: " << &c << "\n"
           "&x: " << &x << "\n"
           "&y: " << &y << "\n"
           "&z: " << &z << '\n';
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
CWG 1437C++11alignas could be used in alias declarationsprohibited
CWG 2354C++11alignas could be applied to the declaration of an enumerationprohibited

# See also