abstract function templates

Jonathan M Davis jmdavisProg at gmx.com
Sun Dec 26 17:26:42 PST 2010


On Sunday 26 December 2010 16:18:19 Simen kjaeraas wrote:
> Andrej Mitrovic <andrej.mitrovich at gmail.com> wrote:
> > Does it really make sense for a class to have methods that accept any
> > type of argument? It's one thing to have a class specialized on some
> > type(s), e.g.:
> > 
> > class Foo(T1, T2)
> > {
> > 
> >     void bar(T1 var, T2 etc) { }
> > 
> > }
> > 
> > But having a class method which can accept any type, that seems odd to
> > me. I don't know since I've never used such a thing before, but maybe
> > it has some good use cases? (I'd love to know about those btw!).
> 
> Well, operator overloading comes to mind. Certainly for types that
> would allow mathematical operations with any generic numeric type.

In many cases, it would make sense to make virtual functions which have all of 
the types that make sense and then have a templated function called in each of 
them so that their implementation is still shared.

A big problem with having template functions be virtual is the fact that such 
functions don't exist until they're called, whereas a class and its virtual 
function need to exist regardless of whether the functions get called - 
particularly when you bring libraries into it.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list