By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

templates vs. virtual methods

P: n/a
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 :)

Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
an************@yahoo.com wrote:
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
Jul 23 '05 #2

P: n/a
Great! I think that suits my needs... Thanks.

Jul 23 '05 #3

P: n/a
an************@yahoo.com wrote:
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
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Jul 23 '05 #4

P: n/a
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!

Jul 23 '05 #5

P: n/a
classes to have a method I want (like above). Good style or bad?


Requiring classes to have specific methods available: good.

For more information on generic programming I recommend Alexandrescu's
book, Modern C++ Design.

http://www.amazon.com/exec/obidos/AS...eeducation-20/

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Jul 23 '05 #6

P: n/a
Great! Thank you.

Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.