【发布时间】:2019-08-29 17:06:23
【问题描述】:
我从这里得到的问题的基础: Failure to deduce template argument std::function from lambda function 这个线程中的问题是: 为什么这段代码不能将 lambda 传递给函数:
#include <iostream>
#include <functional>
template <typename T>
void call(std::function<void(T)> f, T v)
{
f(v);
}
int main(int argc, char const *argv[])
{
auto foo = [](int i) {
std::cout << i << std::endl;
};
call(foo, 1);
return 0;
}
这个帖子的答案是,因为 lambda 不是 std::function。但是为什么要编译这段代码:
#include <iostream>
#include <functional>
template <typename T>
void call(std::function<void(T)> f, T v)
{
f(v);
}
int main(int argc, char const *argv[])
{
auto foo = [](int i) {
std::cout << i << std::endl;
};
call({foo}, 1); // changed foo to {foo}
return 0;
}
【问题讨论】: