什么是“recursive_init_error”例外? [英] What's a "recursive_init_error" exception?

查看:270
本文介绍了什么是“recursive_init_error”例外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定用计算的gotos和本地静态进行测试

  void g(){std :: cout< < 在里面 ; } 
void f(){
int z = 0;
y:z ++;
static int x =
(g(),z == 1?({goto *&& y; 0;}):0);
}

int main(){f(); std :: cout<< !; F();我想看看输出是否为init init!。但令我惊讶的是,我没有得到这个输出,而是GCC处理它,在运行时输出:$ / recursive_init_error:exception

这是什么异常?是标准异常吗? C ++ 03或C ++ 0x?感谢任何解释。

解决方案

它是由C ++ 03§6.7/ 4中所述:


...否则,这样的对象在第一次控制通过其声明时被初始化;这样的对象被认为是在其初始化完成时被初始化的。如果初始化通过抛出异常而退出,则初始化不完整,因此下一次控制进入声明时将再次尝试。如果控件在对象初始化时重新进入声明(递归),行为是未定义的。 [示例:




  int foo(int i)
{
static int s = foo(2 * i); //递归调用 - 未定义
返回i + 1;
}




- 结束示例]


GCC在这种情况下抛出异常。以下是有关该文档的一些文档






C ++ 11更新:在C ++ 11中添加了以下措辞,就在关于递归案例的文本之前:


如果控件在初始化变量时同时进入声明,并发执行将等待完成初始化。 88



88 实现不能在初始化程序的执行周围引入任何死锁。


不改变这里的问题,但是当没有递归时,会使此构造线程安全。


I decided to do a test with computed gotos and local statics

void g() { std::cout << "init "; } 
void f() { 
  int z = 0; 
  y: z++; 
  static int x = 
    (g(), z == 1 ? ({ goto *&&y; 0; }) : 0); 
}

int main() { f(); std::cout << "!"; f(); }

I wanted to see whether the output would be "init init !". But to my surprise I didn't get that output, but instead GCC handled it gracefully, outputting at runtime:

init terminated by recursive_init_error: exception

What's that exception? Is it a Standard exception? C++03 or C++0x? Thanks for any explanation.

解决方案

It's caused by what is stated in C++03 §6.7/4:

... Otherwise such an object is initialized the first time control passes through its declaration; such an object 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 re-enters the declaration (recursively) while the object is being initialized, the behavior is undefined. [Example:

int foo(int i)
{
  static int s = foo(2*i);    // recursive call – undefined
  return i+1;
}

--end example]

GCC throws an exception in that case. Here's some documentation about it.


C++11 update: The following wording was added in C++11, just before the text about the recursive case:

If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization.88

88 The implementation must not introduce any deadlock around execution of the initializer.

Doesn't change the problem here, but does make this construct thread-safe when there is no recursion.

这篇关于什么是“recursive_init_error”例外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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