Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Full-expression boundaries and lifetime of temporaries [duplicate]

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?

like image 962
Vasaka Avatar asked Mar 28 '11 13:03

Vasaka


1 Answers

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.

like image 71
sbi Avatar answered Sep 19 '22 21:09

sbi