Why does std :: aligned_union need a minimum size as a template parameter?

std::aligned_union has the parameter std::size_t "minimum length". Now the ordinary union does not have this, so I wonder why this is necessary. Can someone explain to me why?

+3
c ++ 11
source share
2 answers

The corresponding article looks like N2140 . The reason is that you can reconfigure types. The above example is that you can create a page-aligned data structure.

(I first had aligned_storage and aligned_union, sorry, sorry).

Aligned_storage is described in N2140 as the likely base class of aligned_union. N2140 also describes (but does not provide) the implementation of the aligned_storage<N, ...> point as a warpper of the aligned char[N] . Obviously for this you need N.

Because it is a nontrivial trivial type;)

std::aligned_union is a POD type with at least a given size and alignment with the specified types. These types should not be POD types, from which it becomes apparent that they cannot be members.

However, you can use the new location to place T[n] in this repository provided that aligned_union is aligned for T and its size is not less than sizeof(T[n]) Strike>

+1
source share

I put this for general programming situations where a list of variational type may be empty.

In this case, a certain minimum size will be used so that you still control the alignment (there was no type from which the alignment value follows)

+3
source share

All Articles