I have this piece of code:
class test {
private:
union {
double x;
std::vector<double> y;
} amIValid;
};
I wonder if the union instance amIValid
is valid?
I have this piece of code:
class test {
private:
union {
double x;
std::vector<double> y;
} amIValid;
};
I wonder if the union instance amIValid
is valid?
An unnamed union can be instantiated in C++:
union { int i; double d; } my_thing;
my_thing.i = 3;
// etc.
An anonymous union is an unnamed union that is not instantiated (scroll down). You can access its members directly:
union { int i; double d; };
i = 3;
// etc.
So the answer to the question in the title is that an anonymous union cannot be instantiated because instantiating it means that it is not an anonymous union.