gpt4 book ai didi

c++ - 如何将 lambda 传递给 lambda?

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:25:01 26 4
gpt4 key购买 nike

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

#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
 test.cc:9:5: error: no matching function for call to object of type 'const function<void
(int &)>'

最佳答案

这与 lambda 无关:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com