On 28.04.2013, at 17:52, Marc Glisse
Splitting this into const& and && overloads of lhs would save a move for: T r = std::move(a) + b; (passing an xvalue and not a prvalue)
Thanks Marc, it seems that we still need the usual 4 overloads on const lvalue reference and rvalue reference for the parameters and just return an rvalue in all cases. With todays compilers I fail to find a case for pass-by-value. Currently, this works best for me: T operator+( const T& lhs, const T& rhs ) { T nrv( lhs ); nrv += rhs; return nrv; } T operator+( const T& lhs, T&& rhs ) { #ifdef COMMUTATIVE rhs += lhs; return std::move( rhs ); #else T nrv( lhs ); nrv += std::move( rhs ); return nrv; #endif } T operator+( T&& lhs, const T& rhs ) { lhs += rhs; return std::move( lhs ); } T operator+( T&& lhs, T&& rhs ) { lhs += std::move( rhs ); return std::move( lhs ); } I'll write a larger test program when I have some more time which allows better comparison of different overload sets and we can then try to come up with expressions to show the benefits and the drawbacks of each technique, including if possible cases that benefit from pass-by-value. BR, Daniel