Re: why template version is prefered in this case
On 10 Aug., 21:41, junvi <junvi.b...@gmail.com> wrote:
#include <iostream>
using namespace std;
template <typename T>
void f(const T a) {
cout<<"Template version."<<endl;
}
void f(const int* a) {
cout<<"Plain version."<<endl;
}
int main() {
int *a=0;
f(a);
return 0;
}
the output is Plain version., I don't understand why is the case
The template version is no perfect match. If you
want to make it a better match, you better declare
it as
template <typename T>
void f(T* a) { ... }
If a function template and a non-template function
equally compete, the non-template function is preferred
over the template. This means that in the slightly
changed definition
template <typename T>
void f(const T* a) { ... }
the non-template function will be chosen instead.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]