c++ - 我在[class.copy.elision]中找不到报价,无法确认初始化 `T x = T();`是否有资格使用强制复制省略号

标签 c++ language-lawyer copy-elision

cppreference.com列举了两种情况:复制/移动操作的强制省略。我对第二种情况感兴趣,如下所示:

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),如cppreference.com中所述。

但是我在[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 ]


保证复制省略涉及对值(value)类别的行为和交互方式的巧妙改变。因此,它分布在标准中的多个位置。您可以通过检查original proposal来查看它们

关于c++ - 我在[class.copy.elision]中找不到报价,无法确认初始化 `T x = T();`是否有资格使用强制复制省略号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61920992/

相关文章:

c++ - 将同一 vector 中的元素push_back安全吗?

c - 括号是否强制(当前)C 中的求值顺序?

c++ - 自动和复制省略

Ruby:为什么 'while (p || (p=exns.shift))' 与 'while p ||= exns.shift' 不同?

c - 什么时候可以完全优化 volatile 变量?

c++ - 为什么我不能通过 std::tuple 获得保证的复制省略?

c++ - 为什么 RVO 和 NRVO 不是标准强制要求的?

c++ - 使用运算符!否定重载的 bool 谓词

c++ - 表达式 'i < 0' 始终为假

c++ - 如何使用 for 循环多次提示用户输入不同的文件名?