std::aligned_union

From cppreference.com
< cpp‎ | types
 
 
 
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++14)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++14)
(C++11)
Type trait constants
Metafunctions
(C++17)
(C++17)
(C++17)
Supported operations
Relationships and property queries
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type modifications
(C++11)(C++11)(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
Type transformations
(C++11)
aligned_union
(C++11)
(C++11)
(C++11)
(C++17)
(C++11)
(C++11)
(C++11)
(C++11)
 
Defined in header <type_traits>
template< std::size_t Len, class... Types >
struct aligned_union;
(since C++11)

Provides the member typedef type, which is a POD 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, the behavior is undefined.

Contents

[edit] Member types

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

[edit] Helper types

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

[edit] Member constants

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

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

[edit] Example

[edit] See also

(C++11)
obtains the type's alignment requirements
(class template)
defines the type suitable for use as uninitialized storage for types of given size
(class template)