gpt4 book ai didi

c++ - 如果我默认复制构造函数,是否会生成 move 构造函数和 move 赋值?

转载 作者:太空宇宙 更新时间:2023-11-04 15:42:22 24 4
gpt4 key购买 nike

我对如何最好地定义可复制但不可 move 的类感到有点困惑。在我看来,删除 move 构造函数是个坏主意,因为那样我就无法从临时构造函数构造。另一方面,我不希望编译器为我隐式生成一个可能错误或可能导致意外行为的 move 构造函数。我的问题是是否允许编译器为以下类隐式生成 move 构造函数:

class C {
public:
int i_;
C(const C&) = default;
}

最佳答案

[class.copy]/9 在 CWG 1402 之后:

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.

关于c++ - 如果我默认复制构造函数,是否会生成 move 构造函数和 move 赋值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21210310/

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