如果我不重载myfunc,则可以使用一些简单的代码。
void myfunc(int i)
{
std::cout << "calling myfunc with arg " << i << std::endl;
}
void myfunc(std::string s)
{
std::cout << "calling myfunc with arg " << s << std::endl;
}
void testalgos()
{
std::vector<int> v;
v.push_back(1);
v.push_back(2);
std::vector<std::string> s;
s.push_back("one");
s.push_back("two");
std::for_each( v.begin(), v.end(), myfunc);
std::for_each( s.begin(), s.end(), myfunc);
return;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "Hello World" << std::endl;
testalgos();
return 0;
}
for_each调用会重复以下构建错误。
错误C2914:'std :: for_each':无法推断模板参数,因为函数参数不明确 错误C2784:'_ Fn1 std :: for_each(_InIt,_InIt,_Fn1)':无法从'std :: _ Vector_iterator&lt; _Ty,_Alloc&gt;'中推断'_InIt'的模板参数。
如果我不重载myfunc,它确实有效。有人解释这里发生了什么。
TIA
答案 0 :(得分:10)
在该上下文中,编译器无法解决重载。 std::for_each()
期望其仿函数有一些任意类型F
,而不是某些特定的函数类型,因此重载的myFunc
在这里是不明确的。
您可以明确选择要使用的重载:
std::for_each( v.begin(), v.end(), (void (*)(int))myfunc);
std::for_each( s.begin(), s.end(), (void (*)(std::string))myfunc);
替代品(最后两个来自评论):
typedef void (*IntFunc)(int);
std::for_each(/*...*/, (IntFunc)myfunc);
typedef void IntFunc(int);
std::for_each(/*...*/, static_cast<IntFunc*>(&myFunc));
// using identity (e.g. from boost or C++0x):
std::for_each(/*...*/, (identity<void(int)>::type*)myfunc);
答案 1 :(得分:4)
编译器无法推断出仿函数的类型。你可以制作你的功能模板:
template<typename T> void myfunc(T);
template<> void myfunc(int i)
{
std::cout << "calling myfunc with arg " << i << std::endl;
}
template<> void myfunc(std::string s)
{
std::cout << "calling myfunc with arg " << s << std::endl;
}
然后按如下方式使用:
std::for_each( v.begin(), v.end(), myfunc<int>);
std::for_each( s.begin(), s.end(), myfunc<std::string>);
答案 2 :(得分:2)
编译器无法推断使用哪个,因为两个重载都会匹配参数(它不依赖于迭代器的类型)。
除了将参数显式地转换为合适的指针类型之外,另一个选项可能就是使用std::ptr_fun
辅助函数将其包装在仿函数中,并通过明确地给出(部分)来帮助模板演绎。
std::for_each( v.begin(), v.end(), std::ptr_fun<int>(myfunc));
std::for_each( s.begin(), s.end(), std::ptr_fun<std::string>(myfunc));