469,951 Members | 2,514 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,951 developers. It's quick & easy.

Virtual Member templates

I wanted to know why member templates cannot be virtual -
is it because there is a conceptual impossibility in having them, or is
it because the current mechanism of implementaing virtual functons
would not work with that concept?

Assuming that a compiler doesnot support "export" keyword, I guess it
is necessary that all instantiations of the member template must get
the definition of the template (and hence the definition of the class
of which it is a member) at compile time. In such a case, it is
certainly not impossible for the linker to combine up the vtables to
generate a final entry (it is no different than combining individual
symbol tables, geneate a new one and resolve references.)

I might not be technically correct in what I have written above. So
please correct me if I am writing nonsense.

Thanks a lot.

Nov 11 '05 #1
2 2086
* Neelesh:
I wanted to know why member templates cannot be virtual -
is it because there is a conceptual impossibility in having them, or is
it because the current mechanism of implementaing virtual functons
would not work with that concept?


More that the C++ template mechanism is unconstrained, so that a
templated virtual function corresponds to an infinite number of
potential virtual functions, and you need whole program analysis to
determine which ones are actualized, and whole program analysis isn't
well supported by conventional C and C++ separate compilation.

In addition, outside the standard but well inside the realm of general
practical C++ usage, there is the problem of dynamic libraries.

With templated virtual functions a dynamic library would require dynamic
back-patching somewhere, to provide at run-time the equivalent of or a
substitute for the static whole program analysis.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 11 '05 #2
Ian
Neelesh wrote:
I wanted to know why member templates cannot be virtual -
is it because there is a conceptual impossibility in having them, or is
it because the current mechanism of implementaing virtual functons
would not work with that concept?

Assuming that a compiler doesnot support "export" keyword, I guess it
is necessary that all instantiations of the member template must get
the definition of the template (and hence the definition of the class
of which it is a member) at compile time. In such a case, it is
certainly not impossible for the linker to combine up the vtables to
generate a final entry (it is no different than combining individual
symbol tables, geneate a new one and resolve references.)

It's not possible as all possible instantiations of the template would
have to be known when the class was compiled.

Ian
Nov 11 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Martin | last post: by
2 posts views Thread by Amadeus W. M. | last post: by
5 posts views Thread by dj | last post: by
2 posts views Thread by Heinz Ketchup | last post: by
7 posts views Thread by Markus Svilans | last post: by
11 posts views Thread by mathieu | last post: by
13 posts views Thread by Mike -- Email Ignored | last post: by
5 posts views Thread by want.to.be.professer | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.