# alignof operator (since C++11)

< cpp‎ | language

C++
 Language Headers Library concepts Language support library Diagnostics library Utilities library Strings library Containers library Algorithms library Iterators library Numerics library Input/output library Localizations library Regular expressions library (C++11) Atomic operations library (C++11) Thread support library (C++11) Filesystem library (C++17) Technical Specifications

Expressions
 General value categories (lvalue, rvalue, xvalue) order of evaluation (sequence points) constant expressions unevaluated expressions primary expressions lambda-expression(C++11) Literals integer literals floating-point literals boolean literals character literals including escape sequences string literals null pointer literal(C++11) user-defined literal(C++11) Operators Assignment operators: a=b, a+=b, a-=b, a*=b, a/=b, a%=b, a&=b, a|=b, a^=b, a<<=b, a>>=b Increment and decrement: ++a, --a, a++, a-- Arithmetic operators: +a, -a, a+b, a-b, a*b, a/b, a%b, ~a, a&b, a|b, a^b, a<>b Logical operators: a||b, a&&b, !a Comparison operators: a==b, a!=b, ab, a<=b, a>=b, a<=>b(C++20) Member access operators: a[b], *a, &a, a->b, a.b, a->*b, a.*b Other operators: a(...), a,b, a?b:c Default comparisons(C++20) Alternative representations of operators Precedence and associativity Fold expression(C++17) new-expression delete-expression throw-expression alignof sizeof sizeof...(C++11) typeid noexcept(C++11) Operator overloading Conversions Implicit conversions const_cast static_cast reinterpret_cast dynamic_cast Explicit conversions (T)a, T(a) User-defined conversion

Queries alignment requirements of a type

## Contents

### Syntax

 alignof( type-id )

Returns a value of type std::size_t.

### Explanation

Returns the alignment, in bytes, required for any instance of the type indicated by type-id, which is either complete type, an array type, or a reference type.

If the type is reference type, the operator returns the alignment of referenced type; if the type is array type, alignment requirement of the element type is returned.

### 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;
};

struct Empty {};

struct alignas(64) Empty64 {};

int main()
{
std::cout << "Alignment of"  "\n"
"- char             : " << alignof(char)    << "\n"
"- pointer          : " << alignof(int*)    << "\n"
"- class Foo        : " << alignof(Foo)     << "\n"
"- empty class      : " << alignof(Empty)   << "\n"
"- alignas(64) Empty: " << alignof(Empty64) << "\n";
}

Possible output:

Alignment of
- char             : 1
- pointer          : 8
- class Foo        : 4
- empty class      : 1
- alignas(64) Empty: 64