struct copyable { // and movable copyable() = default; copyable(copyable const&) { /*...*/ }; copyable& operator=(copyable const&) { /*...*/ return *this; } };
Since the copy constructor and copy assignment operation functions are explicitly defined, it signifies that the move constructor and move assignment function cannot be implicitly defined by the compiler and hence the move operation is not allowed.
Can you please let me know whether my above understanding is correct?
The move assignment operator is different than a move constructor because a move assignment operator is called on an existing object, while a move constructor is called on an object created by the operation. Thereafter, the other object's data is no longer valid.
If a copy constructor, copy-assignment operator, move constructor, move-assignment operator, or destructor is explicitly declared, then: No move constructor is automatically generated. No move-assignment operator is automatically generated.
Move constructor moves the resources in the heap, i.e., unlike copy constructors which copy the data of the existing object and assigning it to the new object move constructor just makes the pointer of the declared object to point to the data of temporary object and nulls out the pointer of the temporary objects.
it signifies that move constructor and move assignment function cannot be implicitly defined by the compiler
Yes that's right.
and hence move operation is not allowed.
No, move operations can still be performed via the copy constructor and copy assignment operator (though this may not be the case you expected), because an rvalue could always be bound to const&
.
More precisely, class copyable
is still 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.
and
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.
As @Curious pointed, you can declare the move constructor and move assignment operator delete explicitly to make copyable
unmovable; then using with an rvalue expression the delete
ed overload will be selected and compile would fail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With