* Rakesh Sinha:
Jonathan Turkanis wrote: puzzlecracker wrote: Can the template method be virtual
You can't have a member function template which is virtual.
and what are the consequences?
The penalty for declaring a member function template virtual is
death.
Why is it so ?
Templated virtual functions need whole-program analysis and code
generation in the link phase, because a templated virtual function
(if it existed in C++) is short for an _infinite_ potential set of
virtual functions, which can only be handled properly by backpatching
when the full set of actual instantiations is known.
That is very impractical with dynamically loaded libraries.
Dynamic libraries are not supported by the standard, but since they
are very common the standard cannot stand in the way. So instead of
imposing special, costly requirements, the standard is mostly silent.
The only place I know that the standard goes out of its way to support
dynamic libraries is in the wording of initialization of statics.
I have this code that compiles perfectly (and behaves, as expected ??)
/**
Can the template method be virtual and what are the consequences?
Can the template inline functions be virtual and what are the
consequences
**/
#include <iostream>
using std::cout;
using std::endl;
template <typename T>
class A {
public:
virtual ~A<T>() { }
This is not a template virtual function. It's a virtual function
in a template class, i.e. a _single_ virtual function in that class,
as opposed to an infinite set of potential such functions. Within
the template definition 'A' and 'A<T>' are interchangable, I think,
but I'd have to scrutinize the standard to be 100% sure that a
constructor or destructor can be specified that way.
--
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?