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?