gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:14:24 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com