Boost logo

Boost :

Subject: Re: [boost] [operators] The Dangling Reference Polarization
From: Dave Abrahams (dave_at_[hidden])
Date: 2013-05-04 02:52:25


on Thu Apr 25 2013, Daniel Frey <d.frey-AT-gmx.de> wrote:

> I see some good discussion going on, thanks to everyone participating!
> Rest assured that I value both side's contributions, it is important
> for me (and those agreeing with my perspective) to be able to defend
> this position and also to keep looking for improvements. If our side
> can not be defended, it's probably not good enough :)
>
> That said, here's what I hope can serve as a first summary, let's first look at it from a technical perspective:
>
> If an operator returns an rvalue reference (T&&), it is safe for all
> use-cases except binding the result to an rvalue- or
> const-lvalue-reference:
>
> T r = a + b + c; // no problem
> const T r = a + b + c; // no problem
>
> T& r = a + b + c; // compile-time error
>
> const T& r = a + b + c; // problem (P1)
> T&& r = a + b + c; // problem (P2)
>
> It seems that this is consistent behavior across at least GCC, Clang
> and VC++. If a compiler shows a different behavior, its likely a
> compiler-bug.

I believe Howard and I basically came to the same conclusion about
returning rvalue references: Just Don't Do It. Any potential speedups
are vastly outweighed by the safety costs. Return by value; it will
move anyway :-)

-- 
Dave Abrahams

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