逗号分隔语句中返回值的生命周期

发布于 2024-12-10 20:55:58 字数 411 浏览 0 评论 0原文

下面三行注释的执行顺序是否有保证?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉态萌生 2024-12-17 20:55:58

临时变量将持续存在,直到完整表达式完成。

[n3290: 12.2/3]: 当实现引入临时
具有非平凡构造函数的类的对象(12.1,
12.8),它应确保为临时对象调用构造函数。同样,析构函数应被临时调用
一个不平凡的析构函数(12.4)。临时对象被销毁为
评估完整表达式 (1.9) 的最后一步(词法上)
包含它们的创建点。即使这样也是如此
评估以抛出异常结束。 价值计算和
销毁临时对象的副作用仅与
完整表达式,不带任何特定子表达式。

并且:

[n3290: 1.9/10]: 完整表达式是不是一个表达式
另一个表达式的子表达式。
如果语言构造是
定义为产生函数的隐式调用,使用
语言构造被认为是用于以下目的的表达式
这个定义。对在末尾生成的析构函数的调用
临时对象以外的对象的生命周期是隐式的
充分表达。应用于表达式结果的转换
为了满足语言结构的要求,其中
表达式出现也被认为是
充分表达。 [..]

这意味着 f()second() 都应该存在,直到执行从 test() 返回并得出评估结果后者。

Yes.

Temporaries persist until the completion of the full-expression.

[n3290: 12.2/3]: When an implementation introduces a temporary
object of a class that has a non-trivial constructor (12.1,
12.8), it shall ensure that a constructor is called for the temporary object. Similarly, the destructor shall be called for a temporary with
a non-trivial destructor (12.4). Temporary objects are destroyed as
the last step in evaluating the full-expression (1.9) that (lexically)
contains the point where they were created. This is true even if that
evaluation ends in throwing an exception. The value computations and
side effects of destroying a temporary object are associated only with
the full-expression, not with any specific subexpression.

And:

[n3290: 1.9/10]: A full-expression is an expression that is not a
subexpression of another expression.
If a language construct is
defined to produce an implicit call of a function, a use of the
language construct is considered to be an expression for the purposes
of this definition. A call to a destructor generated at the end of the
lifetime of an object other than a temporary object is an implicit
full-expression. Conversions applied to the result of an expression in
order to satisfy the requirements of the language construct in which
the expression appears are also considered to be part of the
full-expression. [..]

This means that both f() and second() should exist until execution returns from test() with the result of evaluating the latter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文