Click to See Complete Forum and Search --> : Template overloading


Wynants
December 12th, 2002, 05:19 AM
Hi there,

I'm using template overloading but can anyone tell me the difference bewteen normal function overloading and template overloading.

Template function:

template<class T>
T getParamater( int iWere, const T& iDefault )
{
// do something
}

overloading 1:
double getParamater( int iWere, const double& iDefault )
{
// do something else
}

overloading 2:
template<> double getParamater( int iWere, const double& iDefault )
{
// do something else
}

Is there a difference between overloading 1 and 2 :confused:

Greetings Tim

dude_1967
December 12th, 2002, 10:00 AM
Tim,

There are several interesting aspects of the supplied code example. Generally, providing a template is useful for repeating identical code sequences on several different data types. Within this context, there are sometimes slight differences in the desired template instance for a particular data type which merit the implementation of a template specialization for the particular data type. The example "overloading 2" is a a template specialization for the specialized type double.

However the "overloading 1" is a pure overload. Providing this pure overload is allowed. However, some developers might seriously ask why it is needed in a clear design. It seems like it would usually be unnecessary since the template specialization should be able to handle the functional needs for double.

An additional note: A good C++ compiler should be able to deduce the template parameter if none is explicitly provided. The implementation of "overloading 1" would actually mask the template instantiation in case one wanted to use the compiler's ability to deduce the template parameter.

For a good design, I would recommend providing the template and using template specializations where needed. It is, in my opinion, not proper to implement overloading 1 in addition to the template specialization. Also, if your design calls for many specializations, then the the use of a template might not be well suited for the needed functionality. You need to examine the design goals.

Chris.

:)

TheCPUWizard
December 12th, 2002, 10:14 AM
Dud is correct.

There are however some conditions where it makes sence to do an explicit overload for CLASS templates.

First there are those conditions where most of the code is deduceable by the compiler, but one or more functions needs manual intervention. This is usually better supported by making the member pure virtual in the template and deriving specific classes for each case. If only some of the instanciations of the template need manual intervention, or the method requires EXTREME speed, the manual overload is ideal.

Second there is the ability to protect a template so that it can only be instanciated for certain template parameters. Simply declare but do not define a protected/private member function that takes the class type as the parameter. Manually implement this method for allowed classes. Now at link time, if a programmer goes to use the template on a class other than one of the "approved" classes, a link error will occur.


** There are few truely RIGHT answers that apply to all circumstances **

galathaea
December 12th, 2002, 06:48 PM
Also, if a function template identifier is also used for a family of
overloaded functions, the search for which function to call
takes these steps (in the given order):

exact match from among the overloaded functions
exact match from template instantiation
best match from among the overloaded functions (ie. allowing type conversion)

Wynants
December 13th, 2002, 03:13 AM
Hi,
Already thanks for the information.
My template member function is for most of my types, but 3 class types expect another implementation so temlate overloading is justified I think. I know that there a some rules about choosing the functions depending on the type maching (if you use template oveloading there will never be a cast). I think type matching can make a difference if you use more then one template argument. I'm am using only one argument so it will never cast to another types because the template function is used instead.
In this example is there any difference between the overload functions? Or is it just taking the one I like the most?

Originally posted by TheCPUWizard
This is usually better supported by making the member pure virtual in the template and deriving specific classes for each case.

I'm not using template classes just a template member function in a normal class. Can I make this template member function pure virtual in the base class and implement it in the diriving classes?
(MSVC compiler) If it can, how?


Second there is the ability to protect a template so that it can only be instanciated for certain template parameters. Simply declare but do not define a protected/private member function that takes the class type as the parameter. Manually implement this method for allowed classes. Now at link time, if a programmer goes to use the template on a class other than one of the "approved" classes, a link error will occur.

Thanks, can become very usefull :D , never ever had thought about that myself.

Another question: Is there a way I can declare my templates in the headerfile and implement them in the cpp file (MSVC compiler)?

Greetings Tim

Elrond
December 13th, 2002, 04:26 AM
No. As far as I know, the implementation of a template must be in a public file (header).

PaulWendt
December 13th, 2002, 05:12 AM
I want to say that I agree with Elrond, but you COULD put the
template definition in a .cpp file ... but you'd still have to #include
that file. Remember, no object code can be instantiated for a
template definition because the compiler still wouldn't know what
type to create ... so the compiler waits until you actually
instantiate a template before the proper functions are generated.
That's why everything Elrond said is true.

--Paul

dude_1967
December 13th, 2002, 06:57 AM
Gurus,

Thanks for all the rich information on the topic of templates. I just love 'em.

Elrond, Paul: I think that there is a language keyword "export" which is intended to support template bodies within non-header source files.

Evidently it is very challenging to fully support this keyword and very few compilers support it. I have never used this keyword except in so far as to verify that it is not (yet) supported by GCC 3.2 or Microsoft VC6/7 compilers.

The Comeau compiler internet pages (http://www.comeaucomputing.com/) discuss this topic.

Chris.

:)

TheCPUWizard
December 13th, 2002, 07:17 AM
A "possible" method to reduce the coupling between a template based class and the rest of the code is to implement as much work as possible in a non-template base class [all type invariant operations and members]. Then you derive the template from the base class.

While this still leaves the template member functions "exposed" [the compiler does need to know what to write!], it can protect a large part of the design.