Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

templates and virtual functions

177 views
Skip to first unread message

Ralf Fassel

unread,
Jan 25, 2008, 12:54:41 PM1/25/08
to
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)'

TIA
R'

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

peter koch larsen

unread,
Jan 25, 2008, 8:37:46 PM1/25/08
to
On 25 Jan., 18:54, 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";};

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)'

--

int...@gmail.com

unread,
Jan 25, 2008, 8:39:54 PM1/25/08
to
On Jan 25, 8:54 pm, Ralf Fassel <ralf...@gmx.de> wrote:
> Is it possible to have templates in a base class which are 'virtual'?

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.

Daniel Krügler

unread,
Jan 26, 2008, 1:06:08 AM1/26/08
to
On 25 Jan., 18:54, 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)'

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

Alberto Ganesh Barbati

unread,
Jan 26, 2008, 1:08:04 AM1/26/08
to
Ralf Fassel ha scritto:

> Is it possible to have templates in a base class which are 'virtual'?
>

No. 14.5.2/3 says that explicitly: "A member function template shall not
be virtual."

Ganesh

aaragon

unread,
Jan 26, 2008, 5:03:37 AM1/26/08
to
{ Edits: quoted signature and clc++m banner removed, please don't quote
them. Tip: modern newsreader programs (except Google's web-based
interface) remove properly formatted signatures automatically. -mod }

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.

Mathias Gaunard

unread,
Jan 26, 2008, 5:06:14 AM1/26/08
to
On 26 jan, 02:37, peter koch larsen <peter.koch.lar...@gmail.com>
wrote:

> > 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.

Greg Herlihy

unread,
Jan 26, 2008, 8:49:03 AM1/26/08
to
On Jan 25, 10:06 pm, "Daniel Krügler" <daniel.krueg...@googlemail.com>
wrote:

> On 25 Jan., 18:54, Ralf Fassel <ralf...@gmx.de> wrote:
> > 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)'
>
> Besides two others, I hope (Missing return value and
> invalid trailing semicolon after member function
> definitions).

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

Daniel Krügler

unread,
Jan 26, 2008, 2:24:00 PM1/26/08
to
On 26 Jan., 14:49, Greg Herlihy <gre...@mac.com> wrote:
> 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++.

<nod> right and thanks for the correction Greg!

Greetings from Bremen,

Daniel Krügler


Mathias Gaunard

unread,
Jan 26, 2008, 2:26:39 PM1/26/08
to
On 26 jan, 11:03, aaragon <alejandro.ara...@gmail.com> wrote:

> 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?

0 new messages