class base {
public:
template <class T>
int foo(T) {std::cout << "base class\n";};
};
class derived : public base {
public:
template <class T>
int foo(T) {std::cout << "derived class\n";};
};
base *x = ...;
x->foo(); // always calls base class method, never derived method
My compiler (gcc 4.1.2) complains about
class base {
public:
template <class T>
virtual int foo(T) {std::cout << "base class\n";};
};
=>
foo.h:258: error: invalid use of 'virtual' in template declaration of
'virtual int foo(T)'
TIA
R'
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
No. This is not legal. Think about it for a moment. How would you
implement the actual look-up?
/Peter
> };
> =>
> foo.h:258: error: invalid use of 'virtual' in template declaration of
> 'virtual int foo(T)'
--
No. You can have class templates which contain virtual functions, but
not virtual function templates. The latter would essentially require
run-time instantiation of function templates.
Besides two others, I hope (Missing return value and
invalid trailing semicolon after member function
definitions).
Nevertheless, it *is* allowed to have both virtual
functions and member templates as part of a class
or class template. But virtual function templates
are not supported, because the compiler could not
deduce the size of the vtable (or analogous
implementation-specific constructs) in advance.
Which use-case do you try to solve here?
Greetings from Bremen,
Daniel Krügler
No. 14.5.2/3 says that explicitly: "A member function template shall not
be virtual."
Ganesh
On Jan 25, 11:54 am, Ralf Fassel <ralf...@gmx.de> wrote:
> Is it possible to have templates in a base class which are 'virtual'?
>
> class base {
> public:
> template <class T>
> int foo(T) {std::cout << "base class\n";};
> };
> class derived : public base {
> public:
> template <class T>
> int foo(T) {std::cout << "derived class\n";};
> };
>
> base *x = ...;
> x->foo(); // always calls base class method, never derived method
>
> My compiler (gcc 4.1.2) complains about
> class base {
> public:
> template <class T>
> virtual int foo(T) {std::cout << "base class\n";};
> };
> =>
> foo.h:258: error: invalid use of 'virtual' in template declaration of
> 'virtual int foo(T)'
>
Template functions cannot be virtual. Templated code is executed at
compilation time, whereas the virtual allows you to select a different
function depending on a type at runtime. You cannot mix both.
a²
> > My compiler (gcc 4.1.2) complains about
> > class base {
> > public:
> > template <class T>
> > virtual int foo(T) {std::cout << "base class\n";};
>
> No. This is not legal. Think about it for a moment. How would you
> implement the actual look-up?
You just need to instantiate the template member function for all
classes deriving from class base whenever you instantiate it for class
base.
According the the C++ grammar, the semicolon after a member function
definition is optional. So foo()'s trailing semicolon in the example
above - is perfectly valid C++.
Greg
<nod> right and thanks for the correction Greg!
Greetings from Bremen,
Daniel Krügler
> Template functions cannot be virtual. Templated code is executed at
> compilation time, whereas the virtual allows you to select a different
> function depending on a type at runtime. You cannot mix both.
You certainly could if you generated some code at link time.
Bu then there is a problem: what about dynamic linking?