Note that there are some explanatory texts on larger screens.

plurals
  1. POWhich, if any, C++ compilers do tail-recursion optimization?
    primarykey
    data
    text
    <p>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.</p> <p>Do any C++ compilers do this optimization? Why? Why not?</p> <p>How do I go about telling the compiler to do it?</p> <ul> <li>For MSVC: <code>/O2</code> or <code>/Ox</code></li> <li>For GCC: <code>-O2</code> or <code>-O3</code></li> </ul> <p>How about checking if the compiler has done this in a certain case?</p> <ul> <li>For MSVC, enable PDB output to be able to trace the code, then inspect the code</li> <li>For GCC..?</li> </ul> <p>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)</p> <p>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 <code>-O2</code> 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 :)</p> <hr> <p>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.</p> <p>If any destructor needs to be run after the tail-call, the tail-call optimization can not be done.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload