IS STD ::功能与使用自动一样有效

Is std::function as efficient as using auto?

本文关键字:一样 有效 功能 STD IS      更新时间:2023-10-16

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

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

或这个,

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

对第二次进行罚款吗?std::function是否应该谨慎?我不经常使用它;仅在进行递归时。

使用std :: function确实会遇到成本,以便对此成本进行良好的讨论。