Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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; } }; 

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?

like image 655
Balan K Avatar asked Jun 21 '17 09:06

Balan K


People also ask

What is the difference between move constructor and move assignment?

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.

Are move constructors automatically generated?

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.

What is the use of move constructor in C++?

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.


1 Answers

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 deleteed overload will be selected and compile would fail.

like image 192
songyuanyao Avatar answered Sep 20 '22 00:09

songyuanyao