class template
<type_traits>

std::aligned_storage

template <size_t Len, size_t Align = /* default alignment */>
struct aligned_storage;
Aligned storage
Obtains a POD type suitable to use as storage for an object of a size of at most Len bytes, aligned as specified by Align.

The obtained type is aliased as member type aligned_storage::type.

If Align is omitted, the most stringent alignment requirement for any C++ object type whose size is no greater than Len is used as default alignment.

Template parameters

Len
The size of the storage object, in bytes.
This shall not be zero.
size_t is an unsigned integral type.
Align
The alignment requested, in bytes. The actual alignment used may be a divisor of this value.
size_t is an unsigned integral type.

Member types

member typedefinition
typeA POD type suitable to store Len bytes, aligned as specified by Align.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// aligned_storage example
#include <iostream>
#include <type_traits>

struct A {  // non-POD type
  int avg;
  A (int a, int b) : avg((a+b)/2) {}
};

typedef std::aligned_storage<sizeof(A),alignof(A)>::type A_pod;

int main() {
  A_pod a,b;
  new (&a) A (10,20);
  b=a;
  std::cout << reinterpret_cast<A&>(b).avg << std::endl;

  return 0;
}


Output:
15

See also

<type_traits>