Is there any strong reason for not using Templates?

L

loufoque

NewToCPP wrote :
So... is it safe to use template classes/functions all the time. So
that incase infuture if I need support for multiple types I dont have
to change anything. The code that we have already tested and it works.

Just don't expect to make it work correctly with old compilers like MSVC6.
 
C

Chris

You may find that extremely large projects without pimpl
implementations will have a serious compile slowdown when using
templates.
 
I

Ian Collins

NewToCPP said:
So... is it safe to use template classes/functions all the time. So
that incase infuture if I need support for multiple types I dont have
to change anything. The code that we have already tested and it works.
Safe, but there are reasons why you might not. One that springs to mind
is some compilers require the declaration and definition of templates to
be in the same fire, which might not appeal. You may also run into
problems when building libraries, depends on your platform.

If you have good unit tests for your classes, transforming them form a
concrete class to a class template when this is required should be a
pretty straight forward operation. Don't do work you might never need.
 
R

Roland Pibinger

So... is it safe to use template classes/functions all the time. So
that incase infuture if I need support for multiple types I dont have
to change anything. The code that we have already tested and it works.

It is 'safe' but not recommendable. You usually don't write your own
templates in your application (you may use e.g. some generic
containers though). Moreover, writing (designing) generic components
is hard. Do you really have a problem that can be solved by re-using
the same implementation for different types?

Best wishes,
Roland Pibinger
 
K

kwikius

NewToCPP said:
What is the negative effect of using templates instead of a simple
class/function...

1. Does it increase compile time?
2. Does it reduce the application performance?
3. Is executable going to be bigger?

Don't use templates, and then you are *guaranteed* to have no negative
effects, of course you won't get any positive effects either.... but
AFAICS you are only really looking for a reason to justify that using
templates is BAD arent you?

regards
Andy Little
 
M

marius lazer

Victor said:
It might. Then again, is it important? Is it noticeable?

It will. Depending on your usage of templates and your compiler it can
be considerable (10-100 tims slower and with a poor compiler could be
much worse). But don't get me wrong, templates are a good thing and I
encourage thier use.

Marius
 
C

Chris

This also brings up an interesting point of distributing a DLL/Interop
vs C++ template library. I have seen far too many people distributing
templated class libraries where a DLL/COM or some other method is best
suited and opens up usage from languages other than C++.
Distributing templated code for multi-use library code is great, but if
your code is intended for a specific purpose then perhaps distributing
it as templated C++ code is not such as good idea. Again, if your
company does not like publishing its source (and most don't) this may
be a major factor.
 
N

NewToCPP

Andy:

That was really a good observation. I dont use Templates.. Now I am
wondering why should not I use them... Just trying to see if there are
any reasons not to use them and see if any one of them apply to me.. if
not I want to start using Templates.
 

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
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top