【发布时间】:2017-01-14 15:35:20
【问题描述】:
我很难理解这一点。我知道我的编译器(Visual Studio)知道 lambda 是什么“类型”,因为有时它显示它是一个 lambda,但是我的模板没有推断它,auto 关键字也没有。
template <typename T> void templatedFunc(T (*funcPtr)(void)) { }
int main()
{
templatedFunc([] () { return 6;} ); // Error, no template matches argument list
int (*funcPtr)(void) = [] () { return 6;};
templatedFunc(funcPtr); // Works fine
auto p = [] () { return 6; };
templatedFunc(p); // Error, no template matches
auto p = [] () -> int { return 6; }; // Trying with explicit return type
templatedFunc(p) // Error, still doesn't work
}
我真的不明白,任何帮助都会很棒。当我将鼠标悬停在变量“p”上时,它的类型显示为 int()。我可以使这项工作的唯一方法是明确声明一个指针,如:
int (*ptr) (void) = [] () { return 6;};
我不断收到的错误是:
No instance of function template matches the argument list. Argument types are lambda []int () -> int
谢谢。
【问题讨论】:
标签: c++ templates pointers lambda type-deduction