The full answer is quite technical.
First, string literals have char const[N]
type.
Then there is an implicit conversion from char const[N]
to char const*
.
So both your template function match, one using reference binding, one using the implicit conversion. When they are alone, both your template functions are able to handle the calls, but when they are both present, we have to explain why the second foo (instantiated with T=char const[N]) is a better match than the first (instantiated with T=char). If you look at the overloading rules (as given by litb), the choice between
void foo(char const (&x)[4));
and
void foo(char const* x);
is ambigous (the rules are quite complicated but you can check by writing non template functions with such signatures and see that the compiler complains). In that case, the choice is made to the second one because that one is more specialized (again the rules for this partial ordering are complicated, but in this case it is because you can pass a char const[N]
to a char const*
but not a char const*
to a char const[N]
in the same way as void bar(char const*)
is more specialized than void bar(char*)
because you can pass a char*
to a char const*
but not vise-versa).