Querying the alignment of a type

suggest change

The alignment requirement of a type can be queried using the alignof keyword as a unary operator. The result is a constant expression of type std::size_t, i.e., it can be evaluated at compile time.

#include <iostream>
int main() {
    std::cout << "The alignment requirement of int is: " << alignof(int) << '\n';
}

Possible output

The alignment requirement of int is: 4

If applied to an array, it yields the alignment requirement of the element type. If applied to a reference type, it yields the alignment requirement of the referenced type. (References themselves have no alignment, since they are not objects.)

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


keywords:
* class
* float
* bool
* void
* char
* short
* long
* double
* int
* enum
* if
* do
* else
* true
* false
* const
* for
* goto
* struct
* case
* catch
* return
* signed
* static
* this
* switch
* throw
* try
* break
* union
* asm
* Querying the alignment of a type
* sizeof

Table Of Contents
8 Arrays
11 Loops
14 keywords
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype