I read http://www.stroustrup.com/C++11FAQ.html#unions
but I can not compile this example:
union U1 {
int m1;
complex<double> m2;
};
union U2 {
int m1;
string m3;
};
U1 u;
u.m2 = {1,2};
Results in:
main.cpp:85:8: error: use of deleted function 'U1::U1()'
U1 u; // ok
^
main.cpp:75:11: note: 'U1::U1()' is implicitly deleted because the default definition would be ill-formed:
union U1 {
^
main.cpp:77:25: error: union member 'U1::m2' with non-trivial 'constexpr std::complex<double>::complex(double, double)'
complex<double> m2;
^
main.cpp:86:5: error: 'u' does not name a type
u.m2 = {1,2};
^
make: *** [main.o] Error 1
Questions:
I thought that in an unlimited join, the first element would be constructed if the default manual constructor was not set. Is this true and how to write a working example?
The following file will also not compile:
class Y
{
public:
constexpr Y() {}
};
union X
{
int a;
float b;
Y x;
};
X x;
int main(){}
The same error messages:
main.cpp:112:7: error: use of deleted function 'X::X()'
X x;
^
main.cpp:104:11: note: 'X::X()' is implicitly deleted because the default definition would be ill-formed:
union X
^
main.cpp:108:11: error: union member 'X::x' with non-trivial 'constexpr Y::Y()'
Y x;
^
make: *** [main.o] Error 1
source
share