While I was testing my understanding of Functioin Template features by
playing with simple function templates, I got into a problem which I
cannot understand. I would be very grateful if someone offered me a
kind explanation. TIA
Function template and specialization below works fine:
template <typename T>
T maximum (T a, T b)
{
cout << "Template Called: ";
return a < b ? b: a;
}
#include <cstring>
template<>
const char * maximum<const char *>(const char * a, const char * b)
{
cout << "Normal called: ";
return strcmp(a,b) < 0 ? b: a;
}
BUT if I change the template to accept const reference to T like
below:
template <typename T>
const T &maximum (const T & a, const T & b)
{
cout << "Template Called: ";
return a < b ? b: a;
}
#include <cstring>
template<>
const char * maximum<const char *>(const char * a, const char * b)
{
cout << "Normal called: ";
return strcmp(a,b) < 0 ? b: a;
}
The compiler (g++ v3.2) generates the following error message.
tt.cpp:25: template-id `maximum<const char*>' for `const char*
maximum(const
char*, const char*)' does not match any template declaration
Can anyone explain what the problem is?
P.S I am trying to learn the features by playing around. So I would
really appreciate an explanation as to what is causing the problem
rather than "You should overload the template with normal function",
etc types of replies. Thank you very much in advance.
playing with simple function templates, I got into a problem which I
cannot understand. I would be very grateful if someone offered me a
kind explanation. TIA
Function template and specialization below works fine:
template <typename T>
T maximum (T a, T b)
{
cout << "Template Called: ";
return a < b ? b: a;
}
#include <cstring>
template<>
const char * maximum<const char *>(const char * a, const char * b)
{
cout << "Normal called: ";
return strcmp(a,b) < 0 ? b: a;
}
BUT if I change the template to accept const reference to T like
below:
template <typename T>
const T &maximum (const T & a, const T & b)
{
cout << "Template Called: ";
return a < b ? b: a;
}
#include <cstring>
template<>
const char * maximum<const char *>(const char * a, const char * b)
{
cout << "Normal called: ";
return strcmp(a,b) < 0 ? b: a;
}
The compiler (g++ v3.2) generates the following error message.
tt.cpp:25: template-id `maximum<const char*>' for `const char*
maximum(const
char*, const char*)' does not match any template declaration
Can anyone explain what the problem is?
P.S I am trying to learn the features by playing around. So I would
really appreciate an explanation as to what is causing the problem
rather than "You should overload the template with normal function",
etc types of replies. Thank you very much in advance.
Comment