◐ Shell
clean mode source ↗

std::aligned_union - cppreference.com

提供: cppreference.com

<tbody> </tbody>

template< std::size_t Len, class... Types > struct aligned_union;

(C++11以上)

Types にリストされた任意の型のオブジェクトのための未初期化記憶域として使用するのに適したサイズとアライメントのトリビアル標準レイアウト型であるメンバ型 type を提供します。 記憶域のサイズは少なくとも Len です。 std::aligned_union はすべての Types のうち最も厳しい (大きな) アライメント要件を調べ、定数 alignment_value として利用できるようにします。

sizeof...(Types) == 0 の場合、または Types の型のいずれかが完全オブジェクト型でない場合、動作は未定義です。

任意の拡張アライメントがサポートされるかどうかは処理系定義です。

メンバ型

名前 定義
type Types の任意の型の記憶域に適したトリビアルな型

ヘルパー型

<tbody> </tbody>

template< std::size_t Len, class... Types > using aligned_union_t = typename aligned_union<Len,Types...>::type;

(C++14以上)

メンバ定数

すべての Types のうち最も厳しいアライメント要件
(パブリック静的メンバ定数)

実装例

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

欠陥報告

以下の動作変更欠陥報告は以前に発行された C++ 標準に遡って適用されました。

DR 適用先 発行時の動作 正しい動作
LWG 2979 C++11 complete type wasn't required requires complete types

関連項目