【发布时间】:2013-08-18 17:01:48
【问题描述】:
为什么会出现编译错误no matching function for call to `f( __gnu_cxx::__normal_iterator > >)'?
#include <vector>
template<typename T>
void f(const typename std::vector<T>::iterator &) {}
void g() {
std::vector<int> v;
f<int>(v.end()); // Compiles.
f(v.end()); // Doesn't compile, gcc 4.3 can't find any match.
}
最后我想写一个函数,它只需要一个向量迭代器,并且无法编译(有一个有意义的错误)其他任何东西。所以template<typename T>void f(const T&) {} 不是一个好的解决方案,因为它也可以为其他类型编译。
【问题讨论】:
-
你有什么问题?编译错误的原因是您没有为模板化函数调用提供类型。即使您提供了
int作为向量类型,您仍然需要指定它以调用f。