alignof operator (since C++11)

Queries alignment requirements of a type.

# Notes

See alignment for the meaning and properties of the value returned by alignof.

# Example

#include <iostream>
 
struct Foo
{
    int   i;
    float f;
    char  c;
};
 
// Note: alignas(alignof(long double)) below can be
// simplified to alignas(long double) if desired.
struct alignas(alignof(long double)) Foo2
{
    // put your definition here
}; 
 
struct Empty {};
 
struct alignas(64) Empty64 {};
 
#define SHOW(expr) std::cout << #expr << " = " << (expr) << '\n'
 
int main()
{
    SHOW(alignof(char));
    SHOW(alignof(int*));
    SHOW(alignof(Foo));
    SHOW(alignof(Foo2));
    SHOW(alignof(Empty));
    SHOW(alignof(Empty64));
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
CWG 1305C++11type-id could not represent a reference to an arraywith an unknown bound but a complete element typeallowed

# See also