473,394 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

virtual templates

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

Jul 22 '05 #1
8 2027
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.
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
Jul 22 '05 #2
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 ? 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.

Jul 22 '05 #3
* 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?
Jul 22 '05 #4
Alf P. Steinbach wrote:
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.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #5
puzzlecracker wrote:
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()'


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #6

"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message news:35*************@individual.net...
puzzlecracker wrote:
Can the template method be virtual


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


See also the discussion titled "Template virtual member method is not allowed. How to bypass that?" in news:comp.lang.c++ at
http://groups-beta.google.com/group/...f1807b5f86e87f

[snip]

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
Jul 22 '05 #7

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

not really, please explain

Jul 22 '05 #8
puzzlecracker wrote:
A template method of a class can't be virtual, and I guess you can
understand why:


not really, please explain

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


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Sebastian Faust | last post by:
Hi, I have a design problem about which I am thinking now for a while and still couldnt find any help in deja. What I need is something like a virtual function template. I know that this is not...
4
by: Sat | last post by:
Hi, I have a simplified version of a problem that I am facing (hope I haven't oversimplified it). This code doesn't work now and I want to find how I can make it work. Can I call the derived...
25
by: Stijn Oude Brunink | last post by:
Hello, I have the following trade off to make: A base class with 2 virtual functions would be realy helpfull for the problem I'm working on. Still though the functions that my program will use...
4
by: Martin | last post by:
Greetings I want to have virtual member functionality, but without my member functions being virtual:-) As of yet this is all just in my head cause I can't see a nice solution yet so lets...
6
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual...
4
by: pocmatos | last post by:
Hi all, I have an abstract class acting as interface to a given class of objects. And mostly everywhere around my program I'm passing things like: vector<int>, list<unsigned long>,...
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
7
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
11
by: mathieu | last post by:
Hi there, I don't think I'll be able to describe my issue correctly, so instead I'll just give a pseudo C++ code I am struggling with. Basically I am looking for a 'pure virtual template'...
5
by: want.to.be.professer | last post by:
For OO design, I like using virtual member function.But considering efficiency, template is better. Look at this program, class Animal { public: virtual void Walk() = 0; }; class Dog
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.