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:



Table Of Contents