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

时光怂恿深爱的人放手 提交于 2019-12-18 04:02:38

问题


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?


回答1:


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 ]




回答2:


Interesting, +1. (I do not mean to compete with your nice self answer here). Just a side note for anyone interested. If you want a similar effect but allowing non-const you could use move semantics:

#include <iostream>

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

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

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

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

gives

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


来源:https://stackoverflow.com/questions/12554619/what-is-the-lifetime-of-a-default-argument-temporary-bound-to-a-reference-parame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!