As generic programming gains popularity and practitioners, the issue of templates comes up more and more these days. But templates are still tough to grasp and difficult to master. C++ Templates: The Complete Guide, by David Vandevoorde and Nicolai M. Josuttis, transforms generic programming with C++ from a nightmare into a joy.
If you’re like me, you’ve at least occasionally employed C++ templates. For instance, the Standard Template Library (STL) is full of templated code you probably use every day. But you’ll find that creating your own templated classes/functions is far from easy. There are many complex issues to deal with, such as member templates, templated constructors, and function template argument deduction. The syntax can appear peculiar, to say the least. And this is just the beginning.
Despite this complexity, C++ Templates proves that templates can be effective, as well as a lot of fun, when used correctly.
A book for intermediate to advanced C++ programmers
To benefit from C++ Templates, you should have at least an intermediate knowledge of C++. You should also have a good understanding of function overloading, since templates sometimes interact in strange ways with the process. (The book includes an appendix that deals with this issue.) If you don't have a solid grounding in C++ programming, the techniques described in this book will likely be beyond your ability to employ effectively.
If you are an intermediate or advanced C++ developer, this book is definitely for you. It’s meant as a complete reference, and the later sections provide real-life examples that are simple and straight to the point. As a bonus, some chapters offer "afternotes" that synopsize the history of certain templating issues. (It’s fun to learn that member class templates became part of the standard because of an oversight!)
Following the introductory first chapter, "About this Book," C++ Templates is divided into five parts:
- "The Basics"—chapters 2 to 7: An introduction to templates
- "Templates in Depth"—chapters 8 to 13: Templates to the bone and future directions
- "Templates and Design"—chapters 14 to 18: Examples, patterns, metaprograms, expression templates
- "Advanced Applications"—chapters 19 to 22: Type classification, smart pointers, tuples, function objects, and callbacks
- Two appendixes—"The One-Definition Rule" and "Overload Resolution"
Unlike with some programming guides, where introductory sections can be skipped by the seasoned programmer, I recommend you read all of C++ Templates. Even if you think you know a topic, simple assumptions can lead to future template code errors. For instance, did you know that max<>(2, 5) calls max< int>( 2, 5) even if an intmax(int, int' exists? Lessons in section 2.4 point out this potential pitfall.
Part I: The Basics
Every chapter in this book offers something valuable. For instance, chapter 4 shows that template parameters don’t have to be types; they can also be nontype constants. Chapter 5 offers some tricky template basics, such as using member templates. (Did you know that if you have a templated constructor that could match copy-construction, it won’t be called when you copy-construct an object?) Chapter 6 deals with practical issues, such as the fact that templates usually reside in header files.
Part II: Templates in Depth
Looking up template names is sometimes problematic. The answer to this is the Koenig lookup, explained in chapter 9. A key component of generic programming is the specialization of templates. Chapter 12 explains why specialization is sometimes needed and shows you when and how to employ this technique. Chapter 13 is my favorite, describing possible future additions to the C++ standard, such as partial specialization of function templates, a typeof operator, and typedef templates.
Part III: Templates and Design
Part three may well be the prize section of C++ Templates. Chapter 14 demonstrates that polymorphism can also be static, allowing for new design patterns. Chapter 15 discusses traits/policies, which bring reusability to an even higher level. Computing values at compile time can be valuable and can boost efficiency, as chapter 17 illustrates. Just think about it: You can calculate the prime numbers at compile time! Expression templates, the central subject of chapter 18, will knock your socks off. Math-hungry applications can really benefit from these code elements, as complex array operations can now be computed efficiently while keeping the code readable.
Part IV: Advanced Applications
Part four builds on parts two and three. Chapter 19 shows how, at compile time, you can know different aspects of a type such as whether it is a class, a function, or an enumeration. Smart pointers, discussed in chapter 20, fit templates like a glove. Don’t think it’s the usual smart pointer article, as the authors bring up the concept of trule, which allows you to return a newly allocated pointer, freeing you from dealing with exception and deallocation issues. Functors (objects with an overloaded operator) are discussed thoroughly in chapter 22, including where they can be useful—as template arguments to functions/classes for efficient passing of arguments. Functors can also be composed, such as f1(f2(x)), and you can bind certain arguments, as in std::bind1st/ std::bind2nd.
Perhaps the lone drawback of this book is that some portions (for example, chapter 10) are occasionally difficult to grasp. Some of this is to be expected, as advanced generic programming is indeed a difficult subject. The authors prepare you for this complexity.
This book is more than a complete guide to understanding templates. Parts three and four contain applications of templates you can use in day-to-day programming. These days, it’s difficult to find code that doesn't use templates. C++ Templates: A Complete Guide will help you become a developer, rather than merely a user, of templated code.