【发布时间】:2017-11-02 17:10:08
【问题描述】:
考虑以下示例程序:
#include <iostream>
using namespace std;
struct t
{
~t() {cout << "destroyed\n"; }
};
int main()
{
cout << "test\n";
t(), cout << "doing stuff\n";
cout << "end\n";
}
我使用 GCC 4.9.2 得到的输出是:
test
doing stuff
destroyed
end
cpp.sh 链接:http://cpp.sh/3cvm
但是根据 cppreference 关于逗号运算符:
在逗号表达式 E1, E2 中,表达式 E1 被求值,其结果被丢弃,其副作用在表达式 E2 开始求值之前完成
我希望 ~t() 在 cout << "doing stuff" 之前被调用
这是标准行为吗?如果有,在标准中是在哪里定义的?
【问题讨论】:
标签: c++ language-lawyer object-lifetime temporary-objects comma-operator