C++ - Calling copy assignment from move assignment operator

Viewed 998

Take a simple class where the move assignment does essentially the same as the copy assignment plus maybe changes some things in the source object.

In such case is it a good design to call the copy-assignment from the move-assignment?

SomeClass& operator=(const SomeClass& source) {if( this != &source){ /*copy stuff*/}                    return *this;}
SomeClass& operator=(SomeClass&&      source) {if( this != &source){ *this = source; /*modify source*/} return *this;}

Is it safe to assume that all current (and future) compilers will treat source as a normal reference in *this = source, even though originally source is passed as rvalue reference (&&)? So the move assignment won't call itself recursively. Or is it better to just make a protected copy(source) method and call that from both operators?

1 Answers
Related