https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61769

--- Comment #8 from theemathas at hotmail dot com ---
(In reply to Jonathan Wakely from comment #7)
> If you change it to:
> 
>       const int& aa = std::abs(a);
>       const int& foo=std::max(aa, aa);
> 
> You'll see that the problem goes away because the value returned by std::abs
> gets its lifetime extended, then std::max returns a reference to the same
> object, which has not gone out of scope.
> 
> Alternatively, avoid the pointless reference:
> 
>       int foo = std::max(std::abs(a), std::abs(a));

OK... I think I understand now.
By the way, I managed to find the exact text in the standard that states that
the code is invalid:

http://stackoverflow.com/questions/2784262/does-a-const-reference-prolong-the-life-of-a-temporary

"The lifetime extension is not transitive through a function argument. §12.2/5
[class.temporary]: '...A temporary bound to a reference parameter in a function
call (§5.2.2 [expr.call]) persists until the completion of the full expression
containing the call.' "

Thank you for explaining this, though.

Reply via email to