如果工会中只有一个成员没有默认构造函数,为什么会删除它呢? [英] Why do unions have a deleted default constructor if just one of its members doesn't have one?

查看:131
本文介绍了如果工会中只有一个成员没有默认构造函数,为什么会删除它呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

N3797 :: 9.5 / 2 [class.union] 说:


如果联合的任何非静态数据成员具有非平凡的默认
构造函数(12.1),复制构造函数(12.8),移动构造函数(12.8),
复制赋值运算符(12.8),移动赋值运算符(12.8)或
析构函数(12.4),联合的相应成员函数必须由用户提供
,否则将被隐式删除(8.4.3)对于
工会

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

我试图通过示例理解该注释:

I was trying to understand that note by example:

#include <iostream>
#include <limits>

struct A
{
    A(const A&){ std::cout << "~A()" << std::endl; } //A has no default constructor
};

union U
{
    A a;
};

U u; //error: call to implicitly-deleted default constructor of 'U'

int main()
{

}

演示

DEMO

这种行为对我来说还不太清楚。 struct A 没有隐式声明的默认构造函数,因为 12.1 / 4:[class.ctor] 说:

That behavior isn't quite clear to me. struct A doesn't have implicitly-declared default constructor, because 12.1/4: [class.ctor] says:


如果类X没有用户声明的构造函数,则不带参数的构造函数
被隐式声明为默认值(8.4 )。

If there is no user-declared constructor for class X, a constructor having no parameters is implicitly declared as defaulted (8.4).

这意味着结构A 没有不平凡的默认构造函数(根本没有默认构造函数,尤其是平凡的构造函数)。 union U 不必删除默认的构造函数。

Which means struct A doesn't have a non-trivial default constructor (There is no default constructor at all, in particular non-trivial). That's union U doesn't have to have a deleted default constructor. What's wrong?

推荐答案

相关措辞在C ++ 11 [class.ctor] p5(强调我的意思)中:

The relevant wording is in C++11 [class.ctor]p5 (emphasis mine):


X的 default 构造函数X 是类<$的构造函数c $ c> X 可以不带参数地调用。如果对于类 X 没有用户声明的构造函数,则不带参数的构造函数隐式声明为默认值(8.4)。 [...] 在以下情况下,类 X 的默认默认构造函数被定义为已删除:

A default constructor for a class X is a constructor of class X that can be called without an argument. If there is no user-declared constructor for class X, a constructor having no parameters is implicitly declared as defaulted (8.4). [...] A defaulted default constructor for class X is defined as deleted if:

[ ...]


  • X 是类联合的类,其变体成员带有非普通的默认构造函数,

  • X is a union-like class that has a variant member with a non-trivial default constructor,

[...]


  • 任何直接或虚拟基类,或没有括号或相等初始化器非静态数据成员,其类类型为 M (或其数组), M 没有默认构造函数,或应用于 M 的默认构造函数导致模棱两可,或者导致从默认默认构造函数中删除或无法访问的函数,或者

  • any direct or virtual base class, or non-static data member with no brace-or-equal-initializer, has class type M (or array thereof) and either M has no default constructor or overload resolution (13.3) as applied to M's default constructor results in an ambiguity or in a function that is deleted or inaccessible from the defaulted default constructor, or

[...]

您的类 A 没有默认构造函数,因此是 X (无论是工会还是非工会)类的默认默认构造函数(无论是隐式还是显式)在没有初始化程序的情况下插入类型为 A 的非静态数据成员会导致删除 X 的默认构造函数。它必须:编译器根本无法生成任何其他默认构造函数。

Your class A has no default constructor, so a defaulted default constructor (whether implicit or explicit) for a class X (whether union or non-union) containing a non-static data member of type A without an initialiser leads to the default constructor for X being deleted. It has to: there's simply no way for the compiler to generate any other default constructor.

关于注释中的后续问题:

As for your follow-up question in the comments:

如果不是 A not 没有默认构造函数,而是具有非平凡的默认构造函数,则在工会和非工会的班级,这也是[class.ctor] p5的一部分:这是我在前面的引用中不加任何强调的第一个要点。

If instead of A not having a default constructor, it has a non-trivial default constructor, then there is a difference between using that in a union and in a non-union class, and that is also part of [class.ctor]p5: it is the first bullet point that I included, without emphasis, in my earlier quote.

这篇关于如果工会中只有一个成员没有默认构造函数,为什么会删除它呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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