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.)