我的包装功能有问题。
template <typename Iter, typename SomeFunction>
void wrap(Iter first, Iter last, SomeFunction someFunction)
{
someFunction(first, last);
}
我想这样使用它:
template <typename Iter>
void fill5(Iter first, Iter last)
{
fill(first, last, 5);
}
int main()
{
vector<int> v(100, -1);
wrap(v.begin(), v.end(), fill5);
}
但我得到
test.cpp: In function ‘int main()’:
test.cpp:16:40: error: no matching function for call to ‘wrap(std::vector<int>::iterator, std::vector<int>::iterator, <unresolved overloaded function type>)’
test.cpp:16:40: note: candidate is:
wrap.h:6:6: note: template<class Iter, class SomeFunction> void wrap(Iter, Iter, someFunction)
我知道如果我像这样调用该函数
wrap(v.begin(), v.end(), fill5< vector<int>::iterator> );
它会编译。但我总是必须明确地这样做吗?太糟糕了。为什么编译器无法推断出将使用哪个函数?是否有可能编写包装函数来接受第一个函数中的参数?