std::alignment_of

Header: <type_traits>

Provides the member constant value equal to the alignment requirement of the type T, as if obtained by an alignof expression. If T is an array type, returns the alignment requirements of the element type. If T is a reference type, returns the alignment requirements of the type referred to.

# Declarations

template< class T >
struct alignment_of;

(since C++11)

# Notes

This type trait predates the alignof keyword, which can be used to obtain the same value with less verbosity.

# Example

#include <cstdint>
#include <iostream>
#include <type_traits>
 
struct A {};
struct B
{
    std::int8_t p;
    std::int16_t q;
};
 
int main()
{
    std::cout << std::alignment_of<A>::value << ' ';
    std::cout << std::alignment_of<B>::value << ' ';
    std::cout << std::alignment_of<int>() << ' '; // alt syntax
    std::cout << std::alignment_of_v<double> << '\n'; // c++17 alt syntax
}

# See also