REVIEW - C++ Templates - The Complete Guide


Title:

C++ Templates

The Complete Guide

Author:

David Vandevoorde, Nicolai M. Josuttis

ISBN:

Publisher:

Addison-Wesley Professional (2003)

Pages:

528pp

Reviewer:

Josh Walker

Reviewed:

February 2003

Rating:

★★★★★


If you only buy one new C++ book this year, this should be it.

Unless you have been hiding under a very large pile of code lately, you will have noticed that templates are quite in vogue in C++ at the moment. Some of this is due to hype, but mostly it is justified because templates are an extremely powerful and genuinely useful tool. As a result, there are several recent books that make sophisticated use of template techniques, e.g., Modern C++ Design and Generative Programming . Furthermore, any book on the STL will clearly touch on templates. And even general books such as More Exceptional C++ can't avoid mentioning templates. So why do we need another book on templates? Because despite all of the above, there is a gaping hole in the literature, which the authors of C++ Templates describe in Chapter 1:

Yet we have found that most existing books and articles are at best superficial in their treatment of the theory and application of C++ templates. Even those few books that do an excellent job of surveying various template-based techniques fail to describe accurately how these techniques are supported by the language. As a result, beginning and advanced C++ programmers alike are finding themselves wrestling with templates, attempting to decide why their code is handled unexpectedly.

This observation was one of the main motivations for us to write this book.

This book succeeds phenomenally at filling the hole.

You can rest assured that the authors know what they're talking about. Nicolai Josuttis should already be well known to you as the author of The C++ Standard Library , another must-have book. David Vandevoorde may be less familiar. He is an engineer at EDG, which produces a C++ compiler front end widely acknowledged to be the most standards-conforming available. He was also the main implementor of EDG's support for exported templates, the only implementation of this (controversial) feature on the market. In C++ Templates , Josuttis and Vandevoorde live up to their excellent reputations by presenting accurate and useful information in a clear, easy-to-read manner.

Let me list some of the high points so you can see why this book is so useful. Part I of IV is titled The Basics. It begins with a brisk tutorial/review of function templates in Chapter 2, and class templates in Chapter 3. This material is probably familiar, but reading these chapters will clear up any lingering confusion you may have. Already after Chapter 3 we enter territory where many C++ programmers will struggle. The remaining chapters in Part I are Nontype Template Parameters (Chapter 4), Tricky Basics (Chapter 5), which among other things discusses using the keywords typename and template for dependent names, Using Templates in Practice (Chapter 6), which covers compilation models and debugging techniques, and Basic Template Terminology (Chapter 7).

Part II is Templates in Depth. All but the most battle-hardened template coder will learn something here. With topics such as name lookup, SFINAE, points of instantiation, argument deduction ... this part alone is worth the price of the entire book. Part III, Templates in Design and Part IV, Advanced Applications both discuss more concrete details of applying templates to real problems. These also contain a wealth of sample code.

While the information in this book will vastly improve the quality of your template code, another equally valuable contribution is specifying the vocabulary with which we talk about templates. Clarifying the conventional meaning of terms such as traits, policies, template arguments, template parameters, etc. will aid in more precise communication between programmers. As an example, I have already seen the term SFINAE, which the authors have coined, popping up in newsgroups and mailing lists. SFINAE stands for Substitution Failure Is Not An Error, and is a principle that enables remarkable compile-time techniques such as checking for the existence of a particular method in a class.

Now, to keep this review from being entirely one-sided, here are my three complaints. During my review, I was able to contribute several typos to the online errata; however, these are all minor annoyances that shouldn't cause real confusion. Second, I felt that Chapter 13 on Future Directions, which discusses possible additions and changes to templates in C++0x would have been better left as an appendix. Although quite interesting and worth reading, this chapter is necessarily more speculative and may become dated relatively soon. Finally, my biggest complaint is that the

SArray
(simple array) template class presented at the beginning of chapter 18 on expression templates holds some dangers for naïve readers. The copy constructor is not exception-safe; it will leak memory if the contained type's assignment operator throws. Second, the use of assert statements for error checking is inconsistent (assigning two differently-sized arrays will trigger an assertion, but adding them together may silently read past the end of the smaller one). Although these issues are not strictly relevant in the context of expression templates, I felt that this class failed to meet the high standard established by the rest of the book.

But don't let these few complaints dissuade you. Overall this is an excellent book. If you only buy one new C++ book this year, this should be it.


Book cover image courtesy of Open Library.





Your Privacy

By clicking "Accept Non-Essential Cookies" you agree ACCU can store non-essential cookies on your device and disclose information in accordance with our Privacy Policy and Cookie Policy.

Current Setting: Non-Essential Cookies REJECTED


By clicking "Include Third Party Content" you agree ACCU can forward your IP address to third-party sites (such as YouTube) to enhance the information presented on this site, and that third-party sites may store cookies on your device.

Current Setting: Third Party Content EXCLUDED



Settings can be changed at any time from the Cookie Policy page.