I was trying to understand difference between a raw pointer and an vector iterator. However, the following program trips me out. Does template function have priority over non-template function?
Expected: hello! world!
Actual: hello! hello!
#include <bits/stdc++.h>
using namespace std;
template<typename It>
void foo(It begin, It end) {
cout << "hello! ";
}
void foo(const int* a, const int* b, size_t n=0) {
cout << "world! ";
}
int main() {
vector<int> A = {5,6,7,8,9};
int B[] = {1,2,3,4,5};
foo(A.begin(), A.end());
foo(B, B+5);
cout << endl;
}
In general iterators of the class template
std::vector
are not pointers (though in some early versions of compilers they were implemented as pointers).For the both calls there will be called the template function. The call of the non-template function requires conversion to const (qualification conversion).
The non-template function would be called if the array was declared with the qualifier const like
On the other hand, if you will declare the vector with the qualifier const like
nevertheless the template function will be called because there will be used objects of the type
std::vector<int>::const_iterator
that are not pointers to constant objects.