namzug
namzug

Reputation: 155

Is std::function as efficient as using auto?

Say I have a lambda I want to define. I can do it like this,

auto foo = [] (int x)
{
  return x == 42;
};

or this,

std::function<bool(int)> foo = [] (int x)
{
  return x == 42;
};

Is there any penalty for doing the second one? Does std::function have overhead I should be wary of? I don't use it very often; only when doing recursion.

Upvotes: 2

Views: 301

Answers (1)

Shafik Yaghmour
Shafik Yaghmour

Reputation: 158599

Using std::function does incur a cost for a good discussion of this cost you can read Efficient Use of Lambda Expressions and std::function.

Upvotes: 3

Related Questions