C
CoolPint
I posted a question to which I got answers but unfortunately, the
suggestions didn't work so I am re-phrasing the question and posting
it again.
Below template function and the specialization 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 parameters are changed to const references like below compiler
complains
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 C-string called: ";
return strcmp(a,b) < 0 ? b : a;
}
g++ 3.2.3 gives me following:
tt.cpp:26: template-id `maximum<const char*>' for `const char*&
maximum(const
char*&, const char*&)' does not match any template declaration
I tried the followings too for the specialization but none worked:
const char * maximum<const char *>(const char * a, const char * b);
const char *& maximum<char *>(const char *& a, const char *& b);
const char *& maximum<const char *>(const char * const& a, const char
* const &b);
Can anyone kindly provide me with an explanation as to what is causing
the compiler error message? Thank you in advance.
suggestions didn't work so I am re-phrasing the question and posting
it again.
Below template function and the specialization 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 parameters are changed to const references like below compiler
complains
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 C-string called: ";
return strcmp(a,b) < 0 ? b : a;
}
g++ 3.2.3 gives me following:
tt.cpp:26: template-id `maximum<const char*>' for `const char*&
maximum(const
char*&, const char*&)' does not match any template declaration
I tried the followings too for the specialization but none worked:
const char * maximum<const char *>(const char * a, const char * b);
const char *& maximum<char *>(const char *& a, const char *& b);
const char *& maximum<const char *>(const char * const& a, const char
* const &b);
Can anyone kindly provide me with an explanation as to what is causing
the compiler error message? Thank you in advance.