逗号分隔语句中返回值的生命周期
下面三行注释的执行顺序是否有保证?
struct S
{
S() { /* called 1st */ }
~S() { /* called 3rd */ }
};
boost::shared_ptr<S> f()
{
return boost::shared_ptr<S>(new S);
}
int second() { return 0; /* called 2nd */ }
int test()
{
return (f(), second());
}
在我的编译器中,f()
返回的 shared_ptr
似乎一直持续到调用 second()
之后。但这是由标准以及其他编译器保证的吗?
Is the order of execution of the three commented lines below guaranteed?
struct S
{
S() { /* called 1st */ }
~S() { /* called 3rd */ }
};
boost::shared_ptr<S> f()
{
return boost::shared_ptr<S>(new S);
}
int second() { return 0; /* called 2nd */ }
int test()
{
return (f(), second());
}
With my compiler, the shared_ptr
returned by f()
seems to persist until after second()
is called. But is this guaranteed by the standard and thus other compilers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是。
临时变量将持续存在,直到
完整表达式
完成。并且:
这意味着
f()
和second()
都应该存在,直到执行从test()
返回并得出评估结果后者。Yes.
Temporaries persist until the completion of the
full-expression
.And:
This means that both
f()
andsecond()
should exist until execution returns fromtest()
with the result of evaluating the latter.