I'm learning about templates in c++ and I found the following example.
From what I understand, the compiler should always try to use the most "specialized" template if there is no non-template functions matching, yet in this example, the first call result in calling function a(T*) instead of a(int*). Why? And why does second call act differently?
template<typename T> void a(T) {cout << "(T)" << endl;} template<> void a<>(int*) {cout << "(int)" << endl;} template<typename T> void a(T*) {cout << "(T*)" << endl;} template<typename T> void b(T) {cout << "(T)" << endl;} template<typename T> void b(T*) {cout << "(T*)" << endl;} template<> void b<>(int*) {cout << "(int)" << endl;} int main() { int i; a(&i); b(&i); return 0; }
The resulting output is:
(T*)
(int)
I expected it to be:
(int)
(int)