templates vs. virtual methods

A

andreykuzmenko

I'm thinking about using templates instead of virtual methods to boost
the performance of my library. I would like to know if it is possible
to do something like the following:

template<class T>
void just_do_it(T* t)
{
t->do_it();
}

Of course we do not know whether class T has do_it() method or not. But
will this work out? For the following example it would be cool if it
would generate compile error if MyClass doesn't have do_it() method,
and compile correctly if it has:

MyClass myc = new MyClass();
just_do_it<MyClass>(my);

So, is it possible? If not, then why? I think it would be very nice :)
 
V

Victor Bazarov

I'm thinking about using templates instead of virtual methods to boost
the performance of my library. I would like to know if it is possible
to do something like the following:

template<class T>
void just_do_it(T* t)
{
t->do_it();
}

Of course we do not know whether class T has do_it() method or not. But
will this work out?

If 'T' has no 'do_it', then the compilation will fail.
For the following example it would be cool if it
would generate compile error if MyClass doesn't have do_it() method,
and compile correctly if it has:

MyClass myc = new MyClass();
just_do_it<MyClass>(my);

So, is it possible? If not, then why? I think it would be very nice :)

Yes, it is possible. Just try it and you'll see.

V
 
J

Jonathan Bartlett

I'm thinking about using templates instead of virtual methods to boost
the performance of my library. I would like to know if it is possible
to do something like the following:

Templates and virtual methods do very different things. I have rarely
seen a situation in which both were able to accomplish the same goal.
template<class T>
void just_do_it(T* t)
{
t->do_it();
}

Why wouldn't you just call t->do_it()? If the types are known at
compile-time, then t->do_it() would "just work", and if they aren't,
then t->do_it() will still required a virtual method lookup.

Jon
 
A

andreykuzmenko

I am implementing an algorithm, which is general for various types of
entities (neurons in neural network). And I would like to easily reuse
this algorithm for the new types of neurons in the future. I would like
my library be similar to STL, but with its own algorithtms. So,
t->do_it() will work of course, but my purpose is to create a library
of algorithtms. I already have a working version with virtual methods
(in Delphi), but it's now clear for me that the additional level of
abstraction I have is unnecessary because I'm not really in a situation
when the entities (neurons) may be replaced dynamically. Thank you! I
would also like to slightly change my initial question:

Is it _normal_ to do something like the following? Isn't it a bad
_style_ of using C++?

template<class T>
void just_do_it(T* t)
{
t->do_it();
}

For example, STL algorithms require container items to have operators
, <, = implemented right. So I suppose I can require my template
classes to have a method I want (like above). Good style or bad?

It seems to me that generic programming requires more thorough studying
than I did before. For example, the code above is somehow similar to
requiring interface implementation without having the interface itself.
This is really interesting alternative... Wow!
 

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

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,651
Latest member
VeraPiw932

Latest Threads

Top