模板函子不能推导参考类型 [英] Template functor cannot deduce reference type
本文介绍了模板函子不能推导参考类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个functor f,它接受一个函数func和一个与func类型相同的参数t。我不能通过g到f因为编译错误(没有匹配的函数调用 f(int& void;&)(int&))
)。如果g将取非参考参数g(int s),则编译完成。或者如果我手动指定模板参数 f
I've got a functor f, which takes a function func and a parameter t of the same type as func. I cannot pass g to f because of compilation error (no matching function for call to f(int&, void (&)(int&))
). If g would take non-reference parameter g(int s), compilation finishes. Or if I manually specify template parameter f<int&>(i, g)
, compilation also finishes.
template<typename T>
void f(T t, void (*func)(T)) {}
void g(int& s) {}
int main(int, char*[])
{
int i = 7;
f(i, g); // compilation error here
return 0;
}
如何扣除工资?
推荐答案
您可以这样调用函数:
f<int&>(i, g);
但现在我也会通过参考。
But now i will be passed by reference too.
一般来说,我也会让函数成为一个模板类型:
In general, I'd make the function a template type too:
template <typename T, typename F>
void f(T t, F func)
{
func(t); //e.g
}
这篇关于模板函子不能推导参考类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文