This is the home page for C++ Template Metaprogramming, a book by David Abrahams and Aleksey Gurtovoy. To order, visit Addison Wesley's page for the book.
Abrahams and Gurtovy have written something close to a classic… marvelous fun to read…
In a professional practice increasingly constrained by formality, herded by integrated development environments, straitjacketed by approbated design patterns, isn't it wonderful that unheralded beauty can push through the cracks in the asphalt?
C++ Template Metaprogramming is wholesome, brilliant, expertly written, well laid out, and incidentally offers a great deal of immensely useful practical tips about using and debugging C++ templates. The section on wading through compile-time error messages, which we all know tell you everything and nothing about what went wrong during template expansion, is alone worth the price of admission to those who have never touched base at that particular Rosetta Stone.
—Jack J. Woehr, Dr. Dobb's Journal,
June 03, 2005 (full review)
If you're like me, you're excited by what people do with template metaprogramming (TMP) but are frustrated at the lack of clear guidance and powerful tools. Well, this is the book we've been waiting for. With help from the excellent Boost Metaprogramming Library, David and Aleksey take TMP from the laboratory to the workplace with readable prose and practical examples, showing that "compile-time STL" is as able as its runtime counterpart. Serving as a tutorial as well as a handbook for experts, this is the book on C++ template metaprogramming.
—Chuck Allison, Editor, The C++ Source
C++ Template Metaprogramming sheds light on the most powerful idioms of today's C++, at long last delivering practical metaprogramming tools and techniques into the hands of the everyday programmer.
Since the introduction of templates, C++ programmers have discovered surprising and powerful ways to perform computation at compile-time. While the excitement generated by these capabilities among C++ experts has reached the community at large, their practical application remains out-of-reach for many programmers. Literature on C++ template metaprogramming has focused primarily on details of low-level "tricks" at the expense of strong idioms and abstractions, and without illuminating the path from metaprogramming to expressive interfaces and efficient, maintainable software.
This book delivers both "big picture" ideas and practical tools. It explains what metaprogramming is, why it matters, and how the unique combination of features in C++ make it an especially powerful language for metaprogramming. It also presents the Boost Metaprogramming Library, a powerful open source framework of high-level compile-time components based on familiar STL idioms, which makes C++ metaprogramming easy, expressive, and fun.
Errors are inevitable, but we hope to fix them for upcoming editions, and reports of errors are very much appreciated! Please send errata reports to mplbook@boost-consulting.com.
Please visit the errata page to review the known errata.
Ariel Badichi began a set of Wiki Pages containing answers to some of the exercises. The authors are deeply grateful to Ariel for his generous contribution. Feel free to add missing answers or amend existing ones. Although we can't yet vouch for the correctness of any of the answers, we'll be happy to check particular ones by request. Just send email to mplbook@boost-consulting.com and ask us to have a look.
Contrary to the preface in the book's first printing, examples containing workarounds for nonconforming compilers didn't make it onto the book's companion CD. Instead we have posted a zip archive of the book's examples with workarounds for nonconforming compilers, which you can download here.
So far, we have implemented workarounds for Microsoft Visual C++ 6 and 7. The resulting sources can be found in the msvc/chapter-or-appendix subdirectories of the archive.
Note
Some changes to the MPL headers were also neccessary. Patched files have been placed in the msvc/patches/ subdirectory of the archive. To use the MPL with these compilers, place the archive's msvc/patches/ subdirectory in your compiler's #include path ahead of the boost_1_32_0/ directory so that the patched files take precedence, or simply replace the corresponding files from your boost installation with those in the patches/ directory.
These patches have been committed to Boost's CVS repository and will be applied to the next Boost release, so if you are using a more recent version of Boost than 1.32.0, you can skip this step.