我在 [class.copy.elision] 中找不到确认初始化 `T x = T();` 符合强制复制省略条件的引号
I can't find a quote in [class.copy.elision] confirming that the initialization `T x = T();` is eligible for the mandatory copy elision
cppreference.com 引用了 强制省略 copy/move 操作 的两个案例。我对第二种情况感兴趣如下:
In the initialization of an object, when the initializer expression is
a prvalue of the same class type (ignoring cv-qualification) as the
variable type.
因此,下面的初始化将强制省略复制操作:
T x = T();
事实上,当 T 的复制构造函数被删除时,此初始化在 C++14 中无法编译,但它在 C++17 中编译(参见 example),如 [=27] 中所述=].
但我在 [class.copy.elision] 中找不到支持这一点的引述。
那是因为措辞在[dcl.init]/17.6.1
Otherwise, if the destination type is a (possibly cv-qualified) class type:
- If the initializer expression is a prvalue and the cv-unqualified version of the source type is the same class as the class of the
destination, the initializer expression is used to initialize the
destination object. [ Example:
T x = T(T(T()))
; calls the T default
constructor to initialize x. — end example ]
保证复制省略涉及对值类别行为和交互方式的巧妙改变。所以它分布在标准的几个地方。您可以通过检查 original proposal
来查看它们
cppreference.com 引用了 强制省略 copy/move 操作 的两个案例。我对第二种情况感兴趣如下:
In the initialization of an object, when the initializer expression is a prvalue of the same class type (ignoring cv-qualification) as the variable type.
因此,下面的初始化将强制省略复制操作:
T x = T();
事实上,当 T 的复制构造函数被删除时,此初始化在 C++14 中无法编译,但它在 C++17 中编译(参见 example),如 [=27] 中所述=].
但我在 [class.copy.elision] 中找不到支持这一点的引述。
那是因为措辞在[dcl.init]/17.6.1
Otherwise, if the destination type is a (possibly cv-qualified) class type:
- If the initializer expression is a prvalue and the cv-unqualified version of the source type is the same class as the class of the destination, the initializer expression is used to initialize the destination object. [ Example:
T x = T(T(T()))
; calls the T default constructor to initialize x. — end example ]
保证复制省略涉及对值类别行为和交互方式的巧妙改变。所以它分布在标准的几个地方。您可以通过检查 original proposal
来查看它们