Namespaces
Variants

std:: aligned_union

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
aligned_union
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
ヘッダーで定義 <type_traits>
template < std:: size_t Len, class ... Types >
struct aligned_union ;
(C++11以降)
(C++23で非推奨)

ネストされた型 type を提供します。これは、 trivial standard-layout 型であり、 Types にリストされたいずれかの型のオブジェクトを未初期化ストレージとして使用するのに適したサイズとアライメントを持ちます。ストレージのサイズは少なくとも Len です。 std::aligned_union はまた、すべての Types の中で最も厳しい(最大の)アライメント要件を決定し、それを定数 alignment_value として利用可能にします。

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

いずれかの extended alignment がサポートされるかどうかは実装定義です。

プログラムが std::aligned_union に対する特殊化を追加する場合、動作は未定義です。

目次

メンバー型

名前 定義
type Types の任意の型を格納するのに適した trivial かつ standard-layout の型

ヘルパー型

template < std:: size_t Len, class ... Types >
using aligned_union_t = typename aligned_union < Len,Types... > :: type ;
(C++14以降)
(C++23で非推奨)

メンバー定数

alignment_value
[static]
すべての 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)...})];
    };
};

#include <iostream>
#include <string>
#include <type_traits>
int main()
{
    std::cout << sizeof(std::aligned_union_t<0, char>) << ' ' // 1
              << sizeof(std::aligned_union_t<2, char>) << ' ' // 2
              << sizeof(std::aligned_union_t<2, char[3]>) << ' ' // 3 (!)
              << sizeof(std::aligned_union_t<3, char[4]>) << ' ' // 4
              << sizeof(std::aligned_union_t<1, char, int, double>) << ' '    // 8
              << sizeof(std::aligned_union_t<12, char, int, double>) << '\n'; // 16 (!)
    using var_t = std::aligned_union<16, int, std::string>;
    std::cout << "var_t::alignment_value = " << var_t::alignment_value << '\n'
              << "sizeof(var_t::type) = " << sizeof(var_t::type) << '\n';
    var_t::type aligned_storage;
    int* int_ptr = new(&aligned_storage) int(42); // placement new
    std::cout << "*int_ptr = " << *int_ptr << '\n';
    std::string* string_ptr = new(&aligned_storage) std::string("bar");
    std::cout << "*string_ptr = " << *string_ptr << '\n';
    *string_ptr = "baz";
    std::cout << "*string_ptr = " << *string_ptr << '\n';
    string_ptr->~basic_string();
}

出力例:

1 2 3 4 8 16
var_t::alignment_value = 8
sizeof(var_t::type) = 32
*int_ptr = 42
*string_ptr = bar
*string_ptr = baz

欠陥報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR Applied to Behavior as published Correct behavior
LWG 2979 C++11 完全型は要求されていなかった 完全型を要求する

関連項目

型のアライメント要件を取得する
(クラステンプレート)
(since C++11) (deprecated in C++23)
指定されたサイズの型の未初期化ストレージとして使用するのに適した型を定義する
(クラステンプレート)