union A{
int a;
int b;
};
int main(){
A u = {.a = 0};
int r = u.b; // #1 Is this UB?
}
[class.union] says
In a union, a non-static data member is active if its name refers to an object whose lifetime has begun and has not ended ([basic.life]). At most one of the non-static data members of an object of union type can be active at any time, that is, the value of at most one of the non-static data members can be stored in a union at any time.
In this example, only A::a
is active, then [basic.life] p7 says
The program has undefined behavior if:
- the glvalue is used to access the object, or
#1
tries to access the object whose lifetime has not begun. Does this access cause UB, if it is, is this requirement too restrictive?
BTW, Does the C standard impose the same requirement in this example? Or, Does C have a looser requirement in this case?
Update
In C standard, I find a note, which says
If the member used to read the contents of a union object is not the same as the member last used to store a value in the object the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called type punning). This might be a non-value representation.
This means it is permitted in C. However, https://eel.is/c++draft/diff.iso
Subclause [diff.iso] lists the differences between C++ and ISO C, in addition to those listed above, by the chapters of this document.
does not point out the difference.