#include <iostream>
using namespace std;
template <typename T> void compare(const T&, const T&){
cout<<"T"<<endl;
}
void compare(const char*, const char*){
cout<<"const char*"<<endl;
}
int main()
{
char a[]="123";
char b[]="123";
char *p1 = a, *p2 = b;
compare(p1,p2);
return 0;
}
the result is: T
but why? after instantiation the template function may be so:
compare(const char*&, const char*&)
is the same as ordinary function. and the ordinary function should be called!
This is why I think writing
T const&is superior to writingconst T&.The template function is instantiated with the signature
void(char* const&, char* const&)which is a better match thanvoid(char const*, char const*), as it requires no conversions of the pointers fromchar*tochar const*.