错误C2783无法推断模板参数

Not*_*ica 4 c++ templates

我遇到了这个错误.我也找到了一种解决方法,但它有点扼杀了锻炼的全部目的.

我正在尝试创建一个函数,它将指向同一个容器的两个迭代器.我会找到它们之间的元素总和.我创建了像vector这样的顺序容器的通用函数,它工作正常.我为关联容器重载了相同的函数.这是一个给出错误的人.

map<string,double> myMap;
myMap["B"]=1.0;
myMap["C"]=2.0;
myMap["S"]=3.0;
myMap["G"]=4.0;
myMap["P"]=5.0;

map<string,double>::const_iterator iter1=myMap.begin();
map<string,double>::const_iterator iter2=myMap.end();

cout<<"\nSum of map using the iterator specified range is: "<<Sum(iter1,iter2)<<"\n"; 
//Above line giving error. Intellisense is saying: Sum, Error: no instance of overloaded function "Sum" matches the argument list.

//function to calculate the sum is listed below (It appears in a header file with <map> header included):
template <typename T1,typename T2>
double Sum(const typename std::map<T1,T2>::const_iterator& input_begin,const typename std::map<T1,T2>::const_iterator& input_end)
{
double finalSum=0;
typename std::map<T1,T2>::const_iterator iter=input_begin;

for(iter; iter!=input_end; ++iter)
{
    finalSum=finalSum+ (iter)->second;
}

return finalSum;
}
Run Code Online (Sandbox Code Playgroud)

编译错误是:1> c:\ documents and settings\ABC\my documents\visual studio 2010\projects\demo.cpp(41):error C2783:'double Sum(const std :: map :: const_iterator&,const std :: map :: const_iterator&)':无法推断'T1'的模板参数

解决方法:

如果调用Sum(iter1,iter2)替换为Sum <string,double>(iter1,iter2),则编译正常.

我是否首先尝试按照C++标准做一些不可能的事情?

Dav*_*eas 6

在以下模板中,错误实际上非常清楚:

template <typename T1,typename T2>
double Sum(const typename std::map<T1,T2>::const_iterator& input_begin,
           const typename std::map<T1,T2>::const_iterator& input_end)
Run Code Online (Sandbox Code Playgroud)

类型T1T2不能从调用的地方的说法推断.这在标准中定义如此,如果你考虑它(在一般情况下)它是有道理的.

考虑一下,而不是std::map<>::const_iterator你有,sometemplate<T>::nested_type并且在呼叫地点的参数是一个int.如果编译器必须推导出类型,则必须为Universe中的sometemplate所有可能类型实例化T(无限集)并找到嵌套类型nested_type中的哪一个是typedef int.

正如有人在评论中指出的那样,你可以更改模板,这样就不需要对地图的键和值类型进行模板化,而只需要迭代器.


委托提取值

这是一种解决方法,提供Sum可以处理顺序容器和关联容器的单个实现.

namespace detail {
template <typename T> T valueOf( T const & t ) { return t; }
template <typename K, typename V>
V valueOf( std::pair<const K, V> const & p ) {
   return p.second;
}
}
template <typename Iterator>
double Sum( Iterator begin, Iterator end ) {
  double result = 0;
  for (; begin != end; ++begin) {
     result += detail::valueOf(*begin);
  }
  return result;
}
Run Code Online (Sandbox Code Playgroud)

我没有测试过代码,但是应该这样做.这可能比在Sum模板上使用SFINAE简单得多.