12

Possible Duplicate:
C++: Life span of temporary arguments?

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?

Community
  • 1
  • 1
Vasaka
  • 1,953
  • 1
  • 19
  • 30
  • yes, answer to this question should be a part of http://stackoverflow.com/questions/4214153/lifetime-of-temporaries, 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. – Vasaka Mar 29 '11 at 19:51

2 Answers2

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.

sbi
  • 219,715
  • 46
  • 258
  • 445
  • 2
    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. – Dan Nissenbaum Mar 17 '13 at 18:09
  • 1
    @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. – dcmm88 Jan 03 '19 at 20:48
  • @dcmm88 Wow! I had to dig back in. Best I can make of my now-vague memory is this: https://stackoverflow.com/a/6937829/368896 (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. – Dan Nissenbaum Jan 05 '19 at 17:08
  • 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? – Tomingsun Nov 02 '21 at 16:35
  • @Tomingsun 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. – sbi Jan 10 '22 at 15:26
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.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644