A template Member inside a non templated class?

Hey geeks,
why is it not possible in C++ to have a virutal template member function in a non templated class? something like the following

1
2
3
4
5
6
7
  class A
  {
    template<class T>
    virtual void print()
    {
    }
   }
You can't have virtual template functions in any class period.

The reason is because as the compiler scans class declarations, it builds a list of
virtual function declarations that have been encountered. The first one is numbered
0 and is located at the 0th entry in the vtable. The next one is the 1st entry in
the table and so forth. When the compiler wants to call a virtual function, the code
it generates is to read the Nth index of the vtable which contains a pointer to the
function to actually call, where N is computed simply by looking at compile time at
the virtual function being called and find its index in the symbol table.

template functions can have many instantiations, and those aren't known at the
time of parsing the class declaration, therefore the compiler cannot possibly build
the vtable since it does not know how many different ways the template function
will be instantiated.
Topic archived. No new replies allowed.