Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Potentially ambiguous statement in §9.5/8

§9.5/9 from the C++11 Standard (emphasis mine):

A union-like class is a union or a class that has an anonymous union as a direct member. A union-like class X has a set of variant members. If X is a union, a non-static data member of X that is not an anonymous union is a variant member of X.

Is the part in bold saying that between a union-like class that 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 this is so, why? And what practical difference does it make in code?

I'm actually questioning whether this statement meant to say "If X is a union-like class...". It would make full sense to me then.

Either way, this clause has been bugging me for the past few days and I wish to fully comprehend what it is stating.

like image 835
David G Avatar asked Sep 11 '14 02:09

David G


1 Answers

No, your attempted clarification is wrong. Here is a union-like class X:

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

X::a is a non-static data member of a union-like class X that is not an anonymous union. But it most definitely is NOT a variant member.

All non-static data members of a union are variant members. For union-like classes which are not unions, only those nested within union subobjects are variant members.

like image 91
Ben Voigt Avatar answered Oct 12 '22 14:10

Ben Voigt