Magazines  


Book Review
Department Editor: Warren Keuffel, wkeuffel@computer.org

 

Programming Programs With C++ Templates

Ed Harcourt

C++ Templates: The Complete Guide, David Vandevoorde and Nicolai M. Josuttis, Addison Wesley, 2003, ISBN 0-201-73484-2, 528 pp., US$54.99

How many times have you developed a function (in whatever language) only to realize you need to rewrite that same function for different types? Canonical examples include a swap function that can exchange two values of any type. In C, genericity is usually accomplished through either the macro preprocessor or using pointers to void—for example, the standard C library function declarations for bsearch (binary search) and qsort (Quicksort). However, both approaches have problems. Macros are difficult to debug and don’t provide proper scoping. And pointers to void aren’t type safe and can be inefficient because they require an extra level of pointer indirection.

Dynamically typed languages, such as lisp or Perl, solve this by deferring type checking until runtime. Statically typed languages such as Haskell, ML, Ada, and C++ support genericity through parametric polymorphism, and each of these languages has a name for their facility: Ada has generics, and C++ has templates.

Templates have been part of the C++ language for many years, but only in the last couple of years have C++ compiler vendors provided robust and complete support for C++ templates as specified in the ISO C++ Standard. The availability of standard conforming compilers, along with extensive work on exploring new programming techniques enabled by C++ templates, show that C++ templates provide expressive power that goes well beyond what you can find in other languages’ systems. Much of the recent work on Generic Programming (www.boost.org) and Generative Programming (Generative Programming, Krzysztof Czarnecki, Ulrich Eisenecker. Addison-Wesley, 2000) uses C++ templates.

A comprehensive guide

C++ is a complex language on its own; add in templates and things get nasty. Navigating the rules and trying to use templates for all but the standard textbook cases is difficult at best. C++ Templates: The Complete Guide addresses these complexities and provides real examples as well as guidelines on how to use templates in practice. The authors are two C++ experts. Nicolai Josuttis is well known for his excellent book, The C++ Standard Library (Addison-Wesley, 1999). David Vandevoorde works at the Edison Design Group, which provides arguably the most standard-conforming C++ front end in the industry. Both Vandevoorde and Josuttis served on the ISO C++ Standards Committee.

Although scads of well known C++ textbooks explain and support templates, C++ Templates not only covers C++ templates from the ground up but also describes all the arcane syntactic and semantic rules surrounding templates. Additionally, the book goes into great depth, providing real examples in which templates are useful as well as guidelines and idioms on how to use them.

Packed with useful examples

One example of a new programming technique the book describes is a method for developing C++ libraries for numerical data types, such as matrices. Before this technique was discovered, C++ numerical libraries lagged in performance compared to those developed in Fortran. Vandevoorde and Josuttis explain how you can use the technique, known as expression templates, to develop numerical C++ libraries that perform comparable to their Fortran counterparts. The trick lies in building compile-time expressions using templates and involves exploiting a powerful C++ template feature called partial template specialization. Partial template specialization requires that the C++ compiler perform pattern matching on C++ types at compile time. You can apply this pattern matching recursively, so you can write loops and conditionals in the template type system itself. This provides C++ with a kind of meta-programming facility on top of C++?

This leads to another template technique the book describes: template metaprogramming. Vandevoorde and Josuttis explain in detail how template metaprogramming works and how to write template metaprograms. They also give examples of how you can use template metaprograms to make compile-time decisions and perform compile-time optimizations such as loop unrolling.

Not for the beginner, C++ Templates is an entire book (all 528 pages of it) devoted to one particular feature of C++: templates. The book assumes a thorough understanding of base C++. Much of the book is spent going over the gory details of the syntactic and semantic rules surrounding C++ templates and can be tough going. But C++ Templates

is well written and makes heavy use of good examples. For the serious C++ developer, C++ Templates: The Complete Guide belongs on your bookshelf.



Ed Harcourt is an assistant professor of Computer Science at St. Lawrence University. Contact him at edharcourt@stlawu.edu.

         

About Us

Mission, Vision & Goals
History
Awards Program
Volunteer Leadership
Staff Leadership

Contact Us

Member Resources

Volunteer Center

For More Information