
Niels Dekker - mail address until 2008-12-31 wrote:
David Abrahams wrote:
In the case of copy-elision for by-value arguments and return values, the compiler is explicitly allowed to _assume_ there is no semantic difference between the original rvalue and its copy. That's low-hanging fruit for a compiler writer, that pays huge dividends.
Basically it was concluded that /if/ an assignment operator is implemented by means of copy-and-swap, it should preferably be done as follows:
T& operator=(T arg) { arg.swap(*this); return *this; }
And preferably /not/ as follows:
T& operator=(const T & arg) { T(arg).swap(*this); return *this; }
One downside is that self-assignment is made suboptimal. My typical operator= implementation is: T& operator= (T const& that) { if (this != &that) T(that).swap(*this); return *this; } Not sure, however, if it is critical enough.