std :: function和使用auto一样有效吗?

时间:2014-03-08 03:41:25

标签: c++ c++11 lambda

说我有一个我要定义的lambda。我可以这样做,

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

或者,

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

第二个是否有任何惩罚? std::function是否有开销我应该警惕?我不经常使用它;只有在做递归时。

1 个答案:

答案 0 :(得分:3)

使用std::function确实需要花费大量费用来讨论这笔费用,您可以阅读Efficient Use of Lambda Expressions and std::function