Templates Discussion

One starting point for discussion of templates in CeePlusPlus is TheDesignAndEvolutionOfCpp where BjarneStroustrup discusses in Chapter 15 the reasons why they were introduced.

As someone who is using templates, often adapting codes written by other people, I have found it helpful in both a practical way and also in understanding the approach which is being taken. I hope this page can be used to separate the how to elements on e.g. UsingTemplates from the why do it this way discussion. -- JohnFletcher

Java will soon have templates too... In 1.5, so I hear. See http://java.sun.com/aboutJava/communityprocess/review/jsr014/ -- JSR-000014 Adding Generics to the Java Programming Language.

It won't be the same. In order to maintain compatibility with the JVM, templates will be implemented on top of polymorphism. Vector<A> and Vector<B> will really be aliases for a single Vector<Object> class, and A and B will have to descend from Objects. You get the notational convenience, and you get the type safety, because the compiler will insert the appropriate typecasts from Object to A or Object to B. But you don't get the performance advantage. And what about specializations? As far as I know, they won't exist. -- EdwardKiser

Items copied from UsingTemplates.

What's with the template love fest? I hate templates. To me, they are a poor work around for the lack of a base class. I mean, I like C++ for the most part, but templates just infuriate me so much. I'm tempted to go create a class library with a base class every time I have to use templates. If I could change only one thing about C++, I'd chuck the templates and introduce a base class.

You just don't understand templates fully. Templates are part of the next big thing. Spend some time with them - *one* alternative is a base class for *some* of the uses of templates.

C++ has a universal base class - it's called void*. These template aficionados are probably just hung up on type safety.

You can't have a type safe collection of a class with a generalized container.

Not true: see StaticPolymorphism. (why must everything be a collection of base?)
Well, one place that templates really shine is in numerical work, where we can't afford the cost of vpointers. A good discussion of technique etc. can be found at the object oriented numerics page http://oonumerics.org/oon/.

I am a big fan of templates because they help me to stick to the principle of OnceAndOnlyOnce. I think that they are a great language feature. Unfortunately, template support wrt compilers and debuggers still has a way to go in my experience. -- JamesCrawford

Experienced C++ programmers should delight in templates. Newbies and weak journeymen should probably learn to use them without writing them. The syntax for writing templates is staggeringly complex for people who have really just mastered colon-initializer lists.


A further recommendation: Teach code users to typedef their stl classes and iterators and work through the typedefs. They get to avoid the ?? syntax, and you get to replace poorly-thought-out template instantiations in one and only one location. -- MichaelHill

Templates allow not only compile-time binding (hence run-time efficiency in terms of execution speed) but also extensive compile-time optimizations and even better, compile-time computation. This might not appeal to people whose applications are suited to Java or Smalltalk, but for those to whom execution time/memory footprint really matters, these things can save orders of magnitude of time and significant amounts of space at run-time. Books such as the GenerativeProgrammingBook (ISBN 0201309777 ) and ModernCeePlusPlusDesign (ISBN 0201704315 ) show quite well how much more than a "universal base class replacement" templates are, and I would suggest that nobody write off C++ templates until they've learnt what they can do. Templates are a turing-complete system in themselves, even if their syntax is pretty ugly (which is consistent with the rest of C++ syntax, let's face it). -- JamesDennett
I am looking for something on TemplatesForReflectionInCpp and not finding it. Is there something I am missing? -- JohnFletcher
CategoryCpp CategoryCppTemplates

View edit of September 4, 2006 or FindPage with title or text search