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

Is there any strong reason for not using Templates?

P: n/a
Hi,

I am just trying to find out if there is any strong reason for not
using Templates. When we use Templates it is going to replicate the
code for different data types, thus increasing the executable size.
This should not cause any performance issue.

So, is it safe to say that if I can offered to have bigger image size I
can go ahead and use Templates with out worrying about any other
issues?

Thanks.

Jul 26 '06 #1
Share this Question
Share on Google+
28 Replies


P: n/a
NewToCPP wrote :
I am just trying to find out if there is any strong reason for not
using Templates.
There is none.

So, is it safe to say that if I can offered to have bigger image size I
can go ahead and use Templates with out worrying about any other
issues?
It won't be bigger.
Because if you wanted to achieve the same functionality without
templates you would have to write a function for each type you need,
which would have the same result.

Jul 26 '06 #2

P: n/a
The following is what I got from Wikipedia
Advantages and disadvantages
==========================
Some uses of templates, such as the max() function, were previously
filled by function-like preprocessor macros (a legacy of the C
programming language). For example, here is a possible max() macro:

#define max(a,b) ((a) < (b) ? (b) : (a))

Both macros and templates are expanded at compile time. Macros are
always expanded inline; templates can also be expanded as inline
functions when the compiler deems it appropriate. Thus both
function-like macros and function templates have no run-time overhead.

However, templates are generally considered an improvement over macros
for these purposes. Templates are type-safe. Templates avoid some of
the common errors found in code that makes heavy use of function-like
macros. Perhaps most importantly, templates were designed to be
applicable to much larger problems than macros.

There are three primary drawbacks to the use of templates: compiler
support, poor error messages, and code bloat. Many compilers
historically have very poor support for templates, so the use of
templates can make code somewhat less portable. Support may also be
poor when a C++ compiler is being used with a linker which is not
C++-aware, or when attempting to use templates across shared library
boundaries. Most modern compilers though now have fairly robust and
standard template support.

Almost all compilers produce confusing, long, or sometimes unhelpful
error messages when errors are detected in code that uses templates.
This can make templates difficult to develop.

Finally, the use of a templates may cause the compiler to generate
extra code (an instantiation of the template), so the indiscriminate
use of templates can lead to code bloat, resulting in excessively large
executables. However, judicious use of template specialization can
dramatically reduce such code bloat in some cases. The extra
instantiations generated by templates can also cause debuggers to have
difficulty working gracefully with templates. For example, setting a
debug breakpoint within a template from a source file may either miss
setting the breakpoint in the actual instantiation desired or may set a
breakpoint in every place the template is instantiated.

Jul 27 '06 #3

P: n/a
Why does

"Almost all compilers produce confusing, long, or sometimes unhelpful
error messages when errors are detected in code that uses templates."

Jul 27 '06 #4

P: n/a
NewToCPP wrote:
Why does

"Almost all compilers produce confusing, long, or sometimes unhelpful
error messages when errors are detected in code that uses templates."
That depends on the template in question. STL containers bad for this
as they may have several default template parameters, some of these
being templates. So if you you do something silly with a
std::vector<int>, the error message will include the fully specified
template.

--
Ian Collins.
Jul 27 '06 #5

P: n/a
"NewToCPP" writes:
I am just trying to find out if there is any strong reason for not
using Templates.
You have to expose your code to the world. If you have something you would
rather be private to you and your organization, you cannot do so,
Jul 27 '06 #6

P: n/a
Ian Collins wrote:
NewToCPP wrote:
Why does

"Almost all compilers produce confusing, long, or sometimes unhelpful
error messages when errors are detected in code that uses templates."
That depends on the template in question. STL containers bad for this
as they may have several default template parameters, some of these
being templates. So if you you do something silly with a
std::vector<int>, the error message will include the fully specified
template.
See also:

http://www.parashift.com/c++-faq-lit...html#faq-35.17

Cheers! --M

Jul 27 '06 #7

P: n/a

osmium wrote:
"NewToCPP" writes:
I am just trying to find out if there is any strong reason for not
using Templates.

You have to expose your code to the world. If you have something you would
rather be private to you and your organization, you cannot do so,
Could you please explain what you mean here?

Jul 27 '06 #8

P: n/a
sh**********@comcast.net wrote:
osmium wrote:
>"NewToCPP" writes:
>>I am just trying to find out if there is any strong reason for not
using Templates.

You have to expose your code to the world. If you have something you
would rather be private to you and your organization, you cannot do
so,

Could you please explain what you mean here?
If I may... Most implementations require template code to be placed in
the headers, and supplied to the user of the templates in the _source_
form. If your company is not up to sharing their source, you have some
options, but not too many.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 27 '06 #9

P: n/a

Victor Bazarov wrote:
sh**********@comcast.net wrote:
osmium wrote:
"NewToCPP" writes:

I am just trying to find out if there is any strong reason for not
using Templates.

You have to expose your code to the world. If you have something you
would rather be private to you and your organization, you cannot do
so,
Could you please explain what you mean here?

If I may... Most implementations require template code to be placed in
the headers, and supplied to the user of the templates in the _source_
form. If your company is not up to sharing their source, you have some
options, but not too many.

V
OK, I see what he's talking about now -- thanks Victor.

Jul 27 '06 #10

P: n/a

Victor Bazarov wrote:
If I may... Most implementations require template code to be placed in
the headers, and supplied to the user of the templates in the _source_
form. If your company is not up to sharing their source, you have some
options, but not too many.
That is only an issue for library authors really. If you want to sell
libs meant to be used by other developers then you have to share the
headers, and if you have lots of R&D invested in your template code
this could be an issue for you. On the other hand, those interested in
purchasing libraries usually pay for the licenses even if they could
easily take because they want a profit and can't risk the suits.

If you write programs for sale and libraries for your own use then
templates are a non-issue in this area; you don't have to share them as
source code at all. In fact it could theoretically stop someone from
using your libs in their own projects by looking at the dll or whatever
and making headers for it....very difficult to do if much of your lib
is templated.

Jul 27 '06 #11

P: n/a

NewToCPP wrote:
Hi,

I am just trying to find out if there is any strong reason for not
using Templates. When we use Templates it is going to replicate the
code for different data types, thus increasing the executable size.
This should not cause any performance issue.

So, is it safe to say that if I can offered to have bigger image size I
can go ahead and use Templates with out worrying about any other
issues?
Reasons not to use templates:

1) you are very space limited.
2) you don't understand them.
3) your coworkers don't understand them.
4) someone told you not to.

I don't consider any but the last one to be really that valid. You can
use templates and still conserve space. You and your coworkers can
learn templates and be better for it; there are many books on the
subject. The last one though can't really be bypassed...the only thing
you can do is try and convince whatever authority did so that it would
be better if you could use them.

Jul 27 '06 #12

P: n/a
How are the libraries build by Templates are different from standard
non-template C++ libraries? Why would any one have to share the
library code?

Jul 27 '06 #13

P: n/a
On 26 Jul 2006 20:03:53 -0700, "NewToCPP" <he****@yahoo.comwrote:
>Why does
"Almost all compilers produce confusing, long, or sometimes unhelpful
error messages when errors are detected in code that uses templates."
Because "macros and templates are expanded at compile time". Templates
are a macro mechanism that is built into the language (in contrast to
preprocessor macros).
Anyone who has worked with templates can also confirm the above
mentioned drawbacks.
IMO, the important point is that one should strive for the least
effort to reach a goal (which corresponds to the definition of
elegance). A solution without templates is preferable to a templated.
Also, templates are only beneficial for some libraries (containers,
algorithms) but are hardly useful for (and used in) 'normal'
application programming.

Best regards,
Roland Pibinger
--------------------------------------------
"Stephen Hawking received a comment from his publisher during the
writing of A Brief History of Time, to the effect that every equation
he included in the book would halve the number of sales, so he
included only 'E = mc2'. A similar point has sometimes been observed
about templates: each additional generalising parameter will decrease
the number of users by half."
Kevlin Henney
Jul 27 '06 #14

P: n/a

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?

Thanks.

Jul 27 '06 #15

P: n/a

NewToCPP wrote:
How are the libraries build by Templates are different from standard
non-template C++ libraries? Why would any one have to share the
library code?
templates are not built or instantiated until used. If you provide
them in your library interface they are 100% public.

Jul 27 '06 #16

P: n/a
"NewToCPP" <he****@yahoo.comwrote...
>
What is the negative effect of using templates instead of a simple
class/function...

1. Does it increase compile time?
It might. Then again, is it important? Is it noticeable?
2. Does it reduce the application performance?
Definitely not. Usually the opposite. But without testing and measuring
it's impossible to say.
3. Is executable going to be bigger?
Usually not. Modern compilers don't generate code for the templates that
are not used.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 27 '06 #17

P: n/a
That means only the interfaces are visible ... right...

I can have my implementation of those interfaces not visible.. Thats
how we do it in regular C++ libraries...

Is it different when we use Templates?

Jul 27 '06 #18

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

Jul 27 '06 #19

P: n/a
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.
It will usually take you longer to develop a function template or class
template than it will take you to develop a plain function or class. If
you develop the template versions, you are betting some of your time:
you will win your bet only if the templates save time later.

The only thing that it is safe to do all the time is to think very
carefully about what you are doing.
Jul 27 '06 #20

P: n/a
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.
Jul 27 '06 #21

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

NewToCPP wrote:
Hi,

I am just trying to find out if there is any strong reason for not
using Templates. When we use Templates it is going to replicate the
code for different data types, thus increasing the executable size.
This should not cause any performance issue.

So, is it safe to say that if I can offered to have bigger image size I
can go ahead and use Templates with out worrying about any other
issues?

Thanks.
Jul 27 '06 #22

P: n/a
NewToCPP wrote :
Is it different when we use Templates?
Yes, you have to provide the implementation in the headers.
Jul 27 '06 #23

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

--
Ian Collins.
Jul 28 '06 #24

P: n/a
On 27 Jul 2006 14:17:21 -0700, "NewToCPP" <he****@yahoo.comwrote:
>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
Jul 28 '06 #25

P: n/a

NewToCPP wrote:
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

Jul 28 '06 #26

P: n/a

Victor Bazarov wrote:
"NewToCPP" <he****@yahoo.comwrote...

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

1. Does it increase compile time?

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

Jul 28 '06 #27

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

sh**********@comcast.net wrote:
Victor Bazarov wrote:
sh**********@comcast.net wrote:
osmium wrote:
>"NewToCPP" writes:
>>
>>I am just trying to find out if there is any strong reason for not
>>using Templates.
>>
>You have to expose your code to the world. If you have something you
>would rather be private to you and your organization, you cannot do
>so,
>
Could you please explain what you mean here?
If I may... Most implementations require template code to be placed in
the headers, and supplied to the user of the templates in the _source_
form. If your company is not up to sharing their source, you have some
options, but not too many.

V

OK, I see what he's talking about now -- thanks Victor.
Jul 31 '06 #28

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

Jul 31 '06 #29

This discussion thread is closed

Replies have been disabled for this discussion.