<分区>
这个对 What is The Rule of Three? 的回答有以下代码。请注意,除第一个构造函数外,所有构造函数的末尾都有 = default;
:
class person
{
std::string name;
int age;
public:
person(const std::string& name, int age); // Ctor
person(const person &) = default; // 1/5: Copy Ctor
person(person &&) noexcept = default; // 4/5: Move Ctor
person& operator=(const person &) = default; // 2/5: Copy Assignment
person& operator=(person &&) noexcept = default; // 5/5: Move Assignment
~person() noexcept = default; // 3/5: Dtor
};
虽然我以前见过很多次,但我不明白什么时候可以使用 = default
,或者为什么。在我看来,如果您想要 C++ 提供的 default
构造函数或赋值运算符,您可以只删除该声明(以及任何关联的定义),不是吗?
目的是明确禁止给定类型的任何其他构造函数吗?例如:也许这个
person& operator=(const person &) = default; // 2/5: Copy Assignment
位于类定义的顶部只会让类的任何读者或用户清楚地知道不存在或不可能存在其他手动/显式复制赋值运算符?
我在这里需要一些帮助来理解:
- 机制/它的作用是什么(即:在类中的构造函数或函数定义之后使用
= default
)?- 它也适用于第一个构造函数吗?我在示例中没有看到它:
person(const std::string& name, int age);
(另外:这个构造函数有名字吗?也许是“通用构造函数”?
- 它也适用于第一个构造函数吗?我在示例中没有看到它:
- 何时使用
- 为什么要用它
我对 C++ 的更高级功能还很陌生。
更新
以下是来自 Scott Meyers: "A Concern about the Rule of Zero" 的一些额外见解(重点添加):
(如果这个问题没有结束,我可能会将其作为答案)。
The addition of the destructor has the side effect of disabling generation of the move functions, but because Widget is copyable, all the code that used to generate moves will now generate copies. In other words, adding a destructor to the class has caused presumably-efficient moves to be silently replaced with presumably-less-efficient copies. That strikes me as the kind of thing that (1) is likely to surprise people and (2) could really complicate debugging.
I'm inclined to recommend that a better way to rely on the compiler-generated copy and move functions is to expressly say that they do the right thing--to define them via
=default
:class Widget { public: Widget(const Widget&) = default; Widget(Widget&&) = default; Widget& operator=(const Widget&) = default; Widget& operator=(Widget&&) = default; ... };
With this approach, the spirit of the Rule of Zero remains: classes that don't manage resources should be designed so that the compiler-generated functions for copying, moving, and destruction do the right things. But instead of expressing this by not declaring those functions, it's expressed by declaring them explicitly and equally explicitly opting in to the compiler-generated implementations.
What do you think? Does this advice make sense? Should the Rule of Zero perhaps be the Rule of the Five defaults?
Scott 的博文到此结束。然后,在他的帖子下方的评论中,他说:
I think "The Rule of All or Nothing" would be a pretty good rule.
对被标记为重复/请求重新打开此问题的响应:
我觉得问题标记为 "already has an answer" here没有涵盖我所问问题的细微差别,我的问题应该重新开放,以允许其他人就我的问题的具体细节提供更多答案和见解。我看过它的答案。它的问题集中在显式 default
通用构造函数上,但我的问题集中在显式 default
一个或多个“Rule of 5”构造函数/赋值运算符,以及如何这会影响其他人的互动和 react 。我现在投票重新提出我的问题。
沙盒:https://godbolt.org/z/vWMsd3