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

Function template specialization

I have a template function of the type

template<typename T>
const T max(const T& a, const T& b)
{
return a b ? a : b;
}

I provide a template specialization only for C strings as follows:

template<>
const char* max(const char*& a, const char*& b)
{ -------------------------------------------------------------------------------------------------
ERROR
return strcmp(a, b) 0 ? a : b;
}

Now I have the following code in main:

const char* cstr1 = "STRING";
const char* cstr2 = "string";

const char* cstrMax = ::max(cstr1, cstr2);

when I compile this code on my Solaris machine, I keep getting the
error "Could not find a match for max<T>(const char*&, const char*&)"
at the line mentioned above (dotted line ending with ERROR).

Surprisingly when I change my template definition to the following,
everything compiles fine:

template<typename T>
T max(T& a, T& b)
{
return a b ? a : b;
}

As you can see all I have done is remove the const keyword qualifying
the arguments and the return type. In the first case, I would assume
that the typename should evaluate to "char*" but that obviously does
not happen. In the second case, the typename obviously evaluates to
"const char*" and everything works fine. What is the reason that there
is a compilation error in the first case?
Jan 17 '08 #1
1 1996
C++Liliput a écrit :
I have a template function of the type

template<typename T>
const T max(const T& a, const T& b)
{
return a b ? a : b;
}

I provide a template specialization only for C strings as follows:

template<>
const char* max(const char*& a, const char*& b)
{ -------------------------------------------------------------------------------------------------
>ERROR
return strcmp(a, b) 0 ? a : b;
}
Specialization of T=char* is
template<>
char* const max(char* const& a, char* const& b)
{
....
}

You should define
char const * const max(char const * const& a, char const * const& b)
{
....
}
Jan 17 '08 #2

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. ...
7
by: CoolPint | last post by:
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...
2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
3
by: Dave | last post by:
Hello all, I am trying to create a full specialization of a member function template of a class template. I get the following errors: Line 29: 'foo<T1>::bar' : illegal use of explicit...
1
by: Rafal Dabrowa | last post by:
Consider the following code: template <class T> void f(T, int) {} template <class T> void f(int, T) {} template<> void f(int, int) {} // ambiguous ? My compiler complains that the last...
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
2
by: Michael Stembera | last post by:
Here is a very simple piece of code to repro this bug. template<typename T, int N> inline bool foo( void ) { return true; } template<typename T> inline bool foo<T, 1>( void ) { return...
6
by: wkaras | last post by:
I tried a couple of compilers, and both gave errors compiling this: template <bool fin, typename T> T foo(T val); template <typename T> T foo<true, T>(T val) { return(val); } But both gave...
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.