gpt4 book ai didi

c++ - 具有抑制 move 构造函数/分配的类型如何仍被视为可 move 的?

转载 作者:IT老高 更新时间:2023-10-28 12:53:59 25 4
gpt4 key购买 nike

struct copyable { // and movable
copyable() = default;
copyable(copyable const&) { /*...*/ };
copyable& operator=(copyable const&) { /*...*/ return *this; }
};

由于复制构造函数和复制赋值操作函数是显式定义的,这意味着 move 构造函数和 move 赋值函数不能被编译器隐式定义,因此不允许 move 操作。

请问我上面的理解是否正确?

最佳答案

it signifies that move constructor and move assignment function cannot be implicitly defined by the compiler

是的,没错。

and hence move operation is not allowed.

不, move 操作仍然可以通过复制构造函数和复制赋值操作符执行(尽管这可能不是您所期望的),因为右值总是可以绑定(bind)到 const&

更准确地说,类 copyable 仍然是 MoveConstructibleMoveAssignable .

A class does not have to implement a move constructor to satisfy this type requirement: a copy constructor that takes a const T& argument can bind rvalue expressions.

If a MoveConstructible class implements a move constructor, it may also implement move semantics to take advantage of the fact that the value of rv after construction is unspecified.

The type does not have to implement move assignment operator in order to satisfy this type requirement: a copy assignment operator that takes its parameter by value or as a const Type&, will bind to rvalue argument.

If a MoveAssignable class implements a move assignment operator, it may also implement move semantics to take advantage of the fact that the value of rv after assignment is unspecified.

正如@Curious 所指出的,您可以声明 move 构造函数和 move 赋值运算符delete明确地使 copyable 不可 move ;然后使用右值表达式,将选择 deleteed 重载并且编译将失败。

关于c++ - 具有抑制 move 构造函数/分配的类型如何仍被视为可 move 的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44671683/

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