Is it possible to place std :: array <POD, N> in a union?
I have a union declared this way:
union
{
int all[4];
struct
{
int a, b, c, d;
};
};
An array point allsimply simplifies iteration over 4 fields.
To make this even easier, I would like to replace it with std::array<int, 4>. Will this show me nasal demons ?
+3
1 answer
First, it’s important to note that simply having two objects in a union is never undefined. What undefined is to write to one and read from the other, with one exception:
[C++11: 9.5/1]:[. : , (9.2), , ; . 9.2. -end note] [..]
, -, std::array , , , :
[C++11: 23.3.2.1/2]:- (8.5.1), :
array<T, N> a = {};, ,
N,T.
, , .
: ; .
+3