问题描述
我以前认为编译器决定是否在编译时插入一个函数。
I used to think that the compiler decides whether to inline a function or not at compile time.
但是我在Effective C ++中找到了这个代码示例:
But then I found this code example in "Effective C++":
inline void f() {} // assume compilers are willing to inline calls to f
void (*pf)() = f; // pf points to f
f(); // this call will be inlined, because it's a "normal" call
pf(); // this call probably won't be, because it's through a function pointer
,这意味着决定是否内联函数是否在运行时完成,取决于你如何使用该函数。
Now I'm confused, does that mean the decision whether to inline a function or not is done at run time, depending on how you use that function?
推荐答案
p>不,它仍然在编译时完成。在你的示例中,实际上有两个版本的函数由编译器创建,一个内联,另一个不是。
No, it's still done at compile time. In your example there are actually two versions of the function created by the compiler, one inline and one not.
有时编译器有足够的信息来知道指针指向哪个函数通过静态分析,并可以内联它。无论如何。
Sometimes the compiler has enough information to know which function the pointer points to through static analysis, and can inline it anyway.
这篇关于内联是在编译时还是运行时完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!