问题
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