I have the following definition.
using namespace std;
template <typename T>
void foo(const T &s) {
cout << 1;
}
template <typename T>
void foo(const T *s) {
cout << 2;
}
int main(int argc, const char * argv[]) {
char str[] = "ss";
char *s = str;
foo(s);
return 0;
}
Then it outputs
1
From my understanding, both versions have to go through a const conversion. Then void foo(const T *s)
is more specialized and should be invoked. However the compiler chose void foo(const T& s)
. What is the explanation?