4th bullet point in iso 12.1 p5 doesn't make sense to me

Viewed 268

Maybe I'm missing something, but IMO the 4th bullet point in iso §12.1 p5 is wrong:

X is a union and all of its variant members are of const-qualified type (or array thereof),

simply because you can't have more than one const qualified member in a union.

From §9.1 we have:

In a union, at most one of the non-static data members 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.

Edit:

This snippet doesn't compile in Ideone

union U
{
    const int i;
    const char c;
    float x;
    U(int j, char a) : i(j), c(a) {}
};

Edit1:

The following code compiles in Coliru and Ideone. But according to 12.1 p5 4th bullet point, it shouldn't as the default constructor should be deleted.

#include <iostream>
union T
{
    const int y;
    const int x;
    const float z;
    T(int j) : y(j) {}
    T() = default;
};

int main()
{
    T t;
}

Edit2:

I also don't understand why the Standard disallows this code (see note in 9.5 p2)

struct S
{
    int i;
    S() : i(1) {}
};

union T
{
    S s;
    char c;
};

int main()
{
    T t;
}

but allows this. What's the difference?

struct S
{
    int i;
    S() : i(1) {}
};

union T
{
    S s;
    char c;
    T() {}
};

int main()
{
    T t;
}
2 Answers
Related