Function template specialization



 DEVELOP > c-Plus-Plus > Function template specialization

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "C++Liliput"
Date: 17 Jan 2008 04:59:58 AM
Object: 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?
.

User: "Michael DOUBEZ"

Title: Re: Function template specialization 17 Jan 2008 05:18:29 AM
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)
{
....
}
.


  Page 1 of 1

1

 


Related Articles
Full specialization of template member function
Using implicit function template specialization to check if an identifier is an array
Template class member function specialization
specialization of a template function in a template class?!?!?
Difference between template function specialization and function overloading.
template class member function specialization
problem in static function template specialization
Partial template specialization for function
Static Member Function Template Specialization
Member template function specialization in a template class
repost: template function specialization & basic question...
template function specialization inside template class possible?
Specialization of member function template in template class?
Template specialization of pointers with function pointers
Partial template specialization on a function
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER