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

2010-10-04 Thread Hans Wennborg
On Mon, Oct 4, 2010 at 12:41 PM, Hans Wennborg h...@chromium.org wrote: On Mon, Oct 4, 2010 at 12:23 PM, Leandro Graciá Gil leandrogra...@chromium.org wrote: 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.

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

2010-10-04 Thread Leandro Graciá Gil
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. Maybe I'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

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

2010-10-04 Thread Hans Wennborg
On Mon, Oct 4, 2010 at 12:23 PM, Leandro Graciá Gil leandrogra...@chromium.org wrote: 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. Maybe I'm wrong, but are you completely sure about this one? I would say

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

2010-10-04 Thread Mike Marchywka
Date: Mon, 4 Oct 2010 12:23:06 +0100 From: leandrogra...@chromium.org To: le...@google.com CC: webkit-dev@lists.webkit.org Subject: Re: [webkit-dev] PSA: Don't try to hold onto temporaries with references In summary, looking at code like this B b = c

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

2010-10-04 Thread Darin Adler
The standard is clear on this. The temporary does persist for the lifetime of the reference. See https://bugs.webkit.org/show_bug.cgi?id=47055#c4, a comment I posted yesterday morning, for the reference to the appropriate section in the C++ standard. -- Darin

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

2010-10-04 Thread Peter Kasting
On Mon, Oct 4, 2010 at 4:23 AM, Leandro Graciá Gil leandrogra...@chromium.org wrote: 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. Maybe I'm wrong, but are you completely sure about this one? I would

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

2010-10-04 Thread David Levin
On Tue, Oct 5, 2010 at 3:42 AM, Peter Kasting pkast...@chromium.org wrote: On Mon, Oct 4, 2010 at 4:23 AM, Leandro Graciá Gil leandrogra...@chromium.org wrote: In summary, looking at code like this B b = c-foo(); ... b.m(); If c-foo() returns a temporary (return B();), then it is

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

2010-10-03 Thread Adam Barth
If a function returns a temporary, you probably don't want to hold onto it with a const Foo foo. The temporary will get deallocated and then you'll be left with a reference to dead memory, which is bad new bears: http://trac.webkit.org/changeset/68984 http://trac.webkit.org/changeset/68985 This

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

2010-10-03 Thread Darin Adler
On Oct 3, 2010, at 2:21 AM, Adam Barth wrote: If a function returns a temporary, you probably don't want to hold onto it with a const Foo foo. The temporary will get deallocated and then you'll be left with a reference to dead memory, which is bad new bears: I don’t understand why the

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

2010-10-03 Thread Adam Barth
On Sun, Oct 3, 2010 at 10:31 AM, Darin Adler da...@apple.com wrote: On Oct 3, 2010, at 2:21 AM, Adam Barth wrote: If a function returns a temporary, you probably don't want to hold onto it with a const Foo foo.  The temporary will get deallocated and then you'll be left with a reference to

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

2010-10-03 Thread Holger Freyther
On 10/04/2010 01:31 AM, Darin Adler 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. Some other programmers on the project challenged me when I made this

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

2010-10-03 Thread Peter Kasting
On Sun, Oct 3, 2010 at 10:31 AM, Darin Adler da...@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

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

2010-10-03 Thread David Levin
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