c++ - 间接递归,依赖静态变量

标签 c++ c++11 recursion static-initialization

以下间接递归的结果是标准定义的还是未定义的行为?

auto abc() -> int ;

auto xyz() -> int  {
    static int instance = 3 + abc();
    return instance;
}

auto abc() -> int {
    static int instance = 2 + xyz();
    return instance;
}

int main() {
    int tmp = xyz();//or abc();
}

在 VS2012 中 tmp 是 5 但我不确定标准是否保证了这一点。

最佳答案

这是未定义的行为。

[statement.decl]/4

If control re-enters the declaration recursively while the variable 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 ]

关于c++ - 间接递归,依赖静态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19599306/

相关文章:

c++ - 将两个缓冲区合并为 Win32 管道上的单个写入

c++ - 在不使用/proc/mounts、/etc/fstab、system() 或 popen() 的情况下,在 Linux 上列出 C++ 中的挂载点

c++ - 如何生成仅包含 1 到 6 数字的 4 位数字?

c++ - 为什么 `std::remove_const` 在与 `const` 一起使用时不删除引用对象的 `decltype` -ness?

c - 输出窗口卡在黑屏上

Python 递归与 for 循环

java - 如何预测递归方法的最大调用深度?

c++ - 这段 C++ CRTP 代码应该编译吗?如果可以,它应该做什么?

c++ - Boost spirit 将整个比赛作为一个字符串

c++ - C++ 类的构造函数中的线程池被杀死