Adam Petersen - Software Development Pages, Book Reviews Section
Start News Articles Book Reviews

Book Review

C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond
by David Abrahams, Aleksey Gurtovoy

ISBN: 0321227255
Publisher: Addision-Wesley Professional
Pages: 400

I really understood the power of C++ templates as I read Andrei Alexandrescu's Modern C++ Design a couple of years ago. Or at least I thought I understood it. While reading this book I soon realized that there's much more to metaprogramming than I initially thought. Whereas Alexandrescu demonstrates parameterizable generic implementations of design patterns, the focus in Abrahams and Gurtovoy's book is more on library design. And in my opinion this is where the real strength of metaprogramming with C++ templates lies.

Already on page 6 the reader gets an astonishing look at the elegance a well-designed library using metaprogramming techniques provides. The example uses the Boost Spirit library, which is a framework for generating parsers and allows a user to specify a grammar on an EBNF-like format in C++. I particularly like the example as it concretizes the benefits of C++ metaprogramming: expressiveness and a domain-specific language integrated with C++.
Although the Spirit example almost approaches magic at a first glance, the techniques behind it are described later in the book and accompanied with a complete walkthrough of the design of a domain specific embedded language.

Metaprogramming is conceptually hard and, using C++, also a syntactic challenge. The second chapter of the book provides a very good introduction to fundamental concepts in metaprogramming and the Boost metaprogramming library, used throughout the book, strongly simplifies the syntax and thus improves productivity by providing high-level compile-time algorithms.

The authors do an excellent job both at explaining metaprogramming concepts (their presentation of expression templates is the best I've ever read on the subject) and at introducing library design using these techniques. But, no matter how clearly written, this is not an easy read. I usually tend to skim through exercises included in books, but in this case I felt a need to sit down with a compiler and actually work my way through them.

C++ metaprogramming is far from every programmer’s daily concern. Where library writers have to master the art, it is probably enough for most of us to be aware of the concepts and the power it holds. I would however recommend this book to all experienced C++ programmers. Not only is it a great introduction to the subject. It is also, to borrow Eric S. Raymond’s quote on LISP, "worth learning for the profound enlightenment experience you will have when you finally get it".

Reviewed August 2005

©2005 Adam Petersen