C++ Templates: The Complete Guide

June 1st, 2003 by Michael Baxter in

Your rating: None
Book Review: C++ Templates: The Complete Guide by David Vandevoorde and Nicolai M. Josuttis

Boston, Pearson Education, Inc., 2003

ISBN: 0-201-73484-2

$54.99 US (hardcover)

This book is a user's manual for the future of C++ programming. C++ is going through a dramatic metamorphosis, moving far away from C-like pointer manipulation. With the introduction of the standard template library (STL), part of the C++ standard library, low-level coding for data structures and containers is no longer necessary. Instead, efficient generic tools already are available for your code.

One important construct underlying all the changes in C++ is templates, which introduce parameterized types for functions. This new book opens the door to understanding how templates work, with examples throughout.

Part I introduces template basics in seven chapters that extol the virtues of static polymorphism, which allows compile-time type checking that would not be possible at runtime. The authors explain the differences between function templates and class templates, how to use nontype parameters in templates and how to avoid numerous problem areas.

Part II explains templates in-depth and provides information about various kinds of arguments in template code, friend functions, the importance and control of names and how instantiation is done. It also covers how template arguments are deduced and how to decide between generic and specialized code.

The drama really unfolds in Part III. You even may catch yourself thinking, “Whoa, this is C++? I didn't know it could do all this.” The tone is set by a discussion of dynamic vs. static polymorphism. Dynamic polymorphism is the kind generally associated with C++--inheritance with dynamic dispatch at runtime. Templates are a form of static polymorphism, and compile time only seems to be limiting. But this turns out not to be the case. For example, C++ code can be constructed to moderate the behavior of classes by using templatized traits and policy classes. And, templates can be used with inheritance for applications such as parameterized virtuality, which combines the best of two forms of polymorphism.

Metaprogramming, normally associated with Lisp is one of the most powerful uses of templates. Templates actually can be used to construct program-writing programs, or code generators. After the unveiling of capabilities in Part III, Part IV offers a reprise on advanced applications. A highlight is being able to do type classification with templates. Templates allow you to use the type algebra in C++ directly for your own applications, a key to behavioral patterns.

Templates have been used to construct several useful C++ libraries, including the vast collection at boost.org. Both authors are experienced C++ programmers and are closely associated with the C++ Standards Committee. Vandevoorde is a cofounder with of the comp.lang.c++.moderated newsgroup, and Josuttis has written extensively on C++, including The C++ Standard Library.

—Michael Baxter

__________________________


Special Magazine Offer -- 2 Free Trial Issues!
Receive 2 free trial issues of Linux Journal as well as instant online access to current and past issues. There's NO RISK and NO OBLIGATION to buy. CLICK HERE for offer

Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.

Sorry, offer available in the US only. International orders, click here.

Featured Videos

Email is one of the least private and least secure forms of communication, although few people realize this. MixMaster is one way to allow secure, anonymous communication even over the very public medium of email. This tutorial will get you started with MixMaster quickly and easily.

In case you were wondering about the fun side of Linux World Expo, we thought we'd give you a peek at our shenanigans. We at Linux Journal love what we do so much, that we can't help but have a ball wherever we go.

From the Magazine

September 2008, #173

Feeling a bit like a Thermian? Never give up, never surrender! Someday, you could go from underdog to top dog. Just take a look at a few of the underdogs we highlight in this issue: Mutt, djbdns, Nginix, Gentoo, Xara and the program voted mostly likely to fail just a few years back—Firefox. If Firefox is not radical enough for you, check out Chef Marcel's column for some more alternatives. Having trouble mapping your program data to your relational database? If so, Rueven Lerner shows you some tricks in his At The Forge column.

Need to run GUI applications on your server in the next state? In his Paranoid Penguin column, Mick Bauer shows you how to do it securely. Kyle Rankin keeps hacking and slashing and shows you a few split screen secrets you may not be familiar with. Finally, we all know what happens next February, but only Doc knows what happens afterward.

Read this issue