std:: aligned_union
| Type traits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time rational arithmetic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time integer sequences | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
(C++14)
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
헤더에 정의됨
<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
로 제공합니다.
만약
sizeof...
(
Types
)
==
0
이거나
Types
내의 어떤 타입이 완전한 객체 타입이 아닌 경우, 동작은 정의되지 않습니다.
확장된 정렬(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
중 가장 엄격한 정렬 요구사항
(public static member constant) |
가능한 구현
#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 | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 2979 | C++11 | 완전한 타입이 요구되지 않았음 | 완전한 타입을 요구함 |
참고 항목
|
(C++11)
|
타입의 정렬 요구 사항을 얻음
(클래스 템플릿) |
|
(since C++11)
(deprecated in C++23)
|
주어진 크기의 타입들을 위한 초기화되지 않은 저장 공간으로 사용하기 적합한 타입을 정의
(클래스 템플릿) |