当函数中的静态变量的构造函数异常终止时会发生什么? [英] What happen when the constructor of a static variable in a function terminates unusually?

查看:110
本文介绍了当函数中的静态变量的构造函数异常终止时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数可以简化为:

I have a function that can be reduced to this:

void f() {
    static MyObject o("hello");

    DoSomethingWith(o);
}

这个函数在一个C API边界调用,我使用尝试捕获在它们跨越边界之前抛出的任何异常,并拧紧:

This function is called across a C API boundary, so like a good boy, I use try to catch any exceptions that are thrown before they cross the boundary and screw things up:

void f() {
    try {
        static MyObject o("hello");

        DoSomethingWith(o);
    } catch (const MyObjectException& e) {
        Message("Constructor of o failed");
    }
}

这个函数第一次调用, message 失败的构造函数。但是,稍后,该函数再次调用,我再次收到消息。我得到的消息多次,因为 f 被调用。我使用Visual C ++,这样告诉我什么MSVC ++,但不是应该做的。

This function is called the first time and I get the message "Constructor of o failed". However, later, the function is called again, and I get the message again. I get the message as many times as f is called. I am using Visual C++ so this tells me what MSVC++ does, but not what should be done.

我的问题是,当一个 static 函数变量异常终止( throw ing, longjmp 构造函数,它的线程的终止等)?还有什么应该发生与任何其他 static 变量声明之前和之后?

My question is, what should happen when the constructor of a static function variable terminates unusually (by throwing, a longjmp out of the constructor, termination of the thread that it's in, etc)? Also what should happen with any other static variables declared before and after it? I would appreciate any relevant quotes from the standard as well.

推荐答案

第6.7节( [stmt.dcl ] )说明


所有块的零初始化具有静态存储持续时间(3.7.1)或线程存储持续时间(3.7.2)的-scope变量在执行任何其他初始化之前执行。如果适用,具有静态存储持续时间的块范围实体的常量初始化(3.6.2)在其块首次进入之前执行。允许实现在静态或线程存储持续时间在命名空间范围内静态初始化变量的相同条件下,使用静态或
线程存储持续时间来执行其他块范围变量的早期初始化。 2)。否则,这样的变量在第一次控制通过其声明时被初始化; 这样的变量在初始化完成后被视为已初始化。如果初始化通过抛出异常退出,初始化
不完整,因此下次控制进入声明时将再次尝试。
如果控制在初始化变量时同时输入声明,并发执行将等待初始化的完成。
如果控制器在变量
初始化时递归地重新输入声明,则行为是未定义的。

The zero-initialization (8.5) of all block-scope variables with static storage duration (3.7.1) or thread storage duration (3.7.2) is performed before any other initialization takes place. Constant initialization (3.6.2) of a block-scope entity with static storage duration, if applicable, is performed before its block is first entered. An implementation is permitted to perform early initialization of other block-scope variables with static or thread storage duration under the same conditions that an implementation is permitted to statically initialize a variable with static or thread storage duration in namespace scope (3.6.2). Otherwise such a variable is initialized the first time control passes through its declaration; such a variable is considered initialized upon the completion of its initialization. If the initialization exits by throwing an exception, the initialization is not complete, so it will be tried again the next time control enters the declaration. If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization. If control re-enters the declaration recursively while the variable is being initialized, the behavior is undefined.

这篇关于当函数中的静态变量的构造函数异常终止时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆