具有抑制移动 construct/assign 的类型如何仍然被认为是可移动的?
How can a type with suppressed move construct/assign still be considered moveable?
struct copyable { // and movable
copyable() = default;
copyable(copyable const&) { /*...*/ };
copyable& operator=(copyable const&) { /*...*/ return *this; }
};
由于复制构造函数和复制赋值操作函数是显式定义的,这意味着编译器不能隐式定义移动构造函数和移动赋值函数,因此不允许移动操作。
请问我上面的理解是否正确?
it signifies that move constructor and move assignment function cannot be implicitly defined by the compiler
是的,没错。
and hence move operation is not allowed.
不,移动操作仍然可以通过复制构造函数和复制赋值运算符执行(尽管这可能不是您预期的情况),因为右值总是可以绑定到 const&
.
更准确地说,class copyable
仍然是 MoveConstructible and MoveAssignable 。
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 指出的那样,您可以显式声明移动构造函数和移动赋值运算符 delete 以使 copyable
不可移动;然后与右值表达式一起使用 delete
ed 重载将被选中并且编译将失败。
struct copyable { // and movable
copyable() = default;
copyable(copyable const&) { /*...*/ };
copyable& operator=(copyable const&) { /*...*/ return *this; }
};
由于复制构造函数和复制赋值操作函数是显式定义的,这意味着编译器不能隐式定义移动构造函数和移动赋值函数,因此不允许移动操作。
请问我上面的理解是否正确?
it signifies that move constructor and move assignment function cannot be implicitly defined by the compiler
是的,没错。
and hence move operation is not allowed.
不,移动操作仍然可以通过复制构造函数和复制赋值运算符执行(尽管这可能不是您预期的情况),因为右值总是可以绑定到 const&
.
更准确地说,class copyable
仍然是 MoveConstructible and MoveAssignable 。
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 指出的那样,您可以显式声明移动构造函数和移动赋值运算符 delete 以使 copyable
不可移动;然后与右值表达式一起使用 delete
ed 重载将被选中并且编译将失败。