I've got a test program to see how compiler(g++) match template function:
#include<stdio.h>
template<class T>void f(T){printf("T\n");}
template<class T>void f(T*){printf("T*\n");}
template<> void f(int*){printf("int*\n");}
int main(int argc,char**) {
int *p = &argc;
f(p); // int*
return 0;
}
It prints int*
. Seems the specialized template is the high priority match? Then I switched the function declaration a bit, this time:
#include<stdio.h>
template<class T>void f(T){printf("T\n");}
template<> void f(int*){printf("int*\n");}
template<class T>void f(T*){printf("T*\n");}
int main(int argc,char**) {
int *p = &argc;
f(p); // T*
return 0;
}
It prints T*
. The only difference between the 2 programs is I changed the function declaration for overloaded "f" a bit, why the result is different?