[class.prop]/(3.7) seems to be in contradiction with [class.prop]/(3.7.3). What am I missing?

Viewed 111

A class S is a standard-layout class if it:

[class.prop]/(3.7):

  • has no element of the set M(S) of types as a base class, where for any type X, M(X) is defined as follows.102 [Note: M(X) is the set of the types of all non-base-class subobjects that may be at a zero offset in X. — end note]

From the highlighted sentence above we conclude that M(S) is empty if S is a union, simply because unions don't have base classes. For me this is in contradiction with [class.prop]/(3.7.3) below.

[class.prop]/(3.7.3)

  • If X is a union type, the set M(X) is the union of all M(Ui) and the set containing all Ui , where each Ui is the type of the i th non-static data member of X.
1 Answers

I was wrong. See the answer by zygoloid here.

In attention to @GManNickG I'm reproducing below the answer by zygoloid to my issue in GitHub:

The wording is correct as-is. Consider:

struct A {};
union U { A a; };
struct B : A { U u; };

Here, B is not standard-layout because M(B) contains A and B has A as a base class. M(B) is defined as U plus M(U), and M(U) is defined as A.

So we need M(U) to be non-empty for M(B) to properly compute "the set of the types of all non-base-class subobjects that may be at a zero offset in" U, even though U itself can't have base class.

Related