On Fri, 7 Apr 2000, Theodore Papadopoulo wrote:
>
> > I personally feel that these new techniques are elegant (from the user
> > interface standpoint) and worth study. However, I also feel, from my own
> > experience, that these methods are experimental and should not be used for
> > large, important projects at this time. Just the compilation time for the
> > templates is enough to make me shy away from them. Also, non-C++ masters
> > have to be able to maintain my code after I finish my degree so I can't
> > get too complicated.
>
> Well, while I agree with the elegance part, I disagree
> slightly with the remaining part of this paragraph....
>
> First, I think that we are seeing a all new way of thinking and
> designing the programs (I personally think that the most important
> feature in C++ IS templates). While such mechnism exists in other
> langages, C++ is maybe the first very broad langage to support
> generic programming. For myself, I have seen too many people claiming
> that "this algorithm is optimal (or O(something)) with the use of
> such and such structure)" with the optimal version never implemented
> because it was not worth for each specific case. On the contrary,
> since STL is existing many people are using red-black trees (or another
> ``optimal''data structure without even knowing exactly what the
> structure is).
I studied the HP implementation of the STL a few years ago in detail and I
would have to say that the use of templates in the STL is much more
straight forward than the kinds of uses I was talking about. For the most
part the STL takes advantage of compile-time polymorphism (e.g.
standard container and iterator interfaces) and this is much easier to
understand than expression templates and the kind of loop unrolling for
small matrices/vectors that template meta-programs do.
I would categorize the following as straight forward uses for templates in
C++:
1) Templated classes and non-member functions for abstract data types
which include built-in data types. For example, this is what the standard
container classes in the C++ library (STL) are. This is the role that
templates where first envisioned for.
2) Compile-time polymorphism through an agreed upon interface. Again,
container adaptors (e.g. stacks) in the STL rely on a standard interface
to containers. This allows you to add your own container classes. The
STL really brought this to light (at least to me).
I have found many wonder uses for the two types of techniques above.
On the other hand, the follow methods I would describe as advanced or
non-intuitive uses for C++ templates:
1) Expression templates. Building nested types for an expression at
compile time. Here Blitz++ would be the prime example.
2) Template meta-programs, template selectionals. Examples include:
recursive compile-time calculations (e.g. the factorial! example),
compile-time recursive expression parsing (e.g. lots of libraries). I
would imagine that the complaints about C++ templates would fall primarily
into this category.
Does anyone think there should be a finer discretization of these uses for
C++ templates? Should we put together a catalog of C++ template idioms so
that we know what each person is talking about?
> Generic programming is difficult yes, but even for the smallest
> project, injecting genericity is worth in terms of maintenance of the
> code.
>
> Now, I completely agree on the fact that the current compiler are
> slow, but whatever is the slowness of these compilers, using
> genericity is many times better than:
>
> 1) doing it by hand (ie copy and paste the code and adapt it).
> 2) maintaining multiple copies of mostly identical routines...
>
> Agreed, things like template metaprogramming are somewhat difficult
> to grab at first sight all the more that these are new techniques and
> that compilers are not yet completely up to date. But I bet that this
> will be smoothed in the future as the compilers will be more mature
> (and the computer faster :-) ) and that people will get more
> experience on this type of programming.
Faster and more compatible compilers will not make these advanced template
methods any easier to understand. What we need is better education.
Perhaps a catalog of template idioms?
Cheers,
Roscoe
--------------------------------------
| Roscoe Bartlett |
| Ph.D. student |
| Department of Chemical Engineering |
| Carnegie Mellon University |
| email: roscoe@andrew.cmu.edu |
| work phone: (412) 268 - 2238 |
--------------------------------------
--------------------- Object Oriented Numerics List --------------------------
* To subscribe/unsubscribe: use the handy web form at
http://oonumerics.org/oon/
* If this doesn't work, please send a note to owner-oon-list@oonumerics.org
This archive was generated by hypermail 2b29 : Mon Apr 10 2000 - 12:47:56 EST