I don't think it could get extended either.<br><br>I must admit I don't know much about compilers, but naively I would say in order to implement this lifetime extension that the standard mandates, I would examine the signature of get_value(), which returns an int, and allocate enough space for an int on the stack (instead of a const reference) while treating it a const reference to an int for the purpose of compilation (it cannot be modified). That's the only trick I can think of short of allocating this on the heap... and this would be a performance killer.<br>
<br>However the caller of get_reference_no_warning() or get_reference_no_warning_2() expects a const reference, and thus is unlikely to prepare the stack for a copy instead (just in case). While it could possibly be done with an inline function, it would get hairy as soon as the function is defined in another translation unit.<br>
<br>Therefore it seems that the proper thing to do would be to diagnose the issue (in both get_reference_no_warning cases) and let the developer fix them. I surmise it should be possible to detect them (since VC++ already detects the first case), but then, as I said, I am very naive about compilers yet.<br>
<br>I hope to look at this further during the week-end, guess it would be as good a way as any to try and understand how Clang work<br><br>For reference I fixed such an issue today in real code (compiled with gcc 3.4.2), and it had gone undiagnosed too (and generated some mess because it was a string instead of an int...).<br>
<br>- Matthieu<br><br><div class="gmail_quote">2010/9/15 Eli Friedman <span dir="ltr"><<a href="mailto:eli.friedman@gmail.com">eli.friedman@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div class="im">On Wed, Sep 15, 2010 at 11:32 AM, Ted Kremenek <<a href="mailto:kremenek@apple.com">kremenek@apple.com</a>> wrote:<br>
> I'm not certain this is a bug. The 'const int&' causes the lifetime of the object returned from get_value() to be extended to its last use. Does this extend to the caller of get_reference_no_warning_2() since that also returns a const reference?<br>
<br>
</div>In get_reference_no_warning_2, w2 is bound to a temporary which is<br>
live for the lifetime of w2; returning a reference to the temporary<br>
doesn't extend its lifetime.<br>
<font color="#888888"><br>
-Eli<br>
</font></blockquote></div><br>