Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When are temporaries created as part of a function call destroyed?

Tags:

Is a temporary created as part of an argument to a function call guaranteed to stay around until the called function ends, even if the temporary isn't passed directly to the function?

There's virtually no chance that was coherent, so here's an example:

class A { public:     A(int x) : x(x) {printf("Constructed A(%d)\n", x);}     ~A() {printf("Destroyed A\n");}      int x;     int* y() {return &x;} };  void foo(int* bar) {     printf("foo(): %d\n", *bar); }  int main(int argc, char** argv) {     foo(A(4).y()); } 

If A(4) were passed directly to foo it would definitely not be destroyed until after the foo call ended, but instead I'm calling a method on the temporary and losing any reference to it. I would instinctively think the temporary A would be destroyed before foo even starts, but testing with GCC 4.3.4 shows it isn't; the output is:

Constructed A(4)
foo(): 4
Destroyed A

The question is, is GCC's behavior guaranteed by the spec? Or is a compiler allowed to destroy the temporary A before the call to foo, invaliding the pointer to its member I'm using?

like image 418
Michael Mrozek Avatar asked Jun 14 '10 21:06

Michael Mrozek


People also ask

What are temporaries in c++?

In C++ temporaries are unnamed objects that compiler creates in various contexts. The typical uses include reference initialization, argument passing, evaluation of expressions (including standard type conversions), function returns, and exceptions (throw expressions).

Is temporary object created in return by reference?

Explanation: The temporary object is not created. If object is returned by reference, a particular memory location will be denoted with another name and hence same address values will be used.


2 Answers

Temporary objects exist up until the end of the full expression in which they are created.

In your example, the A object created by A(4) will exist at least until the expression ends just after the return from the call to foo().

This behavior is guaranteed by the language standard:

Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created. This is true even if that evaluation ends in throwing an exception (C++03 §12.2/3).

The lifetime of the temporary may be extended by binding a reference to it (in which case its lifetime is extended until the end of the lifetime of the reference), or by using it as an initializer in a constructor's initializer list (in which case its lifetime is extended until the object being constructed is fully constructed).

like image 56
James McNellis Avatar answered Oct 13 '22 00:10

James McNellis


§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."

IOW, you're safe -- the A object must not be destroyed until after foo returns.

like image 27
Jerry Coffin Avatar answered Oct 13 '22 00:10

Jerry Coffin