代码之家  ›  专栏  ›  技术社区  ›  tangy

4.5规则:无移动分配运算符?

  •  0
  • tangy  · 技术社区  · 7 年前

    基于流行的问题 What is the copy-and-swap idiom? :

    1. 为什么4.5的规则不包括移动分配运算符(有效地成为5.5的规则)?相反,我读过(如这里 What is the Rule of Four (and a half)? )我们要么有4.5条规则,要么有5条规则?

    2. 自从 swap 成员函数是 noexcept ,是否也应将复制分配运算符标记为相同(移动构造函数不能,因为它调用可以引发的默认构造函数)?

    dumb_array& operator=(dumb_array other) // should be noexcept?
    {
        swap(*this, other);
        return *this;
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Phil1970    7 年前

    因为它不会有用。

    点击第二个链接,然后点击第四个链接 The Rule of The Big Four (and a half) – Move Semantics and Resource Management

    仔细阅读章节 5_ .

    你会看到

    删除移动分配运算符

    实际上,移动分配操作是不必要的!

    所有的解释!

    基本上是操作员 dumb_array& operator=(dumb_array other) 将在通常使用移动分配运算符时使用。

    我尚未验证,但您也可以删除它,因为它无论如何都不会生成。