Boost logo

Boost :

Subject: Re: [boost] [operators] The Dangling Reference Polarization
From: Daniel Frey (d.frey_at_[hidden])
Date: 2013-05-04 14:53:30


On 04.05.2013, at 19:12, Michael Marcin <mike.marcin_at_[hidden]> wrote:

> 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.

Thanks for testing, the results for MSVC are similar to the results I got with GCC and Clang and are also what I expected, as the result from lhs-=rhs is T& and hence can only lead to a copy instead of a move. If anything, you could try "return std::move( lhs -= rhs );" but from my tests and what I got from Dave's article about pass-by-value, "lhs-=rhs; return lhs;" is the "correct" version which might allow future optimizations.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk