REVIEW - C++ Templates - The Complete Guide


Title:

C++ Templates

The Complete Guide

Author:

David Vandevoorde, Nicolai M. Josuttis, Douglas Gregor

Publisher:

Addison-Wesley (2017)

Pages:

832pp

Reviewer:

Paul Floyd

Reviewed:

May 2020

Rating:

★★★★☆


Recommended for advanced users. I would suggest that beginners start with something else.

I read the first edition of this book sometime back in 2003 (reviewed here https://accu.org/index.php?module=bookreviews&func=search&rid=506) . A great deal has changed in the 15 years between the two editions – 3 new revisions of the C++ standard in particular. This shows up in the page count, and even visibly with the second edition being about 2cm thicker.

This being an ‘upgrade’ read, I thought that I would just be able to skim over the ‘new bits’ in just a few days. Boy, was I wide of the mark… several weeks more like it. Even though, when I look at the two chapter listings, clearly the two books have the same structure, my feeling was that the second edition was an entirely different experience. In part this is due to the large amount of extra material, and also in part a change in my perspective. Back then I thought that I knew a bit about template programming. Now I’m sure that I know little about it.

The book covers templates in depth for all C++ versions up to and including C++17, plus a bit on additions expected a bit in C++20, especially concepts. I won’t list the chapters (there are 28 of them, plus 5 appendices). There are 3 parts to the book, ‘Basics’, ‘Templates in Depth’ and ‘Templates and Design’. I did laugh (out loud even) at the start of part 2 when I read that part 1 (‘Basics’) “ is sufficient to answer most questions that may arise in everyday C++ ”. I doubt that your average C++ programmer’s needs get much past chapter 2 and perhaps chapter 9 (covering instantiation and error messages).

As you would expect, everything regarding templates is covered. Function and Class Templates, Nontype Parameters, much about traits/policies and meta programming. There’s also a lot on overloading and name lookup. There’s considerable coverage of how templates interact with language features, which is very important if you want to understand which types will be used to instantiate your template functions (and to a lesser extent classes). There is some nitty-gritty, but this is no ‘C++ Templates in 7 Days’. The last few chapters cover a few concrete examples such as Tuple and Variant template classes.

The book contains a lot of examples in the form of snippets, often in the form of a few definitions followed a list of 4 or 5 example uses with comments like “// OK T deduced as long int” or “// ERROR”. I found that this bogged down the compiler in my head, and I would often spend 5 or 10 minutes trying to think through why a certain piece of code would resolve to which types. Perhaps I should have skimmed through quickly a first reading and then a more thorough second pass.

I don’t know what sort of reader will benefit from this book. It is neither a tutorial/cookbook nor a straight reference book. I suppose that, as it says on the cover, ‘guide’ is the best way to describe it. Compiler writers (there are a few sections on parsing) and template library writers will want a copy. After that, if you already have copies of Stroustrup, Meyers and Josuttis then you will also want to get this one.


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.