On 5/4/2013 5:49 AM, Daniel Frey wrote:
#ifdef PASS_BY_VALUE T operator-( T lhs, const T& rhs ) { lhs -= rhs; return lhs; } T operator-( const T& lhs, T&& rhs ) { T nrv( lhs ); nrv -= std::move( rhs ); return nrv; } T operator-( T&& lhs, T&& rhs ) { lhs -= std::move( rhs ); return std::move( lhs ); } #
One note of interest. Do not replace: T operator-( T lhs, const T& rhs ) { lhs -= rhs; return lhs; } with T operator-( T lhs, const T& rhs ) { return lhs -= rhs; } At least on msvc11. PASS_BY_VALUE 0 copies, 1 moves, 0 lvalue ops, 1 rvalue ops, V r = V()-V() 0 copies, 1 moves, 1 lvalue ops, 0 rvalue ops, V r = V()-t 0 copies, 1 moves, 1 lvalue ops, 0 rvalue ops, V r = t+V() 1 copies, 0 moves, 0 lvalue ops, 1 rvalue ops, V r = t-V() 1 copies, 1 moves, 1 lvalue ops, 0 rvalue ops, V r = t-t 1 0 1 0 (expected) 1 copies, 2 moves, 2 lvalue ops, 0 rvalue ops, V r = t-t-t 1 1 2 0 (expected) 1 copies, 2 moves, 2 lvalue ops, 0 rvalue ops, V r = t+(t+t) 1 1 2 0 (expected) 2 copies, 1 moves, 1 lvalue ops, 1 rvalue ops, V r = t-(t-t) 2 0 1 1 (expected) 2 copies, 3 moves, 2 lvalue ops, 1 rvalue ops, V r = (t-t)-(t-t) 2 1 2 1 (expected) 1 copies, 3 moves, 3 lvalue ops, 0 rvalue ops, V r = t-t-t-t 1 2 3 0 (expected) 1 copies, 4 moves, 4 lvalue ops, 0 rvalue ops, V r = t-t-t-t-t 1 3 4 0 (expected) becomes PASS_BY_VALUE 0 copies, 1 moves, 0 lvalue ops, 1 rvalue ops, V r = V()-V() 1 copies, 0 moves, 1 lvalue ops, 0 rvalue ops, V r = V()-t 0 1 1 0 (expected) 0 copies, 1 moves, 1 lvalue ops, 0 rvalue ops, V r = t+V() 1 copies, 0 moves, 0 lvalue ops, 1 rvalue ops, V r = t-V() 2 copies, 0 moves, 1 lvalue ops, 0 rvalue ops, V r = t-t 1 0 1 0 (expected) 3 copies, 0 moves, 2 lvalue ops, 0 rvalue ops, V r = t-t-t 1 1 2 0 (expected) 1 copies, 2 moves, 2 lvalue ops, 0 rvalue ops, V r = t+(t+t) 1 1 2 0 (expected) 3 copies, 0 moves, 1 lvalue ops, 1 rvalue ops, V r = t-(t-t) 2 0 1 1 (expected) 4 copies, 1 moves, 2 lvalue ops, 1 rvalue ops, V r = (t-t)-(t-t) 2 1 2 1 (expected) 4 copies, 0 moves, 3 lvalue ops, 0 rvalue ops, V r = t-t-t-t 1 2 3 0 (expected) 5 copies, 0 moves, 4 lvalue ops, 0 rvalue ops, V r = t-t-t-t-t 1 3 4 0 (expected)