2

For example, I have a function template

template<class Fun>
void Foo(Fun f)
{
   ...
}

And the argument is a function object or a function pointer. If it is a normal function pointer, the performance is not good since the function cannot be inline. How about using a lambda function here? Thanks.

4
  • Like most things, if you haven't profiled that this is going to be a bottleneck, chances are it won't change a thing. Commented Jul 16, 2013 at 17:51
  • 2
    Lambdas should be as easy to inline as function objects. Whether they actually are is up to your compiler. Commented Jul 16, 2013 at 17:56
  • 1
    @DavidBrown: Indeed; lambdas are function objects. Commented Jul 16, 2013 at 17:56
  • If the argument is a function pointer, there is nothing fundamental stopping the compiler from eliminating the function call by inlining Foo. It would have to notice that the function pointer variable was initialized at line X with function Y, no chance occurred for it to be modified before it was called on line Z, and replace the pointer access at Z with a direct call to Y. It isn't completely trivial: once things become pointers, a compiler could just give up optimizing. And it is fragile: a seemingly innocuous operation could convince the compiler that the pointer could be modified. Commented Jul 16, 2013 at 18:00

2 Answers 2

4

As you say, calls through function pointers often can't be inlined since the target function may only be known at run time.

A lambda can be inlined here. Its type (deduced as the template parameter Fun) is known at compile time; and therefore the function itself (Fun::operator()) is also known then.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that in some cases, stateful lambda's storing references may require pointer-based data access. On the other hand, with the body of Foo visible as well as the body of Fun, the entire mess can be inlined and the lambda Fun's existence removed.
3

The closure type is the type created by a lambda expression. Its call operator (i.e., operator ()) is declared as inline as per 5.1.2/5 (emphasis is mine):

The closure type for a lambda-expression has a public inline function call operator (13.5.4) whose parameters and return type are described by the lambda-expression’s parameter-declaration-clause and trailing-return-type respectively.

In addition, the compiler can effectively see the definition of the call operator and, therefore, is able to inline the function call.

2 Comments

The inline keyword in C++ has nothing directly to do with making code inline in the sense of the optimization.
@Yakk: It's true that the compiler has no obligation to place the body of the function at the point of the call. However, the inline keyword indicates to the compiler that this is preferable (see 7.1.2/2).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.