Is there any strong reason for not using Templates?

N

NewToCPP

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.
 
L

loufoque

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.
 
N

NewToCPP

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.
 
N

NewToCPP

Why does

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

Ian Collins

NewToCPP said:
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.
 
O

osmium

NewToCPP said:
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,
 
S

shadowman615

osmium said:
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?
 
V

Victor Bazarov

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
 
S

shadowman615

Victor said:
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.
 
N

Noah Roberts

Victor said:
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.
 
N

Noah Roberts

NewToCPP said:
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.
 
N

NewToCPP

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?
 
R

Roland Pibinger

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
 
N

NewToCPP

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.
 
N

Noah Roberts

NewToCPP said:
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.
 
V

Victor Bazarov

NewToCPP said:
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
 
N

NewToCPP

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?
 
N

NewToCPP

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.
 
H

Howard Gardner

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.

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.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top