简体   繁体   English

删除复制构造函数或复制赋值运算符是否计为“用户声明”?

[英]Does deleting a copy constructor or copy assignment operator count as “user declared”?

Per this presentation , if either the copy constructor or copy assignment operator is "user declared", then no implicit move operations will be generated. 根据此演示文稿 ,如果复制构造函数或复制赋值运算符是“用户声明的”,则不会生成隐式移动操作。 Does delete ing the copy constructor or copy assignment operator count as "user declared"? delete复制构造函数或复制赋值运算符是否为“用户声明”?

struct NoCopy {
    NoCopy(NoCopy&) = delete;
    NoCopy& operator=(const NoCopy&) = delete;
};

Will implicit move operations be generated for the NoCopy class? 是否会为NoCopy类生成隐式移动操作? Or does deleting the relevant copy operations count as "user declared" and thus inhibit implicit move generation? 或者删除相关的复制操作是否计为“用户声明”,从而禁止隐式移动生成?

If possible, I'd prefer an answer referencing the relevant parts of the standard. 如果可能的话,我更愿意参考标准的相关部分。

根据演示文稿的幻灯片14,删除的复制构造函数是“用户声明的”,因此禁止移动生成。

The term "user declared" doesn't have a formal definition in the standard. 术语“用户声明”在标准中没有正式定义。 It is meant to be the opposite of "implicitly declared" in the context of special member functions. 它与特殊成员函数的上下文中的“隐式声明”相反。 [dcl.fct.def.default]/4 could be a bit clearer about this fact, but the intention is there: [dcl.fct.def.default] / 4对于这个事实可能会更清楚一点,但目的是:

Explicitly-defaulted functions and implicitly-declared functions are collectively called defaulted functions, and the implementation shall provide implicit definitions for them (12.1 12.4, 12.8), which might mean defining them as deleted. 显式默认函数和隐式声明函数统称为默认函数,实现应为它们提供隐式定义(12.1 12.4,12.8),这可能意味着将它们定义为已删除。 A special member function is user-provided if it is user-declared and not explicitly defaulted or deleted on its first declaration. 如果特殊成员函数是用户声明的,并且在其第一个声明中未明确默认或删除,则用户提供该函数。 A user-provided explicitly-defaulted function (ie, explicitly defaulted after its first declaration) is defined at the point where it is explicitly defaulted; 用户提供的显式默认函数(即,在第一次声明后显式默认)是在明确默认的位置定义的; if such a function is implicitly defined as deleted, the program is ill-formed. 如果将这样的函数隐式定义为已删除,则该程序格式错误。

Both NoCopy(NoCopy&) = delete; NoCopy(NoCopy&) = delete; and NoCopy& operator=(const NoCopy&) = delete; NoCopy& operator=(const NoCopy&) = delete; are declarations of special member functions. 是特殊成员职能的声明。 Since you are explicitly declaring them, as opposed to allowing the compiler to declare them implicitly, they are user-declared. 由于明确声明它们,而不是允许编译器隐式声明它们,因此它们是用户声明的。 Those declarations will therefore suppress the implicit declarations of the move constructor and move assignment operator per [class.copy]/9: 因此,这些声明将禁止移动构造函数的隐式声明,并按[class.copy] / 9移动赋值运算符:

If the definition of a class X does not explicitly declare a move constructor, one will be implicitly declared as defaulted if and only if 如果类X的定义没有显式地声明一个移动构造函数,那么当且仅当一个移动构造函数被隐式声明为默认值时

X does not have a user-declared copy constructor, - X没有用户声明的复制构造函数,

X does not have a user-declared copy assignment operator, - X没有用户声明的复制赋值运算符,

X does not have a user-declared move assignment operator, - X没有用户声明的移动赋值运算符,

X does not have a user-declared destructor, and - X没有用户声明的析构函数,和

— the move constructor would not be implicitly defined as deleted. - 移动构造函数不会被隐式定义为已删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM