c++11 unrestricted unions example

北慕城南 提交于 2019-12-07 10:09:00

问题


I read http://www.stroustrup.com/C++11FAQ.html#unions

but I can't compile the given example:

union U1 {
    int m1;
    complex<double> m2; // ok
};

union U2 {
    int m1;
    string m3;  // ok
};

U1 u;       // ok
u.m2 = {1,2};   // ok: assign to the complex member

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; // ok
                         ^   
main.cpp:86:5: error: 'u' does not name a type
     u.m2 = {1,2};   // ok: assign to the complex member
     ^   
make: *** [main.o] Error 1

Questions:

I thought that in a nonrestricted union the first element will be constructed if no default constructor is manually given. Is this true and how to write a working example?

The following one will also not compile:

class Y
{   
    public:
        constexpr Y() {}
};  

union X
{   
    int a;
    float b;
    Y x;
    //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

回答1:


In the standard, [class.union] mentions in paragraph 2 (in the notes) tells this :

A union can have member functions (including constructors and destructors), but not virtual (10.3) functions. A union shall not have base classes. A union shall not be used as a base class. If a union contains a non-static data member of reference type the program is ill-formed. At most one non-static data member of a union may have a brace-or-equal-initializer. [ Note: If any non-static data member of a union has a non-trivial default constructor (12.1), copy constructor (12.8), move constructor (12.8), copy assignment operator (12.8), move assignment operator (12.8), or destructor (12.4), the corresponding member function of the union must be user-provided or it will be implicitly deleted (8.4.3) for the union. — end note ]

Since your class has not-default constructor, the compilation fails.

Paragraph 3 even provides an example :

union U {
int i;
float f;
std::strings;
};

and says :

Since std::string (21.3) declares non-trivial versions of all of the special member functions, U will have an implicitly deleted default constructor, copy/move constructor, copy/move assignment operator, and destructor. To use U, some or all of these member functions must be user-provided.


Bjarne wrote the same thing :

If a union has a member with a user-defined constructor, copy, or destructor then that special function is deleted; that is, it cannot be used for an object of the union type. This is new.

but wrong examples. Both std::string and std::complex have non-default constructors. Therefore, unions with those require union's constructor.




回答2:


The problem is the presence of m2 in the union. Because complex has a "user-defined" constructor, the constructor for the union that contains it is deleted.

"If a union has a member with a user-defined constructor, a copy operation, a move operation, or a destructor, then that special function is deleted for that union; that is, it cannot be used for an object of the union type.", The C++ Programming Language, p. 215.

union U { int m1;
complex<double> m2; // complex has a constructor
string m3; // string has a constructor (maintaining a serious invariant) 
};

"It is fortunate that U won’t compile." (same reference)



来源:https://stackoverflow.com/questions/22780009/c11-unrestricted-unions-example

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!