【发布时间】:2015-01-08 23:01:14
【问题描述】:
以下程序是非法的,我想了解原因:
#include <functional>
#include <iostream>
template<typename Result, typename Arg>
void deduce(std::function<Result(Arg)> f)
{
std::cout << "Result: " << typeid(Result).name() << std::endl;
std::cout << "Arg: " << typeid(Arg).name() << std::endl;
}
int main()
{
auto f = [](int x)
{
return x + 1;
};
deduce(f);
return 0;
}
clang 的输出:
$ clang -std=c++11 test.cpp
test.cpp:48:3: error: no matching function for call to 'deduce'
deduce(f);
^~~~~~
test.cpp:26:6: note: candidate template ignored: could not match 'function<type-parameter-0-1 (type-parameter-0-0)>' against '<lambda at test.cpp:34:13>'
void deduce(std::function<T2(T1)> f)
^
1 error generated.
看来我应该能够将我的 lambda 转换为 deduce 收到的 std::function。为什么在这种情况下编译器无法应用适当的转换?
【问题讨论】: