I have a templated function, which takes an array reference as a parameter:
template<typename T, int arrSize>
void foo(T (&pArr)[arrSize]) {
cout << "base template function" << endl;
}
I want to specialize this function for C-strings:
template<const char *, int arrSize>
void foo(const char * (&pArr)[arrSize]) {
cout << "specialized template function" << endl;
}
I try to instantiate the base and the specialization:
int main(int argc, const char **argv) {
float nums[] = {0.3, 0.2, 0.11};
const char *words[] = {"word1", "word2"};
foo(nums);
foo(words);
}
But I only seem to get the base instantiation:
./foo
base template function
base template function
I have compiled this on a Mac using clang++ with -std=c++17.
The problem is that the 2nd overloaded function template that you've provided has a non-type template parameter of type
const char*
that cannot be deduced from the function parameter. So to call this overloaded version we have to explicitly provide the template argument corresponding to this non-type parameter.To solve this just remove the first non-type template parameter as shown below:
Demo
Also note that function templates cannot be partially specialized but they can be fully specialized or they can be overloaded.