<div class="gmail_quote">On Mon, Oct 4, 2010 at 4:23 AM, Leandro Graciá Gil <span dir="ltr">&lt;<a href="mailto:leandrogracia@chromium.org">leandrogracia@chromium.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204, 204, 204);padding-left:1ex"><div>In summary, looking at code like this<div><br></div>
<blockquote style="margin:0pt 0pt 0pt 40px;border:medium none;padding:0px">
<div><font face="&#39;courier new&#39;, monospace"> B&amp; b = c-&gt;foo();</font></div>
<div><font face="&#39;courier new&#39;, monospace"> ...</font></div><div><font face="&#39;courier new&#39;, monospace"> b.m();</font></div><div><font face="&#39;courier new&#39;, monospace"><br>
</font></div></blockquote><div>If c-&gt;foo() returns a temporary (&quot;return B();&quot;), then it is safe.<br></div></div></blockquote></div><br></div>Maybe I&#39;m wrong, but are you completely sure about this one? I would say that the temporary object created in return B() will cease to exist as soon as it returns (just after the constructor finishes).</blockquote>
<div><br></div><div>foo() is returning a temp by value.  On the caller side, that value is copied to a (hidden) temp whose lifetime is the same as the lifetime of |b|, and then |b| is set to be a reference to that temp.</div>
<div><br></div><div>By contrast, if foo were returning a temp by reference, then the reference would be invalid on return because the (foo()-scoped) temp it referred to would be destroyed when foo() exited.</div><div><br>
</div><div>PK</div></div>