C++11通过引用定义函数类型捕获

C++11 define function type capture by reference

本文关键字:类型 函数 定义 引用 C++11      更新时间:2023-10-16

如何将下面的函数转换为typedef?

auto fn = [&] (int x) { doSomething(x, 3); }

您可以使用decltype来获得精确的类型:

  auto fn = [&] (int x) { doSomething(x, 3); };
  using lambda_type = decltype(fn);

但是,如果您只想知道一个兼容的,更通用的类型,比如将lambda作为参数传递给另一个函数,您可以使用std::function<void(int)>(正如Joachim提到的)。

怎么样

using my_function_type = std::function<void(int)>;
typedef std::function<void(int)> my_function_type;

适用于VS2012