Skip to content

Latest commit

 

History

History
82 lines (61 loc) · 1.98 KB

aligned-union-class.md

File metadata and controls

82 lines (61 loc) · 1.98 KB
description title ms.date f1_keywords helpviewer_keywords ms.assetid
Learn more about: aligned_union Class
aligned_union Class
11/04/2016
type_traits/std::aligned_union
aligned_union
9931a44d-3a67-4f29-a0f6-d47a7cf560ac

aligned_union Class

Provides a POD type large enough and suitably aligned to store a union type, and the size required.

Syntax

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

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

Parameters

Len
The alignment value for the largest type in the union.

Types
The distinct types in the underlying union.

Remarks

Use the class template to get the alignment and size needed to store a union in uninitialized storage. The member typedef type names a POD type suitable for storage of any type listed in Types; the minimum size is Len. The static member alignment_value of type std::size_t contains the strictest alignment required of all the types listed in Types.

Example

The following example shows how to use aligned_union to allocate an aligned stack buffer to place a union.

// std__type_traits__aligned_union.cpp
#include <iostream>
#include <type_traits>

union U_type
{
    int i;
    float f;
    double d;
    U_type(float e) : f(e) {}
};

typedef std::aligned_union<16, int, float, double>::type aligned_U_type;

int main()
{
    // allocate memory for a U_type aligned on a 16-byte boundary
    aligned_U_type au;
    // do placement new in the aligned memory on the stack
    U_type* u = new (&au) U_type(1.0f);
    if (nullptr != u)
    {
        std::cout << "value of u->i is " << u->i << std::endl;
        // must clean up placement objects manually!
        u->~U_type();
    }
}
value of u->i is 1065353216

Requirements

Header: <type_traits>

Namespace: std

See also

<type_traits>
alignment_of Class