The potentially ambiguous statement in Β§ 9.5 / 8

Β§9.5 / 9 of C ++ 11 Standard (emphasis mine):

A union class is a union or class that has an anonymous union as a direct member. The merged class X has a bunch of options. If X is union , the non-static data member of X , which is not an anonymous union, is a member of variant X

Is the part in bold to say that between a union class, which is either a class or a union, only if it is a union, can it have a non-static variant member that is not an anonymous union? If so, why? And what is the practical difference in the code?

In fact, I am wondering if this statement meant: "If X is a unified class ...". Then that would make sense.

In any case, this item has been pushing me over the past few days, and I want to fully understand what he is saying.

+5
source share
3 answers

No, your attempt at clarification is incorrect. Here is the same class X:

 struct X { int a; union { double b; long c; }; }; 

X::a is a non-static data member of a union type of type X that is not an anonymous union. But this is definitely NOT an option.

All non-static members of the union data are variant members. For unified classes that are not unions, only those that are nested in union subobjects are variant members.

+5
source

I feel your pain, it takes many years of cognitive damage caused by viewing standard documents in order to be able to properly disassemble such things.

only if it is a union, can it have a non-static version that is not an anonymous union?

Not really.

This is not to say that only an allied version of a union class can have a non-static blah blah blah.

What he says may have it (technically, he does not talk about it, but he refuses to refute this possibility), but only the union version will consider it as a β€œvariant element X”.

+2
source

It seems that the last publicly available project (2013-10-13) contains a clearer definition:

A union class is an association or class that has an anonymous union as a direct member. A union-like class X has many member options. If X is a union, a non-static data member X , which is not an anonymous union, is a variant member of X In addition, the non-static data member of the anonymous union, which is a member of X also a member of option X No more than one variant of a member of the union may have an initializer of brackets or equal. Example:

 union U { int x = 0; union { }; union { int z; int y = 1; // error: initialization for second variant member of U }; }; 

As for the question, the bold part actually determines what a variant member is, which should be of the union type (having union as a direct member) as a unifying class.

0
source

Source: https://habr.com/ru/post/1202295/


All Articles