生成的复制和移动运算符?

Generated copy and move operators?

目前我正在阅读 Effective Modern C++ 这本书 Scott Meyers,现在我在:Item 17: 了解特殊成员函数的生成。

我的误解来自以下部分(理由):

这两个复制操作是独立的:声明一个不会阻止编译器生成另一个。因此,如果您声明了一个复制构造函数,但没有复制赋值运算符,然后编写需要复制赋值的代码,编译器将为您生成复制赋值运算符。同样,如果你声明了一个复制赋值运算符,但没有复制构造函数,而你的代码需要复制构造,编译器将为你生成复制构造函数。在 C++98 中是这样,在 C++11 中也是如此。

这两个移动操作不是独立的。如果您声明其中一个,则会阻止编译器生成另一个。 基本原理 是,如果你为你的 class 声明一个移动构造函数,你就表明移动构造的实现方式与默认的有所不同编译器将生成的成员移动。如果成员移动构造有问题,成员移动分配也可能有问题。所以声明移动构造函数会阻止生成移动赋值运算符,声明移动赋值运算符会阻止编译器生成移动构造函数。

我认为基本原理部分也可以应用于 复制构造函数 复制赋值运算符 对,不是吗?因此,如果我声明一个复制构造函数,我会用它表明默认的成员复制对我来说是不够的。如果我这样说,复制赋值运算符可能也应该由用户定义。

我认为这是一本很棒的书,但目前我还不清楚其中的原理。请帮我解释一下。谢谢。

I think the rationale part could be applied to the copy constructor and copy assignment operator pair also, couldn't it ?

当然可以。该标准与您一致。在 [class.copy]:

If the class definition does not explicitly declare a copy constructor, a non-explicit one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted; otherwise, it is defined as defaulted (8.4). The latter case is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor.

并且:

If the class definition does not explicitly declare a copy assignment operator, one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy assignment operator is defined as deleted; otherwise, it is defined as defaulted (8.4). The latter case is deprecated if the class has a user-declared copy constructor or a user-declared destructor.

当然,如果有问题的 "latter cases" 被立即取消,可能有很多现有代码会中断,这就是为什么标准首先弃用并且只会在任意距离删除它们的原因点在未来。

话又说回来,即使是长期弃用并已被删除的功能也有一种长期存在的方式,它们不受欢迎。喜欢