我正在尝试围绕 C++ lambda 表达式和模板的交互进行思考。
此代码按我的预期工作:
#include <iostream>
int bar (int x, int (* f) (int))
{
return f (x);
}
double bar (double x, double (* f) (double))
{
return f (x);
}
int main ()
{
std::cout << bar (16, [] (int x) -> int { return x * x; }) << std::endl;
std::cout << bar (1.2, [] (double x) -> double { return x * x; }) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这样做也是如此:
#include <iostream>
#include <functional>
int …
Run Code Online (Sandbox Code Playgroud)