Boost logo

Boost :

Subject: Re: [boost] [operators] The Dangling Reference Polarization
From: Andrew Ho (helloworld922_at_[hidden])
Date: 2013-04-25 17:32:30


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

VS2012 behaves differently. Not only is T &a = b + c + d not a compile error,
but in all cases I've tested the temporary's life is actually extended! I'll
raise questions to MS to get their input to see if they intended this, or if
it's a bug (never know which it is with MS). My tests with GCC does mirror
your observations.

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


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