Full-expression boundaries and lifetime of temporaries [duplicate]
Asked Answered
T

2

13

Possible Duplicate:
What is the lifetime of temporary function arguments?
When are temporary objects destroyed?

It is said that temporary variables are destroyed as the last step in evaluating the full-expression, e.g.

bar( foo().c_str() );

temporary pointer lives until bar returns, but what for the

baz( bar( foo().c_str() ) );

is it still lives until bar returns, or baz return means full-expression end here, compilers I checked destruct objects after baz returns, but can I rely on that?

Tourmaline answered 28/3, 2011 at 13:34 Comment(1)
yes, answer to this question should be a part of https://mcmap.net/q/45713/-lifetime-of-temporaries-duplicate, I asked a new one because I do not have rights to post comments there and I was interested in a specific detail which is not covered there.Tourmaline
C
17

Temporaries live until the end of the full expression in which they are created. A "full expression" is an expression that's not a sub-expression of another expression.

In baz(bar(...));, bar(...) is a subexpression of baz(...), while baz(...) is not a subexpression of anything. Therefore, baz(...) is the full expression, and all temporaries created during the evaluation of this expression will not be deleted until after baz(...) returned.

Creative answered 19/5, 2011 at 13:8 Comment(5)
Note, though, that constructors are a special case (I think - 90% sure), so that in baz( X(foo().c_str() ) );, where X is a class and the argument to baz is a call to X's constructor, the lifetime of the c_string will end when the constructor exits.Pul
@DanNissenbaum are you saying that if you stick that X in there, that the lifetime of the temporary returned by foo() will end after the constructor of X returns, but BEFORE the call to baz? I tried to read the relevant section in the standard (location keeps changing depending on which version of the draft you look at), and based on my interpretation the standard doesn't suggest this.Barbey
@Barbey Wow! I had to dig back in. Best I can make of my now-vague memory is this: https://mcmap.net/q/409429/-why-lifetime-of-temporary-doesn-39-t-extend-till-lifetime-of-enclosing-object (see highlight of §12.2/4) - which I think comes from a draft of the C++11 standard prior to the official standard being released. The official C++11 standard changed the wording, and in the C++17 standard §15.2 the wording is significantly more narrow, so I doubt it applies in the current example (except maybe in the highly special case noted in the standard), and maybe my "90% certainty" should have been more like "10%" even then.Pul
I have a similar line WriteDeviceBlock2(_bstr_t(block).GetBSTR(), 1, &value_, &iState); where _bstr_t is a class. According to your description, can I say that the temporal variable _bstr_t(block) lives until WriteDeviceBlock2 returns?Azotize
@Azotize Yes. The full expression in your case is the call to WriteDeviceBlock2(). Sub-expressions are the creation of the _bstr_t object, the call to its member, and the invocations of the adress-of operators on the last two arguments.Creative
B
3

As the name suggests, the full-expression is all of the expression, including the call to baz(), and so the temporary will live until the call to baz() returns.

Bagpipes answered 28/3, 2011 at 13:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.