Tobias Brüll Tobias Brüll -4 years ago 92
C++ Question

Statement that isn't a full-expression

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?

Answer Source

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]).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download