Is the following code legal (in c++11/14)?
bool foo() {
union bar { int i; bool b; };
union baz { char c; bar b; };
auto b = baz{'x'};
auto barptr = &b.b;
auto boolptr = &barptr->b;
new (boolptr) bool{true};
return b.b.b;
}
This example is silly, but I'm playing around with a variadic variant
implementation that uses nested unions instead of a char []
block for the variant members, and allowing this will make my current attempt at the copy constructor cleaner.
To break it down into two subquestions:
- Is the assignment of
boolptr
by accessing a member ofbarptr
legal even thoughb.b
is inactive? - Does the in-place construction of
boolptr
activateb.b
andb.b.b
?
References to the standard would be appreciated.