[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Do C++ Programmers Overuse Templates?

tonytech08

12/4/2008 6:36:00 PM

C++ templates have grown to be language within a language, at least
for some users. Do most programmers overuse templates? Given that C++
template techniques, and some of them are quite intricate, can lock in
an entire codebase (that is, require a ground-up re-implementation if
wanting to use another, perhaps new, language), is extreme template
use wrought with peril in the long term view of things? Is a policy
that requires "judicious" use of templates the way to go?

118 Answers

peter koch

12/4/2008 6:53:00 PM

0

On 4 Dec., 19:36, tonytech08 <tonytec...@gmail.com> wrote:
> C++ templates have grown to be language within a language, at least
> for some users. Do most programmers overuse templates? Given that C++
> template techniques, and some of them are quite intricate, can lock in
> an entire codebase (that is, require a ground-up re-implementation if
> wanting to use another, perhaps new, language), is extreme template
> use wrought with peril in the long term view of things? Is a policy
> that requires "judicious" use of templates the way to go?

Why do you care about portability to another language? If you must
port, you will have to rewrite the application anyway.
That said, there might be someone that abuse templates - perhaps when
wanting to write generic code whenever it is not really needed, or who
write to complex code when some other means, such as producing code
from a small script/C++ program would do the trick.
But I am not so sure that template-code is so widely used - perhaps
the problem is that templates in real life are used to little? I
believe that this is a more or equally frequent problem.

/Peter

Jeff Schwab

12/4/2008 6:54:00 PM

0

tonytech08 wrote:
> C++ templates have grown to be language within a language, at least
> for some users. Do most programmers overuse templates? Given that C++
> template techniques, and some of them are quite intricate, can lock in
> an entire codebase (that is, require a ground-up re-implementation if
> wanting to use another, perhaps new, language), is extreme template
> use wrought with peril in the long term view of things? Is a policy
> that requires "judicious" use of templates the way to go?

No. If anything, templates are severely underutilized.

Puppet_Sock

12/4/2008 7:43:00 PM

0

On Dec 4, 1:36 pm, tonytech08 <tonytec...@gmail.com> wrote:
> C++ templates have grown to be language within a language, at least
> for some users. Do most programmers overuse templates? Given that C++
> template techniques, and some of them are quite intricate, can lock in
> an entire codebase (that is, require a ground-up re-implementation if
> wanting to use another, perhaps new, language), is extreme template
> use wrought with peril in the long term view of things? Is a policy
> that requires "judicious" use of templates the way to go?

You can write bad code in any language. Any guy who
walks into the computer lab can write bad code.

If you use a tool correctly, you can be productive.
A sharp knife, used properly, will cause you to cut
yourself much less often than a dull knife.

It is not necessarily obvious what good use of
templates looks like. A good start is the "Effective"
series by Scott Meyers. Other good books are also
out there. Check with www.accu.org, or this news
group, for more advice.
Socks

Jeff Schwab

12/4/2008 8:15:00 PM

0

Puppet_Sock wrote:

> It is not necessarily obvious what good use of
> templates looks like. A good start is the "Effective"
> series by Scott Meyers. Other good books are also
> out there. Check with www.accu.org, or this news
> group, for more advice.

+1 Modern C++ Design, by Andrei Alexandrescu. It's not new anymore, but
it's a classic. Some of the "advanced" techniques Andrei suggested are
now absurdly easy to use, thanks to MPL, Fusion, etc. Policy-based
design is easily implemented without any third-party libraries, is
widely applicable, and is just so graceful and powerful that alternative
solutions to the same problems look sadly cumbersome by comparison.

Sam

12/4/2008 11:35:00 PM

0

tonytech08 writes:

> C++ templates have grown to be language within a language, at least
> for some users. Do most programmers overuse templates? Given that C++
> template techniques, and some of them are quite intricate, can lock in
> an entire codebase (that is, require a ground-up re-implementation if
> wanting to use another, perhaps new, language), is extreme template

Can you give an example of C++ code that does not use templates, and does
not require "a ground-up reimplementation" in order to be converted to
another platform. I'd like to know what kind of C++ code can be touched up,
perhaps with a handful of global search/replace operations, and be suddenly
transform into, say, valid Java code.

> use wrought with peril in the long term view of things? Is a policy
> that requires "judicious" use of templates the way to go?

Templates are a powerful aspect of the language. Properly used, they give
C++ a unique advantage that is not found in other languages.

Paavo Helde

12/5/2008 1:07:00 AM

0

tonytech08 <tonytech08@gmail.com> kirjutas:

> C++ templates have grown to be language within a language, at least
> for some users. Do most programmers overuse templates? Given that C++
> template techniques, and some of them are quite intricate, can lock in
> an entire codebase (that is, require a ground-up re-implementation if
> wanting to use another, perhaps new, language), is extreme template
> use wrought with peril in the long term view of things? Is a policy
> that requires "judicious" use of templates the way to go?

Sure, some people overuse templates. A have seen this, a sure sign is
when the library takes more time to compile than all the other libraries
together, and the next compiler version fails with an ICE on its 7-level
cascaded templates (I have seen that too).

This said, I think this is more an exception than a rule. I believe most
C++ programmers are not knowing templates enough and are thus shying away
from them (which is a good thing; bad things happen when one thinks he
knows templates enough). So "judicious use" is probably OK, assuming one
understands a bit about the engineering aspects of software development
(referring to the recent mammoth thread, in no wish to resurrect it).

Converting a heavily templated C++ project to another language would be
extremely hard (maybe with the exception of D?), but I cannot see any
sound reason for undertaking such an effort. There is nothing which could
not be coded or interfaced in C++, so there should be never be any
technical reason to convert an existing C++ project to another language.

Paavo

dertopper

12/5/2008 8:25:00 AM

0

On 4 Dez., 19:36, tonytech08 <tonytec...@gmail.com> wrote:
> C++ templates have grown to be language within a language, at least
> for some users. Do most programmers overuse templates? Given that C++
> template techniques, and some of them are quite intricate, can lock in
> an entire codebase (that is, require a ground-up re-implementation if
> wanting to use another, perhaps new, language), is extreme template
> use wrought with peril in the long term view of things? Is a policy
> that requires "judicious" use of templates the way to go?

That really depends on your project. I have to admit that even though
I'm programming C++ for the last ten years, and also had my share of
template programming (M$ ATL for COM requires some knowledge of
templates), I'm still a bit cautious when it comes to using template-
based libraries like boost.

I think that templates are the most advanced concept of C++, and thus
-- as probably many in this thread will agree to -- it is also one of
the least understood feature when you consider the majority of C++
programmers. That's why I'd agree with the term "judicious use": After
I introduced too heavy template programming in my current project, I
lost one of my team members (he is about 50, so he learned programming
quite a while ago). That's a bad thing, but I would trade the MS ATL
library for MS MFC when it comes to COM programming. I see it with a
bit of concern that many open source projects require template-ridden
libraries like boost without the least thought that this could turn
off many programmers (which may not be skilled enough to understand
templates but could nevertheless contribute a lot), but I guess there
were similiar feelings when the open source community moved from
procedural programming to object-oriented programming.

Regards,
Stuart

tonytech08

12/5/2008 9:03:00 AM

0

On Dec 5, 2:25 am, dertop...@web.de wrote:
> On 4 Dez., 19:36, tonytech08 <tonytec...@gmail.com> wrote:
>
> > C++ templates have grown to be language within a language, at least
> > for some users. Do most programmers overuse templates? Given that C++
> > template techniques, and some of them are quite intricate, can lock in
> > an entire codebase (that is, require a ground-up re-implementation if
> > wanting to use another, perhaps new, language), is extreme template
> > use wrought with peril in the long term view of things? Is a policy
> > that requires "judicious" use of templates the way to go?
>
> That really depends on your project.

OK, you "answered" just the last question.

> I have to admit that even though
> I'm programming C++ for the last ten years, and also had my share of
> template programming (M$ ATL for COM requires some knowledge of
> templates), I'm still a bit cautious when it comes to using template-
> based libraries like boost.

Well that library may be a case in point! (Good work Sherlock!).

>
> I think that templates are the most advanced concept of C++,

That may be another case in point! The concept can be VERY easy. Or
VERY difficult. (Examples of the latter: "partial specialization",
"derived from object "inherits" derived object's behavior").

> and thus
> -- as probably many in this thread will agree to -- it is also one of
> the least understood feature when you consider the majority of C++
> programmers.

"least understood"? How difficult should "genericity" be? (Apparently
I just indicted it. Oh well).

> That's why I'd agree with the term "judicious use"

Are you confusing the concept with the C++ implementation? Cuz even
without the idiosynchracies of the C++ implementation of genericity, I
(let it be now "divulged") think that judicious use (read, requires
explicit coding standard) is appropriate.

> : After
> I introduced too heavy template programming in my current project, I
> lost one of my team members (he is about 50, so he learned programming
> quite a while ago).

Like I suggested: templatism can become a realm all itself.

> That's a bad thing

Did you tell him "C++" and not say more correctly "Template C++" so he
left for not wanting to be part of that DIALECT of C++?

> I see it with a
> bit of concern that many open source projects require template-ridden
> libraries like boost without the least thought that this could turn
> off many programmers

You took the words right out of my mouth with your true-life
experience you noted.

>(which may not be skilled enough to understand
> templates

Ha! But you "get it" now, don't you? It is unnecessary and
undesireable to learn "languages within languages" ad infinitum. It's
not a non-understanding of the idiosynchratic/complex/etc "feature",
but rather a practical decision to not "bite" on it.

tonytech08

12/5/2008 9:16:00 AM

0

On Dec 4, 12:52 pm, peter koch <peter.koch.lar...@gmail.com> wrote:
> On 4 Dec., 19:36, tonytech08 <tonytec...@gmail.com> wrote:
>
> > C++ templates have grown to be language within a language, at least
> > for some users. Do most programmers overuse templates? Given that C++
> > template techniques, and some of them are quite intricate, can lock in
> > an entire codebase (that is, require a ground-up re-implementation if
> > wanting to use another, perhaps new, language), is extreme template
> > use wrought with peril in the long term view of things? Is a policy
> > that requires "judicious" use of templates the way to go?
>
> Why do you care about portability to another language?

Because I am very practical and can distinguish valuable concepts from
fads. (Obvious analogy: C++ templates, bell-bottomed pants?).

> If you must
> port, you will have to rewrite the application anyway.

To what degree was/is the question. Even at the assembly language
instructions, there is a least common denominator. But to choose to go
the other way?

> That said, there might be someone that abuse templates

Well let me "spill the beans": I WAS suggesting that templates ARE
overused and probably by the unitiated. (The follow up question
becomes apparent: Are C++ templates over-promoted?).

> - perhaps when
> wanting to write generic code whenever it is not really needed,

Which begs the question: When are templates needed? Books have been
written on templates. A small corner case that has been exploded into
undue significance?

> But I am not so sure that template-code is so widely used

Boost, and other template-centric exploits. Enamoration with
templates: good or bad or ugly?

> - perhaps
> the problem is that templates in real life are used to little?

Perhaps one criteria of "goodness of code" is how minimally templates
are used?

tonytech08

12/5/2008 9:25:00 AM

0

On Dec 4, 5:34 pm, Sam <s...@email-scan.com> wrote:
>  application_pgp-signature_part
> < 1KViewDownload
>
> tonytech08 writes:
> > C++ templates have grown to be language within a language, at least
> > for some users. Do most programmers overuse templates? Given that C++
> > template techniques, and some of them are quite intricate, can lock in
> > an entire codebase (that is, require a ground-up re-implementation if
> > wanting to use another, perhaps new, language), is extreme template
>
> Can you give an example of C++ code that does not use templates, and does
> not require "a ground-up reimplementation" in order to be converted to
> another platform.

Surely you can see that heavily templatized C++ code is "dead" as
compared to code written with common concepts such as calling a
function. Are you an extremist or a propagandist? (I can help you with
the first if it is an affliction. You're on your own for the latter,
should it be true).