Two questions about inline functions in C++

前端 未结 6 1155
情书的邮戳
情书的邮戳 2021-01-13 17:09

I have question when I compile an inline function in C++.

Can a recursive function work with inline. If yes then please describe how.

I am sure about loop

6条回答
  •  情深已故
    2021-01-13 17:47

    This particular function definitely can be inlined. That is because the compiler can figure out that this particular form of recursion (tail-recursion) can be trivially turned into a normal loop. And with a normal loop it has no problem inlining it at all.

    Not only can the compiler inline it, it can even calculate the result for a compile-time constant without generating any code for the function.

    With GCC 4.4

    int fac = f(10); 
    

    produced this instruction:

    movl    $3628800, 4(%esp)
    

    You can easily verify when checking assembly output, that the function is indeed inlined for input that is not known at compile-time.

提交回复
热议问题