C++ 中的序列点和例外情况

发布于 10-06 07:03 字数 779 浏览 10 评论 0原文

编译器可以在 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 environment

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

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

发布评论

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

评论(2

不如归去2024-10-13 07:03:32

是的,有一个与 throw 语句关联的序列点,因为每个 语句的末尾都有一个序列点。

因此,在您的示例中,succeeded 必须保持 false

我没有 C++98 标准,但在 C++03 标准中:

1.9p16:每个完整表达式的完成处都有一个序列点。

声明是最简单的“完整表达式”,但标准的措辞包括其他在技术上不属于任何声明的表达式。

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 remain false in your example.

I don't have the C++98 Standard, but in the C++03 Standard:

1.9p16: There is a sequence point at the completion of each full-expression.

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.

一个人练习一个人2024-10-13 07:03:32

分号是一个序列点。抛出发生在 succeeded 设置为 true 之前

编辑:澄清:succeeded 不会设置为 true

The semicolon is a sequence point. The throw happens before succeeded is set to true

EDIT: To clarify: succeeded will not be set to true

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