本文介绍了临时人员的完整表达范围和生存期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据说,临时变量是评估完整表达式的最后一步,例如

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

bar( foo().c_str() );

临时指针一直存在,直到bar返回,但是对于

temporary pointer lives until bar returns, but what for the

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

它仍然存在,直到bar返回,或者baz return表示此处已完全表达,baz返回后我检查了销毁对象的编译器,但是我可以依靠它吗?

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?

推荐答案

临时生命,直到创建它们的完整表达式的结尾. 完整表达式"是不是另一个表达式的子表达式的表达式.

Temporaries life 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.

baz(bar(...));中,bar(...)baz(...)的子表达式,而baz(...)不是任何东西的子表达式.因此,baz(...)是完整表达式,并且在返回baz(...)之后,将不删除此表达式求值过程中创建的所有临时对象.

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.

这篇关于临时人员的完整表达范围和生存期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 13:30