473,372 Members | 1,352 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,372 software developers and data experts.

Novice Question: Function Template Specialization

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.
Jul 22 '05 #1
7 5056
CoolPint wrote:
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?


Your specialization doesn't fit the template. The template has
references to const as parameters and return type, but the
specialization uses a non-const non-reference type. Try:

#include <cstring>
template<>
const char * const & maximum<const char *>(const char * const & a,
const char * const & b)
{
cout << "Normal called: ";
return strcmp(a,b) < 0 ? b: a;
}

Jul 22 '05 #2
On 19 Nov 2003 07:44:49 -0800, co******@yahoo.co.uk (CoolPint) wrote:
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 [snip]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?


A specialization has to have the same arguments as the source
template, otherwise it isn't a specialization of that template.
Substituting T=const char* gives:

#include <cstring>
template<>
inline const char*& maximum<const char*>(const char*& a, const char*&
b)
{
cout << "Normal called: ";
return strcmp(a,b) < 0 ? b: a;
}

If you inline it, the reference parameters shouldn't add any overhead
(and you can define it in a header).

Tom
Jul 22 '05 #3
CoolPint wrote:
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)
This template is for const references !
{
cout << "Template Called: ";
return a < b ? b: a;
}

#include <cstring>
template<>
const char * maximum<const char *>(const char * a, const char * b)
This has no references !

Try:

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?


See comments above.

Jul 22 '05 #4
On Wed, 19 Nov 2003 16:07:16 +0000, tom_usenet
<to********@hotmail.com> wrote:
A specialization has to have the same arguments as the source
template, otherwise it isn't a specialization of that template.
Substituting T=const char* gives:

#include <cstring>
template<>
inline const char*& maximum<const char*>(const char*& a, const char*&
b)


Doh! As Rolf says, it doesn't give that at all, but:

template<>
inline const char* const& maximum<const char*>(const char* const& a,
const char* const& b)

Tom
Jul 22 '05 #5
> Try:

const char * &maximum<const char *>(const char * &a, const char * &b)
{
cout << "Normal called: ";
return strcmp(a,b) < 0 ? b: a;
}


Thank you for answering my question. All the replies suggested the
same solution (to have the parameters to be const reference to make
them same as
the original template function).

I think I tried doing that before posting the question. Anyhow, I
tried again after your replies but unfortunately the compiler still
complains!

tt.cpp:26: template-id `maximum<const char*>' for `const char*&
maximum(const
char*&, const char*&)' does not match any template declaration

Is it my compiler or am I still missing something? I would appreciate
further answer/explanation on this problem. TIA
Jul 22 '05 #6
CoolPint wrote:
Try:

const char * &maximum<const char *>(const char * &a, const char * &b)

{
cout << "Normal called: ";
return strcmp(a,b) < 0 ? b: a;
}

Thank you for answering my question. All the replies suggested the
same solution (to have the parameters to be const reference to make
them same as
the original template function).

I think I tried doing that before posting the question. Anyhow, I
tried again after your replies but unfortunately the compiler still
complains!

tt.cpp:26: template-id `maximum<const char*>' for `const char*&
maximum(const
char*&, const char*&)' does not match any template declaration

Is it my compiler or am I still missing something? I would appreciate
further answer/explanation on this problem. TIA


Tom's second reply got it right.

i.e.
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)
{
....
}

Jul 22 '05 #7
Gianni Mariani <gi*******@mariani.ws> wrote in message news:<bp********@dispatch.concentric.net>...
Tom's second reply got it right.

i.e.
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)
{
...
}


Thank you for that. I tried various combinations and I missed his
second reply. It works. Thank you all those who spent time to answer
my silly question.

Now I see that specialization has to specialize the very same format
off "function parameter" of the ogiginal function template.
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
6
by: NKOBAYE027 | last post by:
FIRST POST Hi All: I'm trying to write a simple specialization before moving on to something a bit more complex - always a good idea in my case, at least. :o) I'm trying to adapt the example...
2
by: Alfonso Morra | last post by:
I have a function template declared as ff: template <class T1, class T2> size_t find( const T1& col, const T2& val, const ColFindEnum& dir ) ; I wanted to specialize it for string,long as...
1
by: Alfonso Morra | last post by:
if I have a class template declared as ff: (BTW is this a partial specialization? - I think it is) template <typename T1, myenum_1 e1=OK, my_enum_2=NONE> class A { public: A(); virtual...
6
by: merdem | last post by:
Hi all, I just started to mess around with templates. First I declared a class Image as follows(this is a small version of the real thing which is pretty big): template<int depth, int space,...
5
by: desktop | last post by:
I have this example: template<class T(1) void f( T ); template<class T(2) void f( T* ); template< (3)
13
by: mike b | last post by:
Hello everyone, thanks in advance for your help. I'm new to C++ templates and have run into some issues using member function templates. I have a shared library containing templates that I'm...
54
by: shuisheng | last post by:
Dear All, I am always confused in using constants in multiple files. For global constants, I got some clues from http://msdn.microsoft.com/en-us/library/0d45ty2d(VS.80).aspx So in header...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.