【发布时间】:2012-09-15 07:09:25
【问题描述】:
我认为引用只是将临时对象的生命周期延长到引用本身的生命周期,但以下 sn-p 的输出似乎是矛盾的:
#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. 输出:
Inside f()
Inside g()
Goodbye, cruel world!
所以似乎在调用g() 之后临时被销毁了......什么给出了?
【问题讨论】:
标签: c++ language-lawyer lifetime temporary-objects