Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::aligned_union

      From cppreference.com
      <cpp‎ |types
       
       
      Metaprogramming library
      Type traits
      Type categories
      (C++11)
      (C++11)(DR*)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11) 
      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
       
      Defined in header<type_traits>
      template<std::size_t Len,class...Types>
      struct aligned_union;
      (since C++11)
      (deprecated in C++23)

      Provides the nested typetype, which is atrivialstandard-layout type of a size and alignment suitable for use as uninitialized storage for an object of any of the types listed inTypes. The size of the storage is at leastLen.std::aligned_union also determines the strictest (largest) alignment requirement among allTypes and makes it available as the constantalignment_value.

      Ifsizeof...(Types)==0 or if any of the types inTypes is not a complete object type, the behavior is undefined.

      It is implementation-defined whether anyextended alignment is supported.

      If the program adds specializations forstd::aligned_union, the behavior is undefined.

      Contents

      [edit]Member types

      Name Definition
      type a trivial and standard-layout type suitable for storage of any type fromTypes

      [edit]Helper types

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

      [edit]Member constants

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

      [edit]Possible implementation

      #include <algorithm> template<std::size_t Len,class...Types>struct aligned_union{staticconstexprstd::size_t alignment_value=std::max({alignof(Types)...}); struct type{        alignas(alignment_value)char _s[std::max({Len, sizeof(Types)...})];};};

      [edit]Example

      Run this code
      #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 newstd::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();}

      Possible output:

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

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2979C++11complete type was not requiredrequires complete types

      [edit]See also

      obtains the type's alignment requirements
      (class template)[edit]
      (since C++11)(deprecated in C++23)
      defines the type suitable for use as uninitialized storage for types of given size
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/types/aligned_union&oldid=155465"

      [8]ページ先頭

      ©2009-2025 Movatter.jp