C++ 编译器可以内联函数指针吗?
问题描述
假设我有一个函数 functionProxy
,它接受一个泛型参数 function
并调用它的 operator()
:
Suppose I've got a function functionProxy
that takes a generic parameter function
and call its operator()
:
<小时>
传递给它的对象可能是:
The object passed to it may be:
一个函子:
a functor:
一个函数:
a function:
一个 (C++0x) lambda 函数:
a (C++0x) lambda function:
<小时>
在上述所有情况下,编译器是否能够将 function
内联到 functionProxy
中?
推荐答案
没问题.
它知道function
的值和它传递给它的值是一样的,知道函数的定义,所以直接内联替换定义,直接调用函数.
It knows the value of function
is the same as the value it passes it, knows the definition of the function, so just replaces the definition inline and calls the function directly.
我想不出编译器不会内联单行函数调用的情况,它只是用函数调用替换函数调用,没有可能的损失.
I can't think of a condition where a compiler won't inline a one-line function call, it's just replacing a function call with a function call, no possible loss.
鉴于此代码:
定义 MANUALLY_INLINE
后,我们得到:
With MANUALLY_INLINE
defined, we get this:
没有,这个:
一样.(与 MSVC 2010 一起编译,原版.)
The same. (Compiled with MSVC 2010, vanilla Release.)
这篇关于C++ 编译器可以内联函数指针吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!