我创建了一个模板函数,试图自动推断模板参数。 MCVE(编译它):
template<class Value, class Allocator>
void foo(const std::vector<Value, Allocator>& v, const std::function<void(const Value&)>& f)
{
}
int main()
{
vector<int> v;
foo<int>(v, [](const int&){}); //okay
//foo(v, [](const int&){}); //not okay
return 0;
}
一开始我以为无法推导出分配器,但似乎这并没有解决问题。我猜测接下来的问题可能与将lambda转换为std :: function有关,但我对进一步步骤毫无头绪。有人知道我需要做什么才能使其可推导吗?
附注:我知道“const int&”可以变成“int”,但在实际代码中那里是非标量数据类型。
std::function
并使用以下代码:template
void foo(const std::vector& v, Func&& f)
{
}
- Jarod42std::vector
可能有超过 2 个模板参数,而您的代码没有考虑到这一点。 - rubenvb