我的问题与这个问题有些相关: Lambdas and std::function 。请阅读此问题及其接受的答案。
因此,接受的答案表明以下代码使模板参数推导 lambda 成功,但为什么呢?
template<class T>
struct Identity{
typedef T type;//why this helps?
};
template<typename BaseT>
vector<BaseT> findMatches(vector<BaseT> search,
typename Identity<function<bool (const BaseT &)>>::type func)
{
vector<BaseT> tmp;
for(auto item : search)
{
if( func(item) )
{
tmp.push_back(item);
}
}
return tmp;
}
void Lambdas()
{
vector<int> testv = { 1, 2, 3, 4, 5, 6, 7 };
auto result = findMatches(testv, [] (const int &x) { return x % 2 == 0; });//lambda here.
for(auto i : result)
{
cout << i << endl;
}
}
int main(int argc, char* argv[])
{
Lambdas();
return EXIT_SUCCESS;
}
上面的代码可以工作,但我不知道为什么。首先我知道BaseT
的模板推导lambda 不会发生这种情况,因为这是一个不可推导的上下文( Identify<function<bool (const BaseT &)>>::type
)。相反,BaseT
从 testv
推导出来如int
,并且模板参数替换发生在 Identity<function<bool (const BaseT &)>>::type
中。
但是接下来会发生什么呢?替换后,Identity<function<bool (const BaseT &)>>::type
变成function<bool (const BaseT &)>
,但是lambda表达式不是不是这种类型,并且模板参数推导中不会发生转换(尽管这里是模板参数替换)?
谢谢您的解释!
附:我似乎知道编译器为每个 lambda 表达式生成唯一命名的类。那么为什么lambda可以匹配function<bool (const BaseT &)>
?
最佳答案
But what's next? after substitution,
Identity<function<bool (const BaseT &)>>::type
becomesfunction<bool (const BaseT &)>
, but isn't lambda expression not of that type, and conversions does not happen in template parameter deduction(although here is template parameter substitution)?
您是正确的,在模板参数推导过程中没有替换,但一旦完成,我们将函数调用视为调用实例化函数。
这意味着Identity<function<bool (const BaseT &)>>::type
已替换为 function<bool (const BaseT &)>
您正在调用的实例化函数是:
vector<int> findMatches(vector<int> search, function<bool (const int&)> func)
既然是混凝土function<bool (const int&)>
lambda 可用于构造 func
关于c++ - 针对 std::function 的 lambda 表达式和模板推导:为什么这有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51787005/