Andrew Wilkins wrote:
Hi,
C++ forbids templated virtual member functions. For example, the
following will not (should not - it doesn't in GCC 3.4.3) compile:
<snip>
template <class T>
class Example {
public:
template <class C>
virtual void test() {}
};
</snip>
Would someone please explain to me why it is disallowed? I have not been
able to find any discussion of it anywhere (apart from the obvious, that
it's not allowed).
Thanks,
Andrew
you probably mean something like this:
struct C {
template <typename T>
virtual void test(T t) { /* something */ }
};
well, let's think about it:
- virtual implies the creation of a vtable for determining
at runtime which implementation to call
- a template definition allows to define an algorithm for
implementing different methods for different type
- using a template the compiler will only know that it needs
a certain implementation when such a call is made
- doing a call to such a method on a derived class with a type
that has not been instantiated in the base class, would
therefore require the base implementation to change, which
is not possible, because its definition must already be
complete at the time when you derive your new class from
the base class.
BTW: That's just what I guess...
Tom