1
编译器是否有可能在这种情况下识别尾递归?复杂的尾递归案例
void f(int x) {
if (x == 1) {
/* do_1... */
}
else if (x == 2) {
/* do_2... */
}
else if (x == 3) { // here, we want do_2 and do_3; the order doesn't matter
/* do_3... */
f(2); // this should be tail recursive
}
else if (x == 4) {
/* do_4... */
}
}
会放置return;
后f(2);
帮助编译器将其识别为一个尾递归的情况下?
你有没有试过看编译器的输出?您的问题的答案非常特定于编译器,包括版本,优化标志等。 –
您不应该依赖优化尾递归的C++编译器。如果正确的行为取决于不炸掉堆栈,则应该消除递归。 –
同时检查栈帧重用是否发生也是一种选择! :) – ScarletAmaranth