|
Boost : |
Subject: Re: [boost] [operators] The Dangling Reference Polarization
From: Michael Marcin (mike.marcin_at_[hidden])
Date: 2013-05-04 15:45:15
On 5/4/2013 1:53 PM, Daniel Frey wrote:
> 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.
>
Here is another data point:
http://ideone.com/GgaZZV
And here are msvc11 results:
http://codepad.org/XFT5qVyE
based on:
http://stackoverflow.com/questions/1693005/copy-elision-on-visual-c-2010-beta-2
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk