Problem with template specialization

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.
 
V

Victor Bazarov

CoolPint said:
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:
[...]

You'd forgotten to post the code that _calls_ your functions.
Please post the _complete_ code you used to pass to your
compiler.

Also, keep in mind that in

template<class T> T foo(T t) {
return t;
}

template<> int& foo<int>(int& t) {
return 2*t;
}

the second one is _NOT_ a specialisation of the first one
because in it the argument type is not the same as the stated
template argument (int& is not the same as int).

Victor
 
C

CoolPint

I got an answer which works. The specialization has to be:

template <typename T>
const T & maximum (const T & a, const T & b);

template<>
const char * const & maximum<const char *>(const char * const & a,
const
char * const & b)
{
...
}

I now see that specialization has to specialize the exact same format
of "function parameters" of the orginal function template.
 
S

Sergei Zyablov

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;
}

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);

Try this:

typedef char* char_ptr;

template<>
const char_ptr& maximum<char_ptr>( const char_ptr& a, const char_ptr& b ){
cerr << "Specialization has been used." << endl;

return (strcmp(a,b) < 0) ? b : a;
}
Can anyone kindly provide me with an explanation as to what is causing
the compiler error message? Thank you in advance.

const char*& r;
is not a const reference to char*. This is a reference to const char*.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,765
Messages
2,569,568
Members
45,042
Latest member
icassiem

Latest Threads

Top