- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
下面三行注释的执行顺序有保证吗?
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()
之后。但这是由标准和其他编译器保证的吗?
最佳答案
是。
临时对象一直存在到完整表达式
完成。
[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.
和:
[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. [..]
这意味着 f()
和 second()
都应该存在,直到执行从 test()
返回评估结果后者。
关于c++ - 逗号分隔语句中返回值的生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7820429/
我正在开发一个使用多个 turtle 的滚动游戏。玩家 turtle 根据按键命令在 Y 轴上移动。当危害和好处在 X 轴上移动时,然后循环并改变 Y 轴位置。我尝试定义一个名为 colliding(
我不明白为什么他们不接受这个作为解决方案,他们说这是一个错误的答案:- #include int main(void) { int val=0; printf("Input:- \n
我正在使用基于表单的身份验证。 我有一个注销链接,如下所示: 以及对应的注销方法: public String logout() { FacesContext.getCurren
在 IIS7 应用程序池中有一个设置 Idle-time out 默认是 20 分钟,其中说: Amount of time(in minutes) a worker process will rem
我是一名优秀的程序员,十分优秀!