[webkit-dev] PSA: Don't try to hold onto temporaries with references

David Levin levin at google.com
Sun Oct 3 19:52:08 PDT 2010


Thanks Peter and Darin.

In summary, looking at code like this

 B& b = c->foo();
 ...
 b.m();

If c->foo() returns a temporary ("return B();"), then it is safe.
If c->foo() returns a reference to a member variable ("return m_b;"), then
it is up to the lifetime of of "c->m_b".

The cases that Adam changed were instances of the former.

dave


On Mon, Oct 4, 2010 at 12:36 PM, Peter Kasting <pkasting at chromium.org>wrote:

> On Sun, Oct 3, 2010 at 10:31 AM, Darin Adler <darin at apple.com> wrote:
>
>> What you say here about object lifetime is not correct. I thought the same
>> thing a year or so back. But the C++ language keeps these objects alive
>> until the end of the block.
>>
>
> Correct.  One helpful section from the standard (12.2/5 "Temporary
> objects"):
>
> "The temporary to which the reference is bound or the temporary that is the
> complete object to a subobject of which the temporary is bound persists for
> the lifetime of the reference except as specified below. A temporary bound
> to a reference member in a constructor’s ctor-initializer (12.6.2) persists
> until the constructor exits. A temporary bound to a reference parameter in a
> function call (5.2.2) persists until the completion of the full expression
> containing the call."
>
> Adam's changes will not make any functional difference.
>
> PK
>
> _______________________________________________
> webkit-dev mailing list
> webkit-dev at lists.webkit.org
> http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20101004/26724a52/attachment.html>


More information about the webkit-dev mailing list