SysTutorials

Linux Manual Pages › Section 3

stdalignment_of(3)

std::alignment_of – std::alignment_of

Synopsis

Defined in header<type_traits>
    template <class T>
    (since C++ 11)
        struct alignment_of;

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.
If alignof(T) is not a valid expression, the behavior is undefined.
Helper variable template template< class T > (since C++17)

inline constexpr std::size_t alignment_of_v = alignment_of<T>::value;

Inherited from std::integral_constant

Member constants

value alignof(T)
         (public static member constant)
[static]

Member functions

                     converts the object to std::size_t, returns value
operator std::size_t (public member function)

operator() returns value
                     (public member function)
(C++14)

Member types

Type Definition
value_type std::size_t
type std::integral_constant<std::size_t, value>

Possible implementation

  template< class T >
  struct alignment_of : std::integral_constant<
                            std::size_t,
                            alignof(T)
                         > {};

Notes

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

Example

// Run this code

#include <iostream>
#include <type_traits>
class A
{
};
int main()
{
    std::cout << std::alignment_of<A>::value << '
';
    std::cout << std::alignment_of<int>() << '
';    // alt syntax
    std::cout << std::alignment_of_v<double> << '
'; // c++17 alt syntax
}

Output:

  1
  4
  8

See also

alignof_operator queries alignment requirements of a type (since C++11)
aligned_storage defines the type suitable for use as uninitialized storage for types of given size
                 (class template)
(C++11)
aligned_union defines the type suitable for use as uninitialized storage for all given types
                 (class template)
(C++11)
max_align_t trivial type with alignment requirement as great as any other scalar type
                 (typedef)
(C++11)