Subject: Re: [boost] [operators] The Dangling Reference Polarization
From: Daniel Frey (d.frey_at_[hidden])
Date: 2013-04-25 18:03:26
On 25.04.2013, at 23:32, Andrew Ho <helloworld922_at_[hidden]> wrote:
>> d) I fail to see any valid use-case for binding the result of the expression
> to a reference. Can someone
>> please provide a convincing example of why (P1) or (P2) are needed/useful?
> Keep in mind that in the context
>> of operator+ (or any other operator in question), we already require that
> the type T is copy-/moveable and
>> that copy-elision most likely takes place if you use "T r = " instead of
> "const T& r = ".
> Found one which fails with gcc 4.7.2 (again, VS2012's trickery extends object
> lifetimes so it "works"):
> for(char c : str1 + str2 + str3)
> // ...
> The temporary returned is destructed prematurely.
Your example suggests you tested with std::string, which works for me with GCC. But I guess you are suggesting that with an operator+ returning an rvalue reference, it would fail. And indeed this is the first example which makes sense to me. It has all the properties I was looking for:
- It's common
- The binding of the result is implicit
- The code doesn't have a bad code-smell, it looks reasonable
Mist. (That's the German "Mist", not the English one ;)
Let me think about it for a while
Best regards, Daniel