【发布时间】:2016-05-15 16:58:39
【问题描述】:
我正在完成“加速 C++”中的练习,我发现了一些我不理解的行为,即编译器如何推断函数模板中的类型。在练习 10-2 中,我们被要求编写一个模板函数,该函数可以计算向量或内置数组中算术类型列表的中位数。我偶然发现了一个解决此问题的示例解决方案,其中涉及一个模板函数,该函数计算并返回两个迭代器之间的容器中值,即我创建了以下名为“median.hpp”的文件:
#ifndef median_hpp
#define median_hpp
#include <algorithm>
#include <stdexcept>
#include <vector>
using std::domain_error;
using std::sort;
using std::vector;
template <class T, class Iterator>
T median(Iterator begin, Iterator end) {
//check if the container is empty
if (begin == end)
throw domain_error("median of an empty container");
//create a vector with the same type as the container
//and copy the container contents into it
vector<T> temp;
for ( ; begin != end; ++begin)
temp.push_back(*begin);
//sort the temporary vector, and compute and return the median
sort(temp.begin(), temp.end());
size_t mid = temp.size() / 2;
T ret = (temp.size() % 2 == 0)
? (temp[mid] + temp[mid - 1]) / 2
: temp[mid];
return ret;
}
#endif /* median_hpp */
所以如果我想计算一个数组和向量的中值来证明这个函数适用于两种容器类型,我会像这样使用前面提到的模板函数:
#include <iostream>
#include <vector>
#include "median.hpp"
using std::vector;
using std::cout;
using std::cin;
using std::endl;
int main()
{
int arr[] = {12,2,4,1,4,56,1};
const size_t nData = sizeof(arr)/sizeof(*arr);
vector<double> v(arr, arr + nData);
cout << median(v.begin(),v.end()) << endl;
cout << median(arr, arr + nData) << endl;
return 0;
}
但是,由于我不明白的原因,我收到以下错误:
没有匹配函数调用“中位数”...候选模板被忽略:无法推断模板参数“T”
据我所知,问题在于编译器无法从取消引用的迭代器中推断出“T”的类型。我想知道
A.为什么会这样?
B.有没有优雅的方法来解决这个问题?
【问题讨论】:
标签: c++ templates pointers types