tail-call

Visual C++ Tail Call Optimization

强颜欢笑 提交于 2019-12-13 14:19:23
问题 According to answers to that question: Which, if any, C++ compilers do tail-recursion optimization? it seems, that compiler should do tail-recursion optimization. But I've tried proposed options and it seems that compiler can't do this optimization in case of template functions. Could it be fixed somehow? 回答1: I don't use the MS compilers, but GCC can certainly do tail-recursion optimisation for templates. Given this function: template <typename T> T f( T t ) { cout << t << endl; if ( t == 0

How to recognize what is, and what is not tail recursion?

拥有回忆 提交于 2019-11-29 06:52:54
问题 Sometimes it's simple enough (if the self call is the last statement, it's tail recursion), but there are still cases that confuse me. A professor told me that "if there's no instruction to execute after the self-call, it's tail recursion". How about these examples (disregard the fact that they don't make much sense) : a) This one should be tail recursive, seeing how the self-call is the last statement, and there's nothing left to execute after it. function foo(n) { if(n == 0) return 0; else