c++ - C++11 中的 union : default constructor seems to be deleted

标签 c++ c++11 language-lawyer unions

我想了解 C++11 是如何扩展 union 的。改变的一件事是现在能够将非静态数据成员与非平凡的特殊成员函数一起使用。来自 cppreference.com

If a union contains a non-static data member with a non-trivial special member function (default constructor, copy/move constructor, copy/move assignment, or destructor), that function is deleted by default in the union and needs to be defined explicitly by the programmer. At most one data member can have a default member initializer.

我正在尝试以下代码:

struct X
{
    ~X() {};
};

union U
{
    X x;
    ~U() {};
};

int main()
{
    U s1{};  // works, probably aggregate initialization
    U s2;    // DOES NOT compile, why?
}

Live on Coliru

此处 X(用作 union 的数据成员)具有用户提供的析构函数,因此 union 的析构函数默认被删除。因此我明确提供了一个。但是,代码编译失败,出现错误

note: 'U::U()' is implicitly deleted because the default definition would be ill-formed:

如果我删除最后一行 U s2;,代码会编译。

问题这里发生了什么?为什么 U s1{}; 可以编译,而 U s2; 不能? union 的默认 ctor 是否标记为已删除(如果是,为什么?!),在第一种情况下我们只是聚合初始化?请注意,如果我提供 U(){};//不是 U() = default; 代码会编译(但如果我只提供 X 的构造函数则不会)。

编辑

深入了解标准后(N4527):

union :9.5/2 [class.union]

[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. —endnote]

这似乎是一个 gcc 错误(现已报告 here)。代码在 clang 和 gcc 4.8.2 或更早版本上编译,在 gcc4.9 和更高版本上中断(感谢 @T.C. 指出)。

编译器:g++5.3,-std=c++11 使用。

最佳答案

X 不是 pod 类型,因为它具有析构函数,因此不可平凡复制 U 也不是 pod 类型。

U s2; 尝试调用被删除的默认构造函数,所以错误

U s1 {}; 使用成员明智的初始化并且不调用任何构造函数

在与非 pod 成员的 union 中, union 的默认构造函数被删除,因为它会调用成员的默认构造函数,即编译器不知道调用哪个成员的默认构造函数

 Union XX{
   string m1; 
   vector <int> m2;
}

XX 的默认构造函数无法调用 m1 和 m2 的默认构造函数,因此将其删除

关于c++ - C++11 中的 union : default constructor seems to be deleted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34581372/

相关文章:

c++ - std::shared_ptr 或 std::unique_ptr 赋值运算符重载

c++ - C++11/1y 中的范围/切片?

c++ - 与有关多态成员的设计决策作斗争

c++ - 为什么 `polymorphic_allocator` 采用 `memory_resource` 指针而不是引用?

c++ - 为什么禁止一次打开多个命名空间?

java - 在原始类类型上忽略显式方法类型参数;编译器错误?

c++ - protected 成员是派生类中的 "not declared in this scope"

c++ - 对象存在于 C++ 中意味着什么?

c++ - 内部作用域枚举、散列函数和 unordered_set 数据成员

c++ - 用于多行字符串的 ECMAScript 正则表达式