virtual templates

P

puzzlecracker

Can the template method be virtual and what are the consequences?
Can the template inline functions be virtual and what are the
consequences?
 
J

Jonathan Turkanis

puzzlecracker said:
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.
Can the template inline functions be virtual

virtual can only be applied to non-static member functions.
and what are the
consequences?

Death here too. (C++ is strict.)

J
 
R

Rakesh Sinha

Jonathan said:
You can't have a member function template which is virtual.


The penalty for declaring a member function template virtual is
death.

Why is it so ? Can you explain a little bit more.

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>() { }
virtual void printMe() {
cout << "I am A " << endl;
}


};


template <typename T>
class B: public A<T> {

public:
void printMe() {
cout << "I am B" << endl;
}

};

int main() {
A<int> * p = new B<int>;
p->printMe();
delete p;
}

Is there anything that I am missing here.
 
A

Alf P. Steinbach

* Rakesh Sinha:
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.
 
I

Ioannis Vranos

puzzlecracker said:
Can the template method be virtual and what are the consequences?
Can the template inline functions be virtual and what are the
consequences?


Methods of template classes can be virtual:


template <class T>
class SomeClass
{
public:
virtual void func() {}
};


template <class T>
class Derived: public SomeClass<T>
{
public:
void func() {}
};


int main()
{
SomeClass<int> sobj;

sobj.func();

Derived<int> dobj;

dobj.func();

SomeClass<int> *p= new Derived<int>;

p->func();
}



A template method of a class can't be virtual, and I guess you can
understand why:


class SomeClass
{
public:
template<class T>
virtual void func() {}
};


int main()
{
SomeClass sobj;
}


5 C:\c\temp.cpp invalid use of `virtual' in template declaration of `
virtual void SomeClass::func()'
 
I

Ioannis Vranos

Alf said:
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.


Yes. For convenience, inside the class definition you can omit the A<T>
use everywhere and replace it with A.

A<T> is the type, A doesn't make sense but is used for convenience.
 
I

Ioannis Vranos

puzzlecracker said:
not really, please explain


Because a template method is not one method, its instances are the
methods, and can have many instances.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top