Which, if any, C++ compilers do tail

It seems to me that it would work perfectly well to do tail-recursion optimization in both C and C++, yet while debugging I never seem to see a frame stack that indicates this optimization. That is kind of good, because the stack tells me how deep the recursion is. However, the optimization would be kind of nice as well.

Do any C++ compilers do this optimization? Why? Why not?

How do I go about telling the compiler to do it?

  • For MSVC: /O2 or /Ox
  • For GCC: -O2 or -O3
  • How about checking if the compiler has done this in a certain case?

  • For MSVC, enable PDB output to be able to trace the code, then inspect the code
  • For GCC..?
  • I'd still take suggestions for how to determine if a certain function is optimized like this by the compiler (even though I find it reassuring that Konrad tells me to assume it)

    It is always possible to check if the compiler does this at all by making an infinite recursion and checking if it results in an infinite loop or a stack overflow (I did this with GCC and found out that -O2 is sufficient), but I want to be able to check a certain function that I know will terminate anyway. I'd love to have an easy way of checking this :)


    After some testing, I discovered that destructors ruin the possibility of making this optimization. It can sometimes be worth it to change the scoping of certain variables and temporaries to make sure they go out of scope before the return-statement starts.

    If any destructor needs to be run after the tail-call, the tail-call optimization can not be done.


    Both the current version of VC++ and GCC do tail call optimizations fairly well and even for mutually recursive calls. I bet the Intel compiler does, too.

    Letting the compiler do the optimization is straightforward: Just switch on optimization for speed. But you do that anyway, right? ;-)

  • For MSVC, use /O2 or /Ox.
  • For GCC, use -O3
  • The easiest way to check if the compiler did the optimization (that I know of) is perform a call that would otherwise result in a stack overflow – or looking at the assembly output. However, you cann usually just assume that the compiler did the optimization.

    /EDIT: Tail call optimization for C has been added to the GCC in the course of a diploma thesis by Mark Probst. The thesis describes some interesting caveats in the implementation. It's worth reading.


    gcc 4.3.2 completely inlines this function (crappy/trivial atoi() implementation) into main() . Optimization level is -O1 . I notice if I play around with it (even changing it from static to extern , the tail recursion goes away pretty fast, so I wouldn't depend on it for program correctness.

    #include <stdio.h>
    static int atoi(const char *str, int n)
    {
        if (str == 0 || *str == 0)
            return n;
        return atoi(str+1, n*10 + *str-'0');
    }
    int main(int argc, char **argv)
    {
        for (int i = 1; i != argc; ++i)
            printf("%s -> %dn", argv[i], atoi(argv[i], 0));
        return 0;
    }
    

    Most compilers don't do any kind of optimisation in a debug build.

    If using VC, try a release build with PDB info turned on - this will let you trace through the optimised app and you should hopefully see what you want then. Note, however, that debugging and tracing an optimised build will jump you around all over the place, and often you cannot inspect variables directly as they only ever end up in registers or get optimised away entirely. It's an "interesting" experience...

    链接地址: http://www.djcxy.com/p/80538.html

    上一篇: 什么是memoization,我如何在Python中使用它?

    下一篇: 如果有的话,哪些C ++编译器会做尾巴