78

When creating a new instance of a MyClass as an argument to a function like so:

class MyClass
{
  MyClass(int a);
};    

myFunction(MyClass(42));

Does the standard make any guarantees on the timing of the destructor?

Specifically, can I assume that it is going to be called before the next statement after the call to myFunction() ?

ks1322
  • 33,961
  • 14
  • 109
  • 164
shoosh
  • 76,898
  • 55
  • 205
  • 325
  • 1
    Related: http://stackoverflow.com/questions/2298781/why-do-un-named-c-objects-destruct-before-the-scope-block-ends – GManNickG Apr 07 '10 at 17:58

4 Answers4

122

Temporary objects are destroyed at the end of the full expression they're part of.

A full expression is an expression that isn't a sub-expression of some other expression. Usually this means it ends at the ; (or ) for if, while, switch etc.) denoting the end of the statement. In your example, it's the end of the function call.

Note that you can extend the lifetime of temporaries by binding them to a const reference. Doing so extends their lifetime to the reference's lifetime:

MyClass getMyClass();

{
  const MyClass& r = getMyClass(); // full expression ends here
  ...
} // object returned by getMyClass() is destroyed here

If you don't plan to change the returned object, then this is a nice trick to save a copy constructor call (compared to MyClass obj = getMyClass();), in case return value optimization was not being applied. Unfortunately it isn't very well known. (I suppose C++11's move semantics will render it less useful, though.)

sbi
  • 219,715
  • 46
  • 258
  • 445
  • 1
    +1 nice answer. Tho i think you mean ) instead of } for if, while, switch etc? `if(f(string())) { ... }` surely won't make the string survive until `}` :) – Johannes Schaub - litb Mar 25 '10 at 12:17
  • @Johannes: No doesn't it? I thought it would. – sbi Mar 25 '10 at 15:06
  • 1
    @sbi the full expression ends at the `)`. The whole thing might be a "full statement" but not a "full expression". Same in `for(iterator i = begin(); i – Johannes Schaub - litb Mar 25 '10 at 15:18
  • 2
    You said it yourself correctly: "A full expression is an expression that isn't a sub-expression of some other expression." and in the while, if, switch cases we have a full expression between the `(` and `)` because the statement they appear in is not an expression by any means. In your interpretation, the end of `;` would not be a full-expression-end at all, because there is a larger "expression" around it if you are within a while/if or switch. I think it's easy to see that this makes no sense :) – Johannes Schaub - litb Mar 25 '10 at 15:20
  • 2
    @Johannes: I changed it. After so many years of C++ you still learn something every week. – sbi Mar 25 '10 at 19:35
  • 3
    I didn't realize you could extend the life of a temporary object using a const reference. Good to know! – Ferruccio Nov 15 '11 at 18:50
  • 5
    In C++11, an rvalue reference also extends the lifetime of a temporary. However, I would not worry about using either of these for performance reasons. I would expect any optimizing compiler to apply copy / move elision here, making the two versions compile to identical code (capture by value or bind to reference). – David Stone Jun 26 '14 at 01:28
  • Modern compilers (clang and gcc I know for sure) give you a warning if you try to `std::move` on a return value, saying it's stupid because of RVO, which it is. – Russell Greene Jun 04 '17 at 03:14
29

Everyone has rightly cited 12.2/3 or similar, which answers your question:

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

I find it amusing that over the next page in my printing of the standard, 12.2/4 says:

There are two contexts in which temporaries are destroyed at a different point than the end of the full-expression.

Neither of them applies to your example, they both relate to the use of temporaries in initializers. But it does go to show that you have to keep your wits about you when dealing with a tricky beast like the C++ standard.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
10

The standard does indeed offer guarantees - from section 12.2/5:

A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full expression containing the call

However, in your code it is not clear if the parameter is being passed by reference or by value, though at some point a copy constructor which does take a reference will be used.

  • Any sections about whether the calling function or the called function calls the destructor of the by-value parameter? Checking disassembly of VS2013 (clang or gcc unknown), the _called_ function calls the destructor before returning. It is understandable for stdcall because the callee would pop the parameters before returning, but it surprised me for cdecl (understandable to avoid code though). – Dwayne Robinson Mar 26 '15 at 18:12
3

In section 12.2, Temporary Objects, clause 3, the ANSI/ISO C standard states: "... Temporary objects are destroyed as the last step in evaluating the full-expression that (lexically) contains the point where they were created."

This is closely related to the concept of Sequence Points. When a sequence point is reached, all side-effects of expressions are guaranteed to have taken place.

Mike Mueller
  • 2,072
  • 15
  • 16