I'm trying to understand how template specialziations work. I've got the following function template:
template <typename T>
void function(const T &t1, const T &t2)
{
std::cout << "in function template: " << t1 << ", " << t2 << std::endl;
}
now, I want to specialize this function template in case it's called with a pointer to const:
// template specialization
template <>
void function(const char *&t1, const char *&t2)
{
std::cout << "in compare template specialization: " << t1 << ", " << t2 << std::endl;
}
but the compiler complains that it can't find a function template to specialize:
In file included from main.cpp:1:0:
template.h:23:5: error: template-id 'compare<>' for 'int compare(const char*&, const char*&)' does not match any template declaration
int compare(const char *&t1, const char *&t2)
^~~~~~~
template.h:10:5: note: candidate is: template<class T> int compare(const T&, const T&)
int compare(const T &t1, const T &t2)
if I specialize the template like this (a reference to a CONST pointer to const), it works:
// template specialization
template <>
int compare(const char * const &t1, const char * const &t2) // now the pointer itself is const
{
std::cout << "in compare template specialization: " << t1 << ", " << t2 << std::endl;
}
I want to call the function with a const char *Ptr = "hello world"
so I thought the inferred parameter T was char* and the parameter was const char *&.
Aren't the const in the function template parameter list always low level const?