我还没有找到为什么这段代码没有
工作:

#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;
}

它返回:
g++ test.cc -o test -std=c++14

最佳答案

这与lambdas无关:

void test(const function<void(int&)>& myFunction) {
  myFunction(10);
}

由于相同的原因而无法编译;您不能将文字10绑定(bind)到int&

也许你是说
const function<void(int)>& myFunction

这样做并修改lambda的签名应该可以使您的代码编译。

关于c++ - 如何将lambda传递给lambda?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44090045/

10-11 23:12
查看更多