std::aligned_union (3) - Linux Manuals

std::aligned_union: std::aligned_union


std::aligned_union - std::aligned_union


Defined in header <type_traits>
template< std::size_t Len, class... Types > (since C++11)
struct aligned_union;

Provides the nested type type, which is a trivial standard-layout type of a size and alignment suitable for use as uninitialized storage for an object of any of the types listed in Types. The size of the storage is at least Len. std::aligned_union also determines the strictest (largest) alignment requirement among all Types and makes it available as the constant alignment_value.
If sizeof...(Types) == 0 or if any of the types in Types is not a complete object type, the behavior is undefined.
It is implementation-defined whether any extended_alignment is supported.

Member types

Name Definition
type the trivial type suitable for storage of any type from Types

Helper types

template< std::size_t Len, class... Types > (since C++14)
using aligned_union_t = typename aligned_union<Len,Types...>::type;

Member constants

alignment_value the strictest alignment requirement of all Types
                (public static member constant)

Possible implementation

  #include <algorithm>
  template <std::size_t Len, class... Types>
  struct aligned_union
      static constexpr std::size_t alignment_value = std::max({alignof(Types)...});

      struct type
        alignas(alignment_value) char _s[std::max({Len, sizeof(Types)...})];


 This section is incomplete
 Reason: no example

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG_2979 C++11 complete type wasn't required requires complete types

See also

alignment_of obtains the type's alignment requirements
                (class template)

aligned_storage defines the type suitable for use as uninitialized storage for types of given size
                (class template)