这是无限递归UB吗? [英] Is this infinite recursion UB?

查看:82
本文介绍了这是无限递归UB吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 11中,作为没有副作用的无限循环,以下程序是UB:

  int main(){
while(true){}
}

还有UB?

  void foo(){
foo
}

int main(){
foo();
}

两个程序的标准引用都是理想的。

$这是UB,因为它不是在循环语句,而是在(1.10p24):


$ b $ b


实现可以假设任何线程最终将执行以下操作之一:




  • terminate ,

  • 拨打图书馆I / O功能,

  • 访问或修改volatile对象,或

  • 执行同步操作或原子操作。


到更老的配方在C ++ 0x草案之一。 (有关讨论,请参见此问题)。



请注意,不考虑这一点,如果递归超过嵌套递归函数调用的数量的实现限制,那么行为可能很容易未定义。一直是这样。


In C++11, as an infinite loop with no side-effects, the following program is UB:

int main() {
   while (true) {}
}

Is the following also UB?

void foo() {
   foo();
}

int main() {
   foo();
}

Citations from the standard for both programs would be ideal.

解决方案

It's UB because it's not worded in terms of loops, but in terms of (1.10p24):

The implementation may assume that any thread will eventually do one of the following:

  • terminate,
  • make a call to a library I/O function,
  • access or modify a volatile object, or
  • perform a synchronization operation or an atomic operation.

This applies to both, as opposed to the more older formulation in one of the C++0x drafts. (See this question for discussions).

Note that disregarding of that, the behavior can easily be undefined if the recursion exceeds the implementation limit of the number of nested recursive function calls. That has always been the case.

这篇关于这是无限递归UB吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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