The standard draft as of N4567 suggests that a defaulted default constructor is defined as deleted if—according to 12.1, paragraph 4:
X is a union and all of its variant members are of const-qualified type (or array thereof),
In other words, it is equivalent to saying that, if one of its variant members is not const-qualified, the above rule does not apply. My question is:
Suppose you have a union with only const members:
union Foo {
const int x;
const double y;
}
A default constructor would have to decide which of the members to initialize and become the active member. Once created you cannot change the value of any member (they are all const) thus you could also not change which is the active member. Thus such a construct would be rather useless as union.
On the other hand, if one member is not const:
union Bar {
int x;
const double y;
Bar() : y(10.0) {}
}
a default constructor could make sense, because you can still later change the active member via
Bar b;
b.x = 10;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With