473,404 Members | 2,114 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,404 software developers and data experts.

virtual functions and templates

Hi,

Is there an extra overhead in using virtual functions and templates?
Or is is just the same overhead as with regular classes?

Either way, if you'd want to avoid it, will you always end up
with code duplication?

For example: let's say you've got two template specializations, which
differ only slightly... So you put the common behaviour in a
base class...
This is fine, but if a "common" method needs to call a "specialized" one,
it seems to me you're always going to need polymorphism...

The only way around seems to be not creating the base class, and
just copying the whole class to specialize it...

Is there a third possibility without the disadvantages of these two?

Thanks,

Hans
Jul 19 '05 #1
5 4464
On Wed, 05 Nov 2003 11:50:26 GMT, SainTiss <st***@gmx.net> wrote:
Hi,

Is there an extra overhead in using virtual functions and templates?
Sort of - each instantiation will get its own vtable and RTTI, etc.,
and this can lead to quite a lot of code bloat when there are a large
number of different instantiations of the template.
Or is is just the same overhead as with regular classes?
Well, it is the same overhead as if you wrote all of the equivalent
non-template code. e.g.

class foo_int
{ //...
};

class foo_double
{ //...
};

etc.

Either way, if you'd want to avoid it, will you always end up
with code duplication?
No, there are techniques to avoid code duplication, the "curiously
recurring template pattern" being one. Some form of parametrised
inheritence is the key, anyway.

For example: let's say you've got two template specializations, which
differ only slightly... So you put the common behaviour in a
base class...
This is fine, but if a "common" method needs to call a "specialized" one,
it seems to me you're always going to need polymorphism...
Yes, but you can use compile-time polymorphism. e.g.

template <class Derived>
class CommonStuff
{
// non-virtual functions, casting this
// to a Derived* if necessary
};

template <class T>
class MyDerived: public CommonStuff<MyDerived<T> >
{
//add/change whatever you like
};

template<>
class MyDerived<int>: public CommonStuff<MyDerived<int> >
{
//add whatever you like
};

//and even
class Special: public CommonStuff<Special>
{
//...
};

Another alternative:

template <class T>
class CommonStuff
{
// non-virtual functions, casting this
// to a Derived* if necessary
};

template <class T>
class MyDerived: public CommonStuff<T>
{
//add/change whatever you like
};

//CommonStuff<int> not specialised
template<>
class MyDerived<int>: public CommonStuff<int>
{
//add whatever you like
};

The only way around seems to be not creating the base class, and
just copying the whole class to specialize it...
Avoid this if possible - it's a maintainance nightmare.
Is there a third possibility without the disadvantages of these two?


One of the above might be suitable.

Tom
Jul 19 '05 #2
tom_usenet wrote:
Another alternative:

template <class T>
class CommonStuff
{
// non-virtual functions, casting this
// to a Derived* if necessary
};

template <class T>
class MyDerived: public CommonStuff<T>
{
//add/change whatever you like
};

//CommonStuff<int> not specialised
template<>
class MyDerived<int>: public CommonStuff<int>
{
//add whatever you like
};


I think I get the first alternative, but this one puzzles me a bit... How
can the CommonStuff class cast to a Derived* if it doesn't know Derived? It
only knows T from what I can see...

So how exactly is this supposed to work then?

Thanks,

Hans

Jul 19 '05 #3
On Wed, 05 Nov 2003 22:41:03 GMT, SainTiss <st***@gmx.net> wrote:
tom_usenet wrote:
template <class T>
class CommonStuff
{
// non-virtual functions, casting this
// to a Derived* if necessary
};
I think I get the first alternative, but this one puzzles me a bit... How
can the CommonStuff class cast to a Derived* if it doesn't know Derived? It
only knows T from what I can see...

So how exactly is this supposed to work then?


Copy and paste error - ignore that comment!

Tom
Jul 19 '05 #4
tom_usenet wrote:
On Wed, 05 Nov 2003 22:41:03 GMT, SainTiss <st***@gmx.net> wrote:
tom_usenet wrote:
template <class T>
class CommonStuff
{
// non-virtual functions, casting this
// to a Derived* if necessary
};

I think I get the first alternative, but this one puzzles me a bit... How
can the CommonStuff class cast to a Derived* if it doesn't know Derived?
It only knows T from what I can see...

So how exactly is this supposed to work then?


Copy and paste error - ignore that comment!

Tom


Ok, but ignoring that comment, I don't really see how this is alternative is
going to avoid polymorphism? How can the CommonStuff class call a
specialized method if it doesn't know about the Derived class and can't use
polymorphism?

Thanks,

Hans
Jul 19 '05 #5
On Thu, 06 Nov 2003 16:47:16 GMT, SainTiss <st***@gmx.net> wrote:
tom_usenet wrote:
On Wed, 05 Nov 2003 22:41:03 GMT, SainTiss <st***@gmx.net> wrote:
tom_usenet wrote:
template <class T>
class CommonStuff
{
// non-virtual functions, casting this
// to a Derived* if necessary
};

I think I get the first alternative, but this one puzzles me a bit... How
can the CommonStuff class cast to a Derived* if it doesn't know Derived?
It only knows T from what I can see...

So how exactly is this supposed to work then?


Copy and paste error - ignore that comment!

Tom


Ok, but ignoring that comment, I don't really see how this is alternative is
going to avoid polymorphism? How can the CommonStuff class call a
specialized method if it doesn't know about the Derived class and can't use
polymorphism?


It does know about the derived class if there is only one derived
class:

template <class T>
struct Derived;

template <class T>
struct Base
{
int f()
{
return static_cast<Derived<T>*>(this)->g() + 1;
}
};

template <class T>
struct Derived: public Base<T>
{
int g()
{
return 10;
}
};

template <>
struct Derived<int> : public Base<int>
{
int g()
{
return 3000;
}
};

#include <iostream>

int main()
{
Derived<double> d;
std::cout << d.f() << '\n';
Derived<int> i;
std::cout << i.f() << '\n';
}

The more general solution (for multiple derived classes) is the other
one I presented.

Tom
Jul 19 '05 #6

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

Similar topics

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...
7
by: Vyacheslav Lanovets | last post by:
Hello, All! One of our target platforms has only 32 MB of virtual memory (Windows CE), so we decided to explicitly load some of our dlls. But the classes created inside such dlls are created...
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...
5
by: alind | last post by:
I m having a problem with templates. A per C++ std says i cant use override template virtual functions. in other words virtual functions cant be templates due to some vtable size issues. Now i want...
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.