473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Is there any strong reason for not using Templates?

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
28 2586
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
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
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
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
"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
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

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

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

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

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

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

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
"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
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
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
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
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
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
NewToCPP wrote :
Is it different when we use Templates?
Yes, you have to provide the implementation in the headers.
Jul 27 '06 #23
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
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

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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

129
by: Torbjørn Pettersen | last post by:
I've started cleaning up my HTML and implementing CSS. So far I've used FrontPage, but am switching over to DreamWeaver. Reading a bit on W3Schools.com and W3.org I see there are a lot of HTML...
10
by: Tony Jones | last post by:
Can anyone think of a reason why a 3rd party vendor writing .NET components would NOT strong name their assemblies? What harm does adding a strong-name to assembly present - I would think none...
20
by: Razzie | last post by:
Hey all, I'm really going through a small hell right now - I've completely lost it :) I made a project, using two interop libraries from exchange (created them as in this msdn article:...
11
by: Vincent Finn | last post by:
Hi, Is there any way of having a strongly typed dynamic array in C#? ArrayList requires you to cast every time you want to access an element object isn't dynamic I have resorted writing a...
6
by: Manuel Lopez | last post by:
Hello, I have a Web Project (UserControls.dll) with some user controls that is shared by many asp.net web applicattions. What we do is copy UserControls.dll to all the applications bin...
1
by: Tim F | last post by:
Problem: I'm receiving the error "File or assembly name XXXXX or one of its dependencies, was not found." when trying to execute code in an assmebly that has both a strong-name and has been...
6
by: raylopez99 | last post by:
Anybody use Strong Name Signing? I think this is used by default for Resource files, which is one reason perhaps I can't get my resource files to work (somehow the public key is messed up, perhaps...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.