Re: Static method vs. template function
Chris wrote:
Just curious about technicalities here, but is there technically a
difference between a static method that gets bound per class and a
template method that gets instantiated per type?
Danger: AFAIK, the term 'method' is always used to refer to a bound
function, i.e. a memberfunction in C++, but you are talking about plain
functions here. I'm not sure though that this is incorrect, it is just
unusual.
That is, if we have:
class A
{
public:
static void untemplateMethod(A const& object);
};
template<typename DataType>
void templateMethod(DataType const& object)
{
}; [ NOTE: the semicolon is wrong here! ]
There is only one untemplateMethod for every A,
Errm, no. There is one untemplateMethod function for class A (or, one
function for all instances of class A, but even without any instances).
and also if there is a subclass of A, the subclasses get their own as
well.
No.
The function can be accessed via the class, via instances of that class and
via instances of derived classes, but there is still only a single
function.
But, templateMethod gets instantiated only for each object that it gets
used for as well, so in a sense only one exists for each class type as
well.
Is this a misunderstanding? Is one preferred over the other?
templateMethod gets instantiated for every type it gets used with. That
means that, other than the class-static function, it gets a separate
instantiation for derived classes. You can even specialise the function for
derived classes.
Uli
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]