【发布时间】:2021-02-20 03:50:33
【问题描述】:
为什么第二次调用f() 会导致编译错误:
lambda 闭包不能转换为 std::function
#include <vector>
#include <functional>
void f(std::function<int(int)>f1, int x) {
f1(x);
}
int g(int x, int y) {
std::cout << x + y;
return x;
}
int main() {
f([](int x, int y = 10){ std::cout << x + y; return x; }, 20); // this works
f([](int x, int y = 10){ g(x,y); }, 20); // this doesn't compile
}
【问题讨论】:
-
您是否记得是否需要使用特定的 C++ 关键字才能从函数中返回一个值?你的闭包返回什么值,它们的类型是什么?