4

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?

levzettelin
  • 2,600
  • 19
  • 32

1 Answers1

3

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

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Hmm, interesting. But what about the first two questions? Are there examples that don't involve any kind of *compound-statement*? – levzettelin Feb 28 '17 at 20:12
  • @TobiasBrüll: Updated. – Kerrek SB Feb 28 '17 at 20:15
  • Aww, you just removed the curly braces! Although, this may be technically correct, that's not what I meant. :) – levzettelin Feb 28 '17 at 20:19
  • 1
    @TobiasBrüll: Please ask what you mean to ask :-) – Kerrek SB Feb 28 '17 at 20:23
  • @KerrekSB Can you relate anything to the other two questions then? If I append a semi-colon to a full-expression, will it... – levzettelin Feb 28 '17 at 21:06
  • @TobiasBrüll: The question is a bit weird. If you literally just appended a semicolon to a full-expression, you'd probably get a syntax error. But indeed for any expression `e`, `e;` is a statement by [stmt.expr]. – Kerrek SB Feb 28 '17 at 21:08