c ++ 11无限制工会的例子 [英] c++11 unrestricted unions example

查看:230
本文介绍了c ++ 11无限制工会的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了 http://www.stroustrup.com/C++11常见问题。 html#union

但我无法编译给定的示例:

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

结果:

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

问题:

我认为在非限制的联合中,如果没有手动给定默认构造函数,这是真的,如何写一个工作示例?

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?

以下一个也不会编译:

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

union X
{   
    int a;
    float b;
    Y x;
    //X(){}
};  

X x;

int main(){}

同样的错误信息:

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


推荐答案

在标准中,[class.union]在第2段(在注释中)提到:

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


联合可以有成员函数(包括构造函数和
析构函数),但不能是虚函数(10.3)。联合不应有
基类。联合不应用作基类。如果union
包含引用类型的非静态数据成员,程序是
格式错误。联合的至多一个非静态数据成员可以具有
括号或平等初始化器。 [注意:如果
联合的任何非静态数据成员有一个非平凡的默认构造函数(12.1),拷贝构造函数
(12.8),移动构造函数(12.8),拷贝赋值运算符(12.8) ,移动
赋值运算符(12.8)或析构函数(12.4),联合的相应
成员函数必须是用户提供的,否则将
隐式删除(8.4.3)for工会。 - end note]

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.

第3段甚至提供了一个例子:

Paragraph 3 even provides an example :

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

并说:


由于std :: string(21.3)声明了所有特殊成员函数的非平凡版本,U将有一个隐式删除的默认构造函数,复制/移动构造函数,复制/移动赋值运算符和析构函数。要使用U,这些成员函数中的一些或全部必须由用户提供。

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写了同样的事情:


Bjarne wrote the same thing :


如果一个union有一个用户定义的构造函数,复制或析构函数的成员,被删除;也就是说,它不能用于联合类型的对象。这是新的。

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.

但示例错误。 std :: string std :: complex 都有非默认构造函数。因此,具有这些要求的工会需要工会的构造函数。

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

这篇关于c ++ 11无限制工会的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆