c++ - 为什么就地成员初始化在 C++11 中使用复制构造函数?

标签 c++ c++11 initialization atomic copy-constructor

我对下面的代码有点困惑:

struct A {
  std::atomic<int> a = 0;
};

这会报错:

copying member subobject of type 'std::atomic' invokes deleted constructor

但几乎相同的代码确实有效:

struct A {
  std::atomic<int> a = {0};
};

好吧,如果第一个变体需要复制构造函数,那么它必须使用 operator=()。可是等等!此运算符在没有复制构造函数的情况下完美工作:

A a;
a.a = 1;

谁能解释一下这两种就地初始化是如何在简单操作方面进行扩展的?为什么第一个需要复制构造函数?

最佳答案

所有引用均指向 N3797,C++1y 当前工作草案。 §8.5 初始化器 [dcl.init]/15 状态:

The initialization that occurs in the form

T x = a;

as well as in argument passing, function return, throwing an exception (15.1), handling an exception (15.3), and aggregate member initialization (8.5.1) is called copy-initialization. [ Note: Copy-initialization may invoke a move (12.8). —end note ]

所以声明:

std::atomic<int> a = 0;

正在执行复制初始化。根据 8.5/17:

The semantics of initializers are as follows. The destination type is the type of the object or reference being initialized and the source type is the type of the initializer expression. If the initializer is not a single (possibly parenthesized) expression, the source type is not defined.

这里的目的地类型std::atomic<int> 源类型int (即 decltype(0) )。为了确定初始化的语义,我们必须确定第 17 段中的哪一个适用:

  • If the initializer is a (non-parenthesized) braced-init-list, the object or reference is list-initialized (8.5.4).
  • If the destination type is a reference type, see 8.5.3.
  • If the destination type is an array of characters, an array of char16_t, an array of char32_t, or an array of wchar_t, and the initializer is a string literal, see 8.5.2.
  • If the initializer is (), the object is value-initialized.
  • Otherwise, if the destination type is an array, the program is ill-formed.
  • If the destination type is a (possibly cv-qualified) class type:
    • If the initialization is direct-initialization, or if it is copy-initialization where the cv-unqualified version of the source type is the same class as, or a derived class of, the class of the destination, ... [does not apply, source type is int]
    • Otherwise (i.e., for the remaining copy-initialization cases), user-defined conversion sequences that can convert from the source type to the destination type or (when a conversion function is used) to a derived class thereof are enumerated as described in 13.3.1.4, and the best one is chosen through overload resolution (13.3). If the conversion cannot be done or is ambiguous, the initialization is ill-formed. The function selected is called with the initializer expression as its argument; if the function is a constructor, the call initializes a temporary of the cv-unqualified version of the destination type. The temporary is a prvalue. The result of the call (which is the temporary for the constructor case) is then used to direct-initialize, according to the rules above, the object that is the destination of the copy-initialization. In certain cases, an implementation is permitted to eliminate the copying inherent in this direct-initialization by constructing the intermediate result directly into the object being initialized; see 12.2, 12.8.
  • ...

我们到了。初始化表达式 - 0 - 转换为 std::atomic<int>通过创建一个用 std::atomic<int>(int) 初始化的临时对象构造函数。该临时对象用于直接初始化原始 std::atomic<int>目的。我们之前忽略的另一个“(可能是 cv 限定的)类类型”项目符号现在适用:

  • 如果初始化是直接初始化,或者如果是复制初始化,其中源类型的 cv 非限定版本与目标类相同或派生类,则考虑构造函数。枚举了适用的构造函数(13.3.1.3),并通过重载决议(13.3)选择最佳构造函数。调用如此选择的构造函数来初始化对象,使用初始化表达式或 expression-list 作为其参数。如果没有构造函数适用,或者重载决议不明确,则初始化格式错误。

回想一下,新的初始值设定项是纯右值 std::atomic<int> .重载解析确定没有合适的std::atomic<int>接受单个参数的构造函数std::atomic<int>&& (std::atomic<int> 不可移动或复制)并诊断程序格式错误。

对于问题的第二部分,

std::atomic<int> a = {0};

再次按照 8.5/15 进行复制初始化。然而,这一次,8.5/17 的第一个项目符号适用:

  • If the initializer is a (non-parenthesized) braced-init-list, the object or reference is list-initialized (8.5.4).

对于list-initialization,我们必须看8.5.4/3:

List-initialization of an object or reference of type T is defined as follows:

  • If T is an aggregate, aggregate initialization is performed (8.5.1).
  • Otherwise, if the initializer list has no elements and T is a class type with a default constructor, the object is value-initialized.
  • Otherwise, if T is a specialization of std::initializer_list<E>, a prvalue initializer_list object is constructed as described below and used to initialize the object according to the rules for initialization of an object from a class of the same type (8.5).
  • Otherwise, if T is a class type, constructors are considered. The applicable constructors are enumerated and the best one is chosen through overload resolution (13.3, 13.3.1.7). If a narrowing conversion (see below) is required to convert any of the arguments, the program is ill-formed.
  • ...

std::atomic<int>是类类型,不是聚合或 initializer_list特化,因此考虑了构造函数。 std::atomic<int>::atomic(int)构造函数将被选为完美匹配并用于初始化对象。

关于c++ - 为什么就地成员初始化在 C++11 中使用复制构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21708606/

相关文章:

C++变量在push_back之后没有传递它的值

c++ - condition_variable 使用 Xcode 抛出 system_error - 使用 VStudio 没问题

c++ - map<string, int> 从 const 函数调用时抛出 std::out_of_range

python - __init__ 的用途是什么?

c++ - 如何初始化用 auto 关键字声明的循环计数器?

c++ - 在模板类中实现和调用静态方法

c++ - Win32 释放环境变量导致 Windows 断点

c++ - 堆栈或堆变量创建使用哪个顺序

c++ - std::error_code 的用例

c - 未打印指针字符串的第一个字符