如何将lambda传递给lambda?

Dra*_*neg 1 c++ lambda c++11

我还没有找到为什么这段代码不起作用:

#include <iostream>
#include <functional>

using namespace std;

int main()
{
  auto xClosure = [](const function<void(int&)>& myFunction) {
    myFunction(10);};

  xClosure([]
       (int& number) -> void
       {cout<<number<<endl;
       });
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

它返回:

g++ test.cc -o test -std=c++14
Run Code Online (Sandbox Code Playgroud)
 test.cc:9:5: error: no matching function for call to object of type 'const function<void
  (int &)>'
Run Code Online (Sandbox Code Playgroud)

Yak*_*ont 9

这与lambdas无关:

void test(const function<void(int&)>& myFunction) {
  myFunction(10);
}
Run Code Online (Sandbox Code Playgroud)

由于同样的原因,这无法编译; 你不能将文字绑定10int&.

也许你的意思

const function<void(int)>& myFunction
Run Code Online (Sandbox Code Playgroud)

这样做并修改lambda的签名应该使你的代码编译.