Templates and generic programming?

J

Johs

I have read that when you are using templates you are making generic
programs. But I don't see whats so generic about templates. You can make
generic programs without templates through the use of function overload and
the virtual functionality.

As I understand when you use templates you just increase the runtime
performance since the code is resolved at compile time. How come people are
mixing templates with generic programming when all they actually do is
increase runtime performance?
 
R

Roland Pibinger

I have read that when you are using templates you are making generic
programs. But I don't see whats so generic about templates.

This a common misunderstanding. Alexander A. Stepanov, creator of STL,
writes: "Some of you might have also heard that generic programming is
a style of programming using C++ templates. This is not so. Generic
programming has nothing to do with C++ or templates. Generic
programming is a discipline that studies systematic organization of
abstract software components"
(http://www.stepanovpapers.com/MusserForeward.pdf).
You can make
generic programs without templates through the use of function overload and
the virtual functionality.
As I understand when you use templates you just increase the runtime
performance since the code is resolved at compile time.

Function overloading is also resolved at compile time. Late binding
may be an advantage or a disadvantage, depending on the context.
How come people are
mixing templates with generic programming when all they actually do is
increase runtime performance?

Better performance is always, well, better. In general I agree with
you. Templates are currently overrated in C++.
 
Z

Zeppe

Johs said:
As I understand when you use templates you just increase the runtime
performance since the code is resolved at compile time. How come people are
mixing templates with generic programming when all they actually do is
increase runtime performance?

Increase the runtime performance and decrease the runtime errors, given
that the issues regarding the binding are solved at compile-time.
Template and virtual are different features that provide different
functionalities. For example, how would you do a generic std::map with
virtual class? A generic MapObject* with some virtual constructor,
assignment operator and some sorting function to be possibly given as an
external parameter during the creation of the container?

It's not just a matter of performance.

Regards,

Zeppe
 
J

Jon Harrop

Johs said:
I have read that when you are using templates you are making generic
programs. But I don't see whats so generic about templates. You can make
generic programs without templates through the use of function overload
and the virtual functionality.

As I understand when you use templates you just increase the runtime
performance since the code is resolved at compile time. How come people
are mixing templates with generic programming when all they actually do is
increase runtime performance?

Using templates can also result in more static checking because types are
resolved at compile time. Parametric polymorphism is the better solution
used in most modern languages.
 

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
474,294
Messages
2,571,511
Members
48,198
Latest member
couriermedicine

Latest Threads

Top