By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

Problem with template specialization

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"CoolPint" <co******@yahoo.co.uk> wrote...
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

Jul 22 '05 #2

P: n/a
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.
Jul 22 '05 #3

P: n/a
co******@yahoo.co.uk (CoolPint) wrote in message news:<15**************************@posting.google. com>...
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*.
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.