Do C++ Programmers Overuse Templates?

T

tonytech08

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

peter koch

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
 
P

Puppet_Sock

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
 
D

dertopper

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
 
T

tonytech08

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

tonytech08

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

tonytech08

 application_pgp-signature_part
< 1KViewDownload


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).
 
J

James Kanze

C++ templates have grown to be language within a language, at
least for some users. Do most programmers overuse templates?

What kind of a question is that? For any X, some programmers
overuse X, others don't. Without defining "most", your question
doesn't mean anything, and for any concrete definition, it's
probably unanswerable.

My experience is that most application programmers don't use
templates other than those defined in the standard library. I'd
hardly call that "overuse".
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?

Any C++ code will require a ground-up re-implementation if you
want to compile it in another language. Templates are only a
minor aspect here.
Is a policy that requires "judicious" use of templates the way
to go?

A policy which requires "judicious" use of everything is
certainly the way to go.
 
D

dertopper

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

Maybe we should differentiate between different techniques that are
used with template programming: Surely everyone will agree that the
container classes of the STL are plain and simple. It gets a bit more
complicated when you consider traits. Probably the most complicated
thing is the heavy use of SFINAE (like it is done in boost), the CRTP,
or Koenige-Lookup (which is not strictly a template-related problem
but plays a role in template programming). Maybe we should consider
policies like
"Template programming is okay, but no use of SFINAE, CRTP, and other
techniques that are beyond the common skills of our team".
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.

I'm sorry if I couldn't made myself clear (English is not my first
language). I wanted to say that I think that your idea of having a
policy that requires "judicious" use of templates is a good thing.
However, such a policy cannot be made for the whole programming
language but rather for each single project.
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++?

Well, I actually didn't ask him before it was too late. But that's not
too bad, as I can "fill in" the template-related stuff and let him
write non-template related C++ code. Besides, he is more or less only
a hobby programmer and spends more time with hardware related
problems.
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.

Absolutely. One should always keep it as simple as possible (although
I can understand that programmers who have a certain palette of
techniques tend to use this palette on projects that could have been
solved with "less complicated" tools).

Regards,
Stuart
 
G

Guest

sometimes programs have to be porrted to new languages.
Though I've normally done it with pretty obscure languages
(CORAL-66 anyone?). C++ isn't going to go away in the near future.
Because I am very practical and can distinguish valuable concepts from
fads. (Obvious analogy: C++ templates, bell-bottomed pants?).

I think you've already answerdd your question. *You* consider
C++ templates a fad and think they are over-used.
To what degree was/is the question. Even at the assembly language
instructions, there is a least common denominator.
what?

But to choose to go the other way?

the other way?
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?).

ah! you see I was right

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?


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


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

seems a rather narrow definition. So my CORAL-66 code is excellent
because it has no templates in it?
 
G

Guest

Surely you can see that heavily templatized C++ code is "dead" as
compared to code written with common concepts such as calling a
function.

what does "dead" mean? I usually try to write code that is
flexible and portable. How do C++ templates fly in the face of this?
I don't usually try to write code so it can be easily converted to
another language (I'm not sure how I'd do this!). If you really are
stuck with a mass of C++ that you need to rewrite in Python or
something (this sounds so bizzare!) can you find tools that
will expand the templates?
Are you an extremist or a propagandist?

is this your name for people who genuinely disagree with you
on a technical issue?
(I can help you with
the first if it is an affliction. You're on your own for the latter,
should it be true).


--
Nick Keighley

"XML is isomorphic to the subset of Lisp data
where the first item in a list is required to be atomic."
John McCarthy
 
J

James Kanze

Maybe we should differentiate between different techniques
that are used with template programming: Surely everyone will
agree that the container classes of the STL are plain and
simple. It gets a bit more complicated when you consider
traits. Probably the most complicated thing is the heavy use
of SFINAE (like it is done in boost), the CRTP, or
Koenige-Lookup (which is not strictly a template-related
problem but plays a role in template programming). Maybe we
should consider policies like
"Template programming is okay, but no use of SFINAE, CRTP, and
other techniques that are beyond the common skills of our
team".

Maybe that should be rephrased to say that no code should use
techniques that are beyond the reasonably expected skills of
those who will have to maintain it. Templates aren't the only
issue. And different parts of a project will (or should be)
maintained by people with different skill sets; the quants here
do things I couldn't do, and vice versa. The application level
code written by the quants probably should not define templates,
period. In the low level code I tend to work on, on the other
hand, simple use of SFINAE isn't a problem.
 
M

Matthias Buelow

Sam said:
Templates are a powerful feature
that's unique to C++, and is one of the major features of the language.
No other modern language has anything similar to offer.

JFTR, C++ templates are a special form of compiler macros, which some
other languages (Common Lisp, Scheme) have been offering for a long
time. They're also more of an ad-hoc workaround around the restrictive
C++ type system rather than a feature that has any value itself.
 
J

James Kanze

JFTR, C++ templates are a special form of compiler macros,
which some other languages (Common Lisp, Scheme) have been
offering for a long time.

They're a bit more than macros, although I'm not sure that all
of the differences are improvements. And of course, as you say,
they idea behind them is hardly new, having been part and parcel
of languages in the Lisp family for decades. (But in general, I
think the idea behind C++ isn't so much inventing new ideas, as
adapting all of the best existing ideas into a single language.)
They're also more of an ad-hoc workaround around the
restrictive C++ type system rather than a feature that has any
value itself.

And that's just bullshit. In fact, the one real problem with
C++ type system is that it isn't restrictive enough.
 
M

Matthias Buelow

James said:
And that's just bullshit. In fact, the one real problem with
C++ type system is that it isn't restrictive enough.

A common use (the most common?) of templates seems to be covering up the
lack of type variables in the C++ type system, like they exist in some
functional languages (for example, Standard ML.) Algorithmic template
metaprogramming is very ugly and tedious so I don't see this as a real
plus for templates.
 
S

Sherm Pendley

James Kanze said:
A policy which requires "judicious" use of everything is
certainly the way to go.

I would say that a "judicious" use of policy is the way to go. :)

sherm--
 
N

Noah Roberts

tonytech08 said:
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).

LOL!!!!!

Ok, guy. The only objective reason you seem to be able to come up with
to say templates are bad is questioned and you back it up with "can't
you see"??

Nothing to see here. Moving on.
 
N

Noah Roberts

Matthias said:
JFTR, C++ templates are a special form of compiler macros, which some
other languages (Common Lisp, Scheme) have been offering for a long
time. They're also more of an ad-hoc workaround around the restrictive
C++ type system rather than a feature that has any value itself.

I don't know much about the other languages' versions of this concept
and how close they match, but I certainly wouldn't call the C++ type
system /restrictive/ nor would I consider templates a /workaround/ to
this system but an *extension* of it.
 
N

Noah Roberts

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

Completely silly. Such programmers are best lost anyway. The boost
libraries may be template ridden on the inside, but very few of them
require direct interaction from the user. Implementation details that
clients and users need to concern themselves about unless they're
actually interested. Many of the things boost does are not possible
without templates.

Better get used to it too. A good portion of those "template ridden
libraries" are getting sucked into the next standard.
 
V

Vidar Hasfjord

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?

Judicious is always good.

Sorry for diverging, but I think the world would have been a better
place if C had templates from the start. C based on templates could
have made a more regular and powerful language.

C has two fundamental generic types: arrays and pointers. If C had
templates we could have been spared from the complicated special
syntax that were chosen for arrays and pointers. Quite likely the
semantics would have been more regular as well.

Where C ended up with

int a [12], b [12];
memcpy (b, a, sizeof b);
int* p = a, *u = 0;
int i = *p;
int (*foo (int key)) [12];
int j = (*foo (42)) [0]

C + templates could have given us

array [int, 12] a, b;
b = a; // copy semantics
pointer [int] p = address (a (0)), u = 0;
int i = p (); // call to dereference
pointer [array [int, 12]] foo (int key);
int j = foo (42) () (0);

Regards,
Vidar Hasfjord
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top