C++ 中的序列点和例外情况
编译器可以在 C++ 中重新排序变量设置和 throw() 操作吗?或者,标准 C++ 14882-1998 是否允许或禁止编译器进行此转换?
对于代码:
bool funct()
{
bool succeeded = false;
bool res_throw = false;
try {
throw("it");
succeeded = true;
}
catch(...) {
res_throw = true;
}
cout << "Result of throw: " << res_throw << endl;
cout << "succeeded: " << succeeded << endl;
return succeeded;
}
输出可以是
Result of throw: true
succeeded: true
标准说:“[intro.execution]#7”:
修改对象..都是侧面 的影响,即变化 执行环境状态
在执行序列中称为序列点的某些指定点,先前评估的所有副作用都应完成,并且后续评估的副作用不会发生
throw
语句是否为序列点?
Can compiler reorder variable setting and throw()
op in C++? Or, does standard C++ 14882-1998 allows or prohibit compiler of this transform?
For code:
bool funct()
{
bool succeeded = false;
bool res_throw = false;
try {
throw("it");
succeeded = true;
}
catch(...) {
res_throw = true;
}
cout << "Result of throw: " << res_throw << endl;
cout << "succeeded: " << succeeded << endl;
return succeeded;
}
Can the output be a
Result of throw: true
succeeded: true
The Standard says: "[intro.execution]#7":
modifying an object .. are all side
effects, which are changes in the
state of the execution environmentAt certain specified points in the execution sequence called sequence points, all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have taken place
Is throw
statement a sequence point?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

是的,有一个与
throw
语句关联的序列点,因为每个 语句的末尾都有一个序列点。因此,在您的示例中,
succeeded
必须保持false
。我没有 C++98 标准,但在 C++03 标准中:
声明是最简单的“完整表达式”,但标准的措辞包括其他在技术上不属于任何声明的表达式。
Yes, there is a sequence point associated with the
throw
statement, because there is a sequence point at the end of every statement.So
succeeded
must remainfalse
in your example.I don't have the C++98 Standard, but in the C++03 Standard:
A statement is the simplest kind of "full-expression", but the Standard is worded to include other expressions that are not technically part of any statement.