473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Problem with template specialization

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
3 2721
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit...
4
by: TT \(Tom Tempelaere\) | last post by:
Comeau compiler complains (too few arguments for class template "B") at line *** #include <memory> template<typename T, size_t n> struct A {}; template<typename T, size_t n> struct B;
13
by: Walt Karas | last post by:
The following gives an error in the declaration of the member function x() of the class template Tpl, compiliing with a recent version of GCC under Solaris: class A { }; class B { }; ...
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
1
by: toton | last post by:
Hi, I am doing some template specialization for a template class, where specialization is done on a member function. I am not able to get exact syntax for it. The code is give as below, enum...
1
by: Bari | last post by:
Hi, When i do the following specializition which commented in the source then including the header in two cpp files gives me an error: test3.obj : error LNK2005: "class A<int* __cdecl...
9
by: Jerome Durand | last post by:
Hello, I'm trying to write something along the following lines but I cannot get this to compile. template <typename derivedstruct Base { typedef typename derived::valueType valueType;...
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
8
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
6
by: abir | last post by:
i have a template as shown template<typename Sclass Indexer{}; i want to have a specialization for std::vector both const & non const version. template<typename T,typename Aclass...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.