以下间接递归的结果是标准定义的还是未定义的行为?
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/