Considering the following concept/example class
class Test { public: explicit Test(std::string arg_string) : my_string( std::move(arg_string) ) { } Test(const Test& Copy) { this->my_string = Copy.my_string; } Test& operator=(Test Copy) { MoveImpl( std::move(Copy) ); return *this; } Test(Test&& Moved) { MoveImpl( std::forward<Test&&>(Moved) ); } Test& operator=(Test&& Moved) { MoveImpl( std::forward<Test&&>(Moved) ); return *this; } private: void MoveImpl(Test&& MoveObj) { this->my_string = std::move(MoveObj.my_string); } std::string my_string; }; The copy constructor takes a const& as usual.
The copy assignment operator is implemented in terms of the copy constructor (as ,if I remember correctly, Scott Meyers pointed out that exception safety and self assignment issues are resolved this way).
When implementing the move constructor and move assignment operator I figured that there was some "code duplication", which I "eliminated" by adding the MoveImpl(&&) private method.
My question is, since we know that the copy assignment operator gets a new copy of the object which will be cleaned up on scope end, whether it is correct/good practice to use the MoveImpl() function to implement the functionality of the copy assignment operator.
Test() = Test();).= default;it? Actually, every constructor besides the first one does an assignment, and that is not good.std::forward<Test&&>(Moved)can simply bestd::move(Moved).Test& operator=(const Test& Copy). However, I think this brings back the self-assignment and exception safety issues.MoveImplperforms a move how does the move constructor perform an assignment ?