By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,130 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,130 IT Pros & Developers. It's quick & easy.

Template Member Functions

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.