规则 4.5:没有移动赋值运算符?

Rule of 4.5: No move assignment operator?

基于热门问题What is the copy-and-swap idiom?

  1. 为什么4.5的规则不包括移动赋值运算符(有效地成为5.5的规则)?相反,我读过(例如这里 )我们有 4.5 或 5 的规则?

  2. 因为 swap 成员函数是 noexcept,复制赋值运算符不应该也被标记为相同(移动构造函数不能因为它调用默认构造函数可以扔)?

dumb_array& operator=(dumb_array other) // should be noexcept?
{
    swap(*this, other);
    return *this;
}

因为没有用。

单击第二个 link,然后单击 link The Rule of The Big Four (and a half) – Move Semantics and Resource Management

仔细阅读第 5 节 – 移动分配

你会看到

Eliminating the move assignment operator

In reality is the move assignment operator is unnecessary!

所有的解释!

本质上,运算符 dumb_array& operator=(dumb_array other) 将在您通常使用移动赋值运算符时使用。

我还没有验证过,但你也可以删除它,因为它无论如何都不会生成。