The Wayback Machine - https://web.archive.org/web/20210516014048/https://en.cppreference.com/w/cpp/types/alignment_of
Namespaces
Variants
Views
Actions

std::alignment_of

From cppreference.com
< cpp‎ | types
 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)

Elementary string conversions
(C++17)
(C++17)
Stacktrace
 
Type support
Basic types
Fundamental types
Fixed width integer types (C++11)
Numeric limits
C numeric limits interface
Runtime type information
Type traits
Type categories
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++14)
(C++11)
(C++11)(until C++20)
(C++11)(deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Constant evaluation context
Supported operations
Relationships and property queries
alignment_of
(C++11)
(C++11)
(C++11)
Type modifications
(C++11)(C++11)(C++11)
Type transformations
(C++11)
(C++11)
(C++17)
(C++11)(until C++20)(C++17)
 
Defined in header <type_traits>
template< class T >
struct alignment_of;
(since C++11)

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.

The behavior of a program that adds specializations for alignment_of or alignment_of_v (since C++17) is undefined.

Contents

[edit] Helper variable template

template< class T >
inline constexpr std::size_t alignment_of_v = alignment_of<T>::value;
(since C++17)

Inherited from std::integral_constant

Member constants

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

Member functions

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

Member types

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

[edit] Possible implementation

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

[edit] Notes

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

[edit] 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
}

Possible output:

1 2 4 8

[edit] See also

alignof operator(C++11) queries alignment requirements of a type[edit]
alignas specifier(C++11) specifies that the storage for the variable should be aligned by specific amount[edit]
defines the type suitable for use as uninitialized storage for types of given size
(class template) [edit]
defines the type suitable for use as uninitialized storage for all given types
(class template) [edit]
trivial type with alignment requirement as great as any other scalar type
(typedef) [edit]