473,406 Members | 2,954 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.

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 2000
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: 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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.