Which overload function is prefered in this example?

The Example is taken from the < c++ primer > ( 4th Edtion ) by Stanley B. Lippman, and it resides in P681. He says : There are an overloaded function set which contains both ordinary overloaded functions as well as function templates.
And here are two of them:
1
2
template < typename T > int compare( const T& , const T& );
int compare( const char*, const char* );
He says the following call will run the template version.
1
2
3
char ch_arr1[] = "world", ch_arr2[] = "hi";
char * p1 = ch_arr1, *p2 = ch_arr2 ;
compare( p1, p2 );

Well, he also says the reason is that the function template is an exact match with binding char* to T while the plain version still requires a conversion from char* to const char*.
This confuse me!
As he says, the function template binds char* to T, so the instantiation of the function template should be int compare( const char* & , const char* & ) , not int compare( char* , char* ) right?
If it is the case, then the viable functions should be int compare( const char* &, const char* & ) which is an instantiation of the function template , and int compare( const char*, const char* ) which is the ordinary function, so the template version needs an extra binding apart from the conversion from char* to const char*, that is to say, the ordinary function should be a better match, Right?
Well, I test it on my machine , it turns out what Lippman says is the case, but i really don't understand, does what i said is unreasonable?
Note the difference between const char* and const char* &.

The difference is "const char* & " need an extra binding ,that is to say , if i give it the argument of char*, the compiler need to bind the argument, then convert char* to const char*, so the template version is a worse match than the plain one, Right?
I believe the confusion above is with const char * &. It's not a reference to a variable of type 'const char *', it's a constant reference to a variable of type 'char *' (which matches exactly the type of p1 and p2). If it helps, try changing compare to take a 'char * const' instead of a 'const char *', and the compiler should choose that version rather than its templated counterpart.

--Rollie
OMG! All of sudden, i am enlightened, what a surprising. Thanks you, rollie , it really helps me. And Thank you too, moorecm.
Topic archived. No new replies allowed.