Topic: template function overloading


Author: steffend@lamar.colostate.edu (Dave Steffen)
Date: 1995/07/22
Raw View
Hi all!

 I've got a question about something in the template section of
the April 28 draft. Section 14.10.5 gives this example of overloading:

template<class T> T max (T a, T b) { ... }
int max (int a, int b);

 Which is followed by a note that says:

Note: this implies that a definition of max(int, int) ... will be
implicitly generated from the template...

 Does this mean that I end up with two versions of max (int,
int): one explicitly defined and one generated from the template? If I
call max (int, int) which one gets called?

 The note then continues on:

If such implicit instantiation is not wanted, the explicit
specialization syntax should be used instead:

template<class T> T max (T a, T b) {...}
int max<int> (int a, int b);

 So this gives me a single definition of max (int, int)?

 I'm a bit confused by all of this; any enlightening comments
from the C++ Gods out there would be most appreciated!

 THANKS!!!

                                 /\
                                 \/

Dave Steffen                      No, his mind is not for rent
Dept. of Physics                  To any God or Government
Colorado State University         Always hopeful, yet discontent
steffend@lamar.colostate.edu      He knows changes aren't permanent-
          But change is...
"Speak softly...
... and carry a black belt!"              -Neal Peart / RUSH
-----------------------------------------------------------------------











Author: s5vijen@portland.watson.ibm.com (Anil Vijendran)
Date: 1995/07/23
Raw View
>>>>> On 22 Jul 1995 23:02:11 GMT, steffend@lamar.colostate.edu (Dave
Steffen) said:

    Dave> Hi all!
    Dave>  I've got a question about something in the template section of
    Dave> the April 28 draft. Section 14.10.5 gives this example of overloading:

    Dave> template<class T> T max (T a, T b) { ... }
    Dave> int max (int a, int b);

    Dave>  Which is followed by a note that says:

    Dave> Note: this implies that a definition of max(int, int) ... will be
    Dave> implicitly generated from the template...

    Dave>  Does this mean that I end up with two versions of max (int,
    Dave> int): one explicitly defined and one generated from the template? If I
    Dave> call max (int, int) which one gets called?

If you notice carefully, there are no two definitions. The only
definition is the one that'll be generated from the function
template. The other one is simply a declaration. The effect of the
declaration to get an instantiation of the max template to be
generated.

This implies that you can't have another definition of max, like this:

int max(int a, int b) { ... }

because in this case, there'll be two cases for a call like:

 int a, b;
 ...
 cout << max(a, b);,

one generated from the function template and the other one you
defined.

If you really want a specialization for a specific type, thus
suppressing the instantiation for that type, then you need to use the
following syntax for defining it:

 complex max<complex>(complex a, complex b)
 {
  ...
 }







--
Peace.... +<:-)

Anil
akv@cacs.usl.edu
s5vijen@watson.ibm.com