在C ++中,对于编译器“内联”意味着什么,一个函数对象? [英] In C++ what does it mean for a compiler to "inline" a function object?
问题描述
在关于功能对象的维基百科文章中,在与 for_each 配合使用时具有性能优势,因为编译器可以内联它们。
In the wikipedia article about function objects it says such objects have performance advantages when used with for_each because the compiler can "inline" them.
这个上下文...或任何上下文我尴尬地说。感谢任何帮助!
I'm a bit foggy on exactly what this means in this context... or any context I'm embarrassed to say. Thanks for any help!
推荐答案
for_each
模板的最后一个参数是 。 是可以使用()
运算符(可能带有参数)调用的函数。通过定义,有两种不同类型的函子:
The last parameter of for_each
template is a functor. Functor is something that can be "called" using the ()
operator (possibly with arguments). By defintion, there are two distinctive kinds of functors:
- 普通非成员函数是
函子。 - 具有重载
()
运算符(所谓的函数对象)的类类型的对象也是函子。
- Ordinary non-member functions are functors.
- Objects of class type with overloaded
()
operator (so called function objects) are also functors.
现在,如果你想使用普通函数作为 for_each
,它看起来像下面这样
Now, if you wanted to use an ordinary function as a functor for for_each
, it would look something like the following
inline void do_something(int &i) { /* do something */ }
int main() {
int array[10];
std::for_each(array, array + 10, &do_something);
}
在这种情况下, for_each
template使用[deduced]参数
& do_something
。从 for_each
函数的角度来看,这是一个运行时值。因为它是一个运行时值,所以对函子的调用不能被内联。 (就像在一般情况下不可能通过函数指针内联任何调用)。
In this case the for_each
template is instantiated with [deduced] arguments <int *, void (*)(int &)>
. Note that the actual functor value in this case is the function pointer &do_something
passed as the function argument. From the point of view of for_each
function this is a run-time value. And since it is a run-time value, the calls to the functor cannot be inlined. (Just like it is in general case impossible to inline any call made through a function pointer).
但是如果我们使用一个函数对象,代码可能如下
But if we use a function object instead, the code might look as follows
struct do_something {
void operator()(int &i) { /* do something */ }
};
int main() {
int array[10];
std::for_each(array, array + 10, do_something());
}
在这种情况下, for_each
template用[deduced] arguments < int *,do_something>
实例化。从 for_each
内部对函数的调用将被定向到 do_something :: operator()
。调用的目标是已知的,并在编译时固定。因为目标函数在编译时是已知的,所以调用可以容易地被内联。
In this case the for_each
template is instantiated with [deduced] arguments <int *, do_something>
. The calls to the functor from inside for_each
will be directed to do_something::operator()
. The target for the call is known and fixed at compile-time. Since the target function is known at compile-time, the call can easily be inlined.
在后一种情况下,我们当然也有一个运行时值作为参数传递给 for_each
。它是我们在调用 for_each
时创建的 do_something
类的一个[可能的虚拟临时实例。但是这个运行时间值对调用的目标没有影响(除非 operator()
是虚拟的),因此它不会影响内联。
In the latter case we, of course, also have a run-time value passed as an argument to for_each
. It is a [possibly "dummy" temporary] instance of do_something
class we create when we call for_each
. But this run-time value has no effect on the target for the call (unless the operator ()
is virtual), so it doesn't affect inlining.
这篇关于在C ++中,对于编译器“内联”意味着什么,一个函数对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!