Statement that isn't a full-expression

China☆狼群 提交于 2021-02-08 21:01:09

问题


It is often heard that in C++ temporary objects get deconstructed at the end of the full-expression. A full-expression is defined to be an expression that isn't a sub-expression of some other expression. This sounds very similar to the notion of a statement to me.

So my questions are: If I append a semi-colon to a full-expression, will it always be a statement? Can one arrive at every full-expression by taking some statement with a semi-colon at the end, and removing that semi-colon? Can I assume that every temporary will live until the end of its statement?


回答1:


Here's a statement that is longer than the lifetime of a contained temporary:

if (T() == T())
  foo();

The two temporaries created by the expression in the condition are destroyed at the end of the full-expression and are no longer alive when the statement (foo();) executes.

Note that for any expression e, e; is a statement (namely an expression statement, see [stmt.expr]).



来源:https://stackoverflow.com/questions/42517747/statement-that-isnt-a-full-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!