class template
<type_traits>

std::alignment_of

template <class T> struct alignment_of;
Alignment of

Trait class to obtain the alignment requirement of type T.

If T is a reference type, the alignment obtained is the one required by the referenced type.

If T is an array type, the alignment obtained is the one required by the element type.

The class behaves as if defined as:
 
template<class T> struct alignment_of : integral_constant <size_t,alignof(T)> {};


Template parameters

T
A complete object type, or an array thereof, or a reference to a complete object type.

Member types

Inherited from integral_constant:
member typedefinition
value_typesize_t
typeintegral_constant<size_t,alignof(T)>

Member constants

Inherited from integral_constant:
member constantdefinition
valuealignof(T)

Member functions

Inherited from integral_constant:

Example

1
2
3
4
5
6
7
8
9
10
11
12
// alignment_of example
#include <iostream>
#include <type_traits>

int main() {
  std::cout << "alignment_of:" << std::endl;
  std::cout << "char: " << std::alignment_of<char>::value << std::endl;
  std::cout << "int: " << std::alignment_of<int>::value << std::endl;
  std::cout << "int[20]: " << std::alignment_of<int[20]>::value << std::endl;
  std::cout << "long long int: " << std::alignment_of<long long int>::value << std::endl;
  return 0;
}


Possible output:
alignment_of
char: 1
int: 4
int[20]: 4
long long int: 8

See also

<type_traits>