1

Suppose we have some struct, A, with mathematical operators overloaded, like +,-,*,/ etc. If we then have an equation

A a1, a2, a3, a4, a5;
A a6 = a1*a2 + a3/a4 + 15;

There are intermediates created; for example the result of a1*a2 and the result of a3/a4 (which are then added together). My question is, when do these intermediates get destructed?

Compiling on gcc 4.2.1 the intermediates appear to get destructed after a6 is created. Does this happen for all compilers? Is there something in the standard about this?

Thanks

user2020792
  • 239
  • 1
  • 11
  • 3
    Temporaries are destructed *after* the full expression, per the standard. I'll let someone who can quote the standard leave an answer. – Mark Ransom Apr 30 '13 at 02:13
  • Answers: loosely defined, no, yes - My guess is anytime before the end of block is legal. – brian beuning Apr 30 '13 at 02:18
  • It seems none of our standards junkies are around tonight. Here's a link that might shed some light: http://stackoverflow.com/questions/2506793/c-life-span-of-temporary-arguments – Mark Ransom Apr 30 '13 at 03:37

2 Answers2

2

I only have access to a draft version of the standard, but from 12.2.3:

Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created.

With the definition of a full expression, from 1.9.10:

A full-expression is an expression that is not a subexpression of another expression.

This is true except for two special cases (12.2.4 and 12.2.5):

There are two contexts in which temporaries are destroyed at a different point than the end of the full- expression. The first context is when a default constructor is called to initialize an element of an array. If the constructor has one or more default arguments, the destruction of every temporary created in a default argument expression is sequenced before the construction of the next array element, if any.

The second context is when a reference is bound to a temporary. The temporary to which the reference is bound or the temporary that is the complete object of a subobject to which the reference is bound persists for the lifetime of the reference except ...

I've left out the exposition of the second special case, as it does not apply here - your example is encompassed by 12.2.3.

Yuushi
  • 25,132
  • 7
  • 63
  • 81
-1

I am not sure of the standards but you can check when they are destroyed by a well designed destructor function for the said class.
The general rule of thumb in any compiler is to destroy the object when all the references to that object are removed i,e when no other object refers to this object.

csurfer
  • 296
  • 1
  • 7
  • *The general rule of thumb in any compiler is to destroy the object when all the references to that object are removed i,e when no other object refers to this object.* No, that is for garbage collected/reference counted languages. Destructors in C++ are called when a variable goes out of scope (for automatic variables) or when `delete` or `delete[]` are explicitly called (for `new`'d values). – Yuushi Apr 30 '13 at 04:09