gpt4 book ai didi

c++ - 默认 move 构造函数/赋值和删除的复制构造函数/赋值

转载 作者:IT老高 更新时间:2023-10-28 22:03:11 25 4
gpt4 key购买 nike

按照标准,

If the definition of a class X does not explicitly declare a move constructor, one will be implicitly declared as defaulted if and only if

— X does not have a user-declared copy constructor,

— X does not have a user-declared copy assignment operator,

— X does not have a user-declared move assignment operator, and

— X does not have a user-declared destructor.

现在以下编译失败

# include <utility>

class Foo
{
public:
Foo() = default;
Foo(Foo const &) = delete;
};

int main()
{
Foo f;
Foo g(std::move(f)); // compilation fails here
return 0;
}

所以似乎删除的函数被认为是用户定义的,这是有道理的(它不是它的默认实现)。但是,在那种特殊情况下,如何删除复制构造函数/赋值困惑默认 move 构造函数/赋值?

我认为这个问题具有实际意义,因为手动生成和 esp。维护这样的默认函数很容易出错,同时,像 std::unique_ptr 这样的类作为类成员的使用的(正义的)增加使得不可复制的类成为更常见的野兽比从前。

最佳答案

user-declared 表示 user-provided(由用户定义),显式默认( = default) 或 explicitly deleted (= delete) 与隐式默认/删除(例如您的 move 构造函数)形成对比。

所以在你的情况下, move 构造函数被隐式删除,因为复制构造函数被显式删除(因此用户声明的)。

However, in that particular case, how would deleted copy constructor/assignment mess default move constructor/assignment?

不会,但标准并没有区分这种情况和复杂的情况。

最短的答案是,在某些情况下,隐式定义 move 构造器和显式删除的复制构造器可能是危险的,当您有 user-defined 析构函数且没有 user-defined 复制构造函数时也是如此(参见 rule of three/five/zero )。现在,您可以争辩说用户定义的析构函数不会删除复制构造函数,但这只是语言中的一个缺陷,无法删除,因为它会破坏很多旧的(坏的)程序。引用 Bjarne Stroustrup 的话:

In an ideal world, I think we would decide on “no generation” as the default and provide a really simple notation for “give me all the usual operations.” [...] Also, a “no default operations” policy leads to compile time errors (which we should have an easy way to fix), whereas a generate operations by default policy leads to problems that cannot be detected until run time.

您可以在 N3174=10-0164 中了解更多信息.

请注意,大多数人都关注 rule of three/five/zero ,在我看来你应该。通过隐式删除默认的 move-constructor,标准是“保护”您免受错误的影响,并且在某些情况下应该通过删除 copy-constructor 来保护您很长时间(参见 Bjarne 的论文)。

有兴趣的可以继续阅读:

I think this question has practical importance because manual generation and esp. maintenance of such default functions is error prone, while at the same time, the (righteous) increase of the use of classes such as std::unique_ptr as class members made non-copyable classes much more common beasts than they used to be.

将 move 构造函数标记为显式默认将解决此问题:

class Foo {
public:
Foo() = default;
Foo(Foo const &) = delete;
Foo(Foo&&) = default;
};

你会得到一个带有默认 move 构造函数的不可复制对象,在我看来,这些显式声明比隐式声明更好(例如,仅将 move 构造函数声明为 default 而不删除复制-构造函数)。

关于c++ - 默认 move 构造函数/赋值和删除的复制构造函数/赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37276413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com