This question is based on this
Consider the following:
struct Hdr { int type; };
struct A { Hdr h; };
union Big {
Hdr h;
A a;
};
and suppose that for Big big
we know that big.a
is the active member of the union. Is the access to big.h.type
undefined behavior?
I think is indeed UB, based on:
... [ Note: One special guarantee is made in order to simplify the use of unions: If a standard-layout union contains several standard-layout structs that share a common initial sequence ([class.mem]), and if a non-static data member of an object of this standard-layout union type is active and is one of the standard-layout structs, it is permitted to inspect the common initial sequence of any of the standard-layout struct members; see [class.mem]. — end note ]
We have a standard layout union which has standard layout member structs but as understand it, the common initial sequence for Hdr
and A
is empty even though the first data member of A
is of type of Hdr
.
Am I right in that this is UB ? if not, which point of common initial sequence I misunderstood so that the access big.h.type
is defined ?
big.a.h
inspected to look for common sequence withbig.h
? – xeros