为什么默认移动ctor并且在定义析构函数时编译器不再添加赋值?

Dar*_*ioP 6 c++ constructor default c++11

我无法理解自动添加默认ctors背后的基本原理.特别是我觉得非常尴尬,每次我只需要添加一个空的虚拟析构函数,仅此而已,我放松了移动的东西,但添加它们我松散了副本和默认的东西,所以我最终添加所有这些代码:

virtual ~SomeClass(){}           // you are the guilty!
//virtual ~SomeClass() = default // would be the same

SomeClass(SomeClass&&) = default;                 // no more auto-added
SomeClass& operator=(SomeClass&&) = default;      // no more auto-added
SomeClass(const SomeClass&) = default;            // but with the moves defined,
SomeClass& operator=(const SomeClass&) = default; // I'm now missing the copy
SomeClass(){}                                     // and the default as well
Run Code Online (Sandbox Code Playgroud)

我确信有一个理由让我的课程变得丑陋,让我想要一个邪恶的宏观,我只是想知道它感觉更舒服.

tri*_*e_r 1

看看这个。它解释了所谓的“五法则”,这本质上是标准所要求的。

通常,在大多数情况下,编译器会为复制构造函数、复制赋值、移动赋值和析构函数创建默认值。但是,如果程序员定义了其中任何一个,那么编译器就会假设用户已经在此类中封装了一些需要他/她特殊的东西,比方说。析构函数。既然程序员知道他/她将需要一个析构函数,编译器就会知道程序员知道发生了什么,并且不会为其余部分创建默认值(因为,根据编译器所做的假设,默认值可能会出错,甚至可能导致不良行为)。