Topic: Virtual template member functions


Author: "Nigel Porter" <nporter@ndsuk.com>
Date: 1998/09/17
Raw View
Does anyone know why its not possible to have a virtual template member
function in a class?

class A
{
public:
 template<class T> virtual T myFunc(T n){return n;};
};

Cheers,

Nigel




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/09/17
Raw View
Nigel Porter <nporter@ndsuk.com> writes:

> Does anyone know why its not possible to have a virtual template member
> function in a class?

Just try to design an efficient implementation of virtual dispatching
for an unbounded set of possible specializations, and you'll find out
why :-)

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Email_To::.Hans.Olsson@dna.lth.se (Hans Olsson)
Date: 1998/09/17
Raw View
In article <orn27za6tu.fsf@tiete.dcc.unicamp.br>,
Alexandre Oliva  <oliva@dcc.unicamp.br> wrote:
>
>Nigel Porter <nporter@ndsuk.com> writes:
>
>> Does anyone know why its not possible to have a virtual template member
>> function in a class?
>
>Just try to design an efficient implementation of virtual dispatching
>for an unbounded set of possible specializations, and you'll find out
>why :-)

First a related complaint:

Why does a non-virtual template member function not override a
virtual template member function? The number of possible specializations
is in this case trivially bounded (<=virtual functions in base-class
sharing the same name).

I have seen cases where it would have been beneficial, and the only
problem I see would be that a template function would be instantiated
based on the functions in the base-class and not by calls to the functions.

As for virtual template member functions I also thought it doable
until I considered all possibilities.

Code can:
1. Call the virtual template member function.
2a. A derived class can specialize&override with a non-template function.
2b. A derived class can override the virtual template member function
with a virtual template member function.

// Hypothetical language with v.t.m.f. i.e. not C++:
struct X {
 virtual template<class T> void f(T);
};
X&x=...
x.f(int(1)); // 1.
struct Y:public X {
 virtual void f(int); // 2a.
};
struct Z:public X {
 virtual template<class T> void f(T); // 2b.
};

Although a bit difficult I believe that compilers could handle
combinations of cases 1. and 2a, but combinations of 1. and 2b. seems
impossible unless compilers&linkers were substantially changed to allow
archives to contain templates (=general code for Z::f(T)) and not merely
specialization of template functions (code for Z::f(int),
Z::f(double),...). (Error messages would also be a problem.)



--
// Home page  http://www.dna.lth.se/home/Hans_Olsson/
// Email To..Hans.Olsson@dna.lth.se [Please no junk e-mail]




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]