Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the lifetime of a default argument temporary bound to a reference parameter?

I thought references only extend the lifetime of temporaries to the lifetime of the reference itself, but the output of the following snippet seems contradictory:

#include <iostream>

struct X{ ~X(){ std::cout << "Goodbye, cruel world!\n"; } };

X const& f(X const& x = X()){
  std::cout << "Inside f()\n";
  return x;
}

void g(X const& x){
  std::cout << "Inside g()\n";
}

int main(){
  g(f());
}

Live example. Output:

Inside f()
Inside g()
Goodbye, cruel world!

So it seems the temporary is destroyed after g() is called... what gives?

like image 536
Xeo Avatar asked Sep 23 '12 17:09

Xeo


1 Answers

The standard handles this in a special case in §12.2 [class.temporary]:

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

p5 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:

  • 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.

The standard also has a handy note on full-expressions and the evaluation of their subexpressions with regards to default parameters in §1.9 [intro.execution] p11:

[ Note: The evaluation of a full-expression can include the evaluation of subexpressions that are not lexically part of the full-expression. For example, subexpressions involved in evaluating default arguments (8.3.6) are considered to be created in the expression that calls the function, not the expression that defines the default argument. —end note ]

like image 59
Xeo Avatar answered Sep 28 '22 03:09

Xeo