且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

关于C ++中内联函数的两个问题

更新时间:2023-02-25 18:04:58

这个特定的函数肯定可以内联。这是因为编译器可以知道这种特定形式的递归(尾递归)可以简单地变成正常循环。和一个正常的循环,它没有任何问题inlining它在所有。

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.

使用GCC 4.4

int fac = f(10); 

产生此指令:

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.