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/