Programming Books C Java PHP Python Learn more Browse Programming Books
C++ Template Metaprogramming and over one million other books are available for Amazon Kindle. Learn more

Sorry, this item is not available in
Image not available for
Image not available

To view this video download Flash Player


Sign in to turn on 1-Click ordering
Sell Us Your Item
For a $11.65 Gift Card
Trade in
More Buying Choices
Have one to sell? Sell yours here
Start reading C++ Template Metaprogramming on your Kindle in under a minute.

Don't have a Kindle? Get your Kindle here, or download a FREE Kindle Reading App.

C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond [Paperback]

by David Abrahams, Aleksey Gurtovoy
3.9 out of 5 stars  See all reviews (15 customer reviews)

List Price: $54.99
Price: $38.53 & FREE Shipping. Details
You Save: $16.46 (30%)
o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o
Only 17 left in stock (more on the way).
Ships from and sold by Gift-wrap available.
Want it tomorrow, April 25? Choose One-Day Shipping at checkout. Details
Free Two-Day Shipping for College Students with Amazon Student


Amazon Price New from Used from
Kindle Edition $25.49  
Paperback $38.53  
Sell Us Your Books
Get up to 80% back when you sell us your books, even if you didn't buy them at Amazon. Learn more

Book Description

December 20, 2004 0321227255 978-0321227256
Abrahams and Gurtovoy have written something close to a classic… marvelous fun to read…
Read the complete book review by Jack J. Woehr, Dr. Dobbs Journal, June 03, 2005

"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.

A metaprogram is a program that generates or manipulates program code. Ever since generic programming was introduced to C++, programmers have discovered myriad "template tricks" for manipulating programs as they are compiled, effectively eliminating the barrier between program and metaprogram. While excitement among C++ experts about these capabilities has reached the community at large, their practical application remains out of reach for most programmers. This book explains what metaprogramming is and how it is best used. It provides the foundation you'll need to use the template metaprogramming effectively in your own work.

This book is aimed at any programmer who is comfortable with idioms of the Standard Template Library (STL). C++ power-users will gain a new insight into their existing work and a new fluency in the domain of metaprogramming. Intermediate-level programmers who have learned a few advanced template techniques will see where these tricks fit in the big picture and will gain the conceptual foundation to use them with discipline. Programmers who have caught the scent of metaprogramming, but for whom it is still mysterious, will finally gain a clear understanding of how, when, and why it works. All readers will leave with a new tool of unprecedented power at their disposal—the Boost Metaprogramming Library.

The companion CD-ROM contains all Boost C++ libraries, including the Boost Metaprogramming Library and its reference documentation, along with all of the book's sample code and extensive supplementary material.

Frequently Bought Together

C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond + Modern C++ Design: Generic Programming and Design Patterns Applied + The C++ Standard Library: A Tutorial and Reference (2nd Edition)
Price for all three: $132.96

Buy the selected items together

Editorial Reviews

About the Author

David Abrahams is a founding member and moderator of the Boost C++ library development group. Dave has been an ANSI/ISO C++ committee member since 1996, where he is best known for contributing a theory, specification, and implementation of exception handling for the C++ standard library. His company, Boost Consulting, provides Boost-related support and development services and professional training in the art of software construction.

Aleksey Gurtovoy is a technical lead for MetaCommunications and a contributing member of the Boost C++ community. Aleksey is the original author of the Boost Metaprogramming Library. He has been working with C++ since 1993, and holds a M.S. degree in computer science from Krasnoyarsk Technical State University, Russia.

Excerpt. © Reprinted by permission. All rights reserved.

In 1998 Dave had the privilege of attending a workshop in Generic Programming at Dagstuhl Castle in Germany. Near the end of theworkshop, a very enthusiastic Kristof Czarnecki and Ullrich Eisenecker (of Generative Programming fame) passed out a few pages of C++ source code that they billed as a complete Lisp implementation built out of C++ templates. At the time it appeared to Dave to be nothing more than a curiosity, a charming but impractical hijacking of the template system to prove that you can write programs that execute at compile time. He never suspected that one day he would see a role for metaprogramming in most of his day-to-day programming jobs. In many ways, that collection of templates was the precursor to the Boost Metaprogramming Library (MPL): It may have been the first library designed to turn compile-time C++ from an ad hoc collection of "template tricks" into an example of disciplined and readable software engineering. With the availability of tools to write and understand metaprograms at a high level, we've since found that using these techniques is not only practical, but easy, fun, and often astoundingly powerful.

Despite the existence of numerous real systems built with template metaprogramming and the MPL, many people still consider metaprogramming to be other-worldly magic, and often as something to be avoided in day-to-day production code. If you've never done any metaprogramming, it may not even have an obvious relationship to the work you do. With this book, we hope to lift the veil of mystery, so that you get an understanding not only of how metaprogramming is done, but also why and when. The best part is that while much of the mystery will have dissolved, we think you'll still find enough magic left in the subject to stay as inspired about it as we are.

Making the Most of This Book

The first few chapters of this book lay the conceptual foundation you'll need for most everything else we cover, and chapters generally build on material that has come before. That said, feel free to skip ahead for any reason—we've tried to make that possible by providing cross-references when we use terms introduced earlier on.

Chapter 10, Domain-Specific Embedded Languages, is an exception to the rule that later chapters depend on earlier ones. It focuses mostly on concepts, and only appears late in the book because at that point you'll have learned the tools and techniques to put Domain-Specific Embedded Languages into play in real code. If you only remember one chapter by the time you're done, make it that one.

Near the end of many chapters, you'll find a Details section that summarizes key ideas. These sections usually add new material that deepens the earlier discussion,* so even if you are inclined to skim them the first time through, we suggest you refer back to them later.

We conclude most chapters with exercises designed to help you develop both your programming and conceptual muscles. Those marked with asterisks are expected to be more of a workout than the others. Not all exercises involve writing code—some could be considered "essay questions"—and you don't have to complete them in order to move on to later chapters. We do suggest you look through them, give a little thought to how you'd answer each one, and try your hand at one or two; it's a great way to gain confidence with what you've just read.

Supplementary Material

This book comes with a companion CD that supplies the following items in electronic form

  • Sample code from the book.
  • A release of the Boost C++ libraries. Boost has become known for high-quality, peer-reviewed, portable, generic, and freely reusable C++ libraries. We make extensive use of one Boost library throughout the book—the Boost Metaprogramming Library (MPL)—and we discuss several others.
  • A complete MPL reference manual, in HTML and PDF form.
  • Boost libraries discussed in this book that are not yet part of an official release.

The index.html file at the top level of the CD will provide you with a convenient guide to all of its contents. Additional and updated material, including the inevitable errata, will appear on the book's Web site: You'll also find a place there to report any mistakes you might find.

Trying It Out

To compile any of the examples, just put the CD's boost 1 32 0/ directory into your compiler's #include path.

The libraries we present in this book go to great lengths to hide the problems of less-than-perfect compilers, so it's unlikely that you'll have trouble with the examples we present here. That said, we divide C++ compilers roughly into three categories.

      A. Those with mostly conforming template implementations. On these compilers, the examples and libraries "just work." Almost anything released since 2001, and a few compilers released before then, fall into this category.
      B. Those that can be made to work, but require some workarounds in user code.
      C. Those that are too broken to use effectively for template metaprogramming.

Appendix D lists the compilers that are known to fall into each of these categories. For those in category B, Appendix D refers to a list of portability idioms. These idioms have been applied to the copies of the book's examples that appear on the accompanying CD, but to avoid distracting the majority of readers they don't appear in the main text.

The CD also contains a portability table with a detailed report of how various compilers are doing with our examples. GCC is available free for most platforms, and recent versions have no problems handling the code we present here.

Even if you have a relatively modern compiler from category A, it might be a good idea to grab a copy of GCC with which to cross-check your code. Often the easiest way to decipher an inscrutable error message is to see what some other compiler has to say about your program. If you find yourself struggling with error messages as you try to do the exercises, you might want to skip ahead and read the first two sections of Chapter 8, which discusses how to read and manage diagnostics.

And now, on to C++ Template Metaprogramming!

— Dave and Aleksey

* We borrowed this idea from Andrew Koenig and Barbara Moo's Accelerated C++: Practical Programming By Example.


Product Details

  • Paperback: 400 pages
  • Publisher: Addison-Wesley Professional (December 20, 2004)
  • Language: English
  • ISBN-10: 0321227255
  • ISBN-13: 978-0321227256
  • Product Dimensions: 9.2 x 7.4 x 0.7 inches
  • Shipping Weight: 1.5 pounds (View shipping rates and policies)
  • Average Customer Review: 3.9 out of 5 stars  See all reviews (15 customer reviews)
  • Amazon Best Sellers Rank: #138,831 in Books (See Top 100 in Books)

More About the Author

Discover books, learn about writers, read author blogs, and more.

Customer Reviews

Most Helpful Customer Reviews
28 of 30 people found the following review helpful
4.0 out of 5 stars An interesting book... December 30, 2004
This is a really interesting book. The template metaprogramming technical topics covered are extremely advanced, and right at the leading edge of C++ library development, yet the step-by-step presentation makes the material understandable even to intermediate programmers. Since the book draws its examples from code that works with the Boost libraries (supplied on a CD), readers can try the examples and play around with them to see how they work.

Some of the libraries discussed (Boost Type Traits, Boost Bind) are well along to becoming part of the C++ standard via a library technical report, so they will eventually become available to every C++ programmer. The bulk of the book is devoted to the Boost Metaprogramming Library, which packages up a lot of advanced techniques into accessible form.

One issue with template metaprogramming is that compile times can get out of hand. The book includes an appendix with hints on avoiding the problem, together with test timings for a half-dozen popular compilers. I really like the timings; too many other authors make assertions about efficiency without supporting data.

I would guess that this book will be of interest to intermediate and advanced C++ programmers interested in library development. And less interesting to beginning programmers, or programmers who never package up their creations into libraries.
Comment | 
Was this review helpful to you?
42 of 49 people found the following review helpful
3.0 out of 5 stars I found the title misleading April 25, 2006
This book is called "C++ Template Metaprogramming" but it should be called "Boost MPL API Reference." The first portion of this book covers the basics of template metaprogramming fairly well, but what I wanted the rest of the book to cover were both advanced techniques and real-world applications. What I got was material on how to use the Boost metaprogramming library. This book mostly covers just that library, and the various templates that it offers, but what I felt the book should have provided was not only how the more interesting parts of the MPL were implemented, but also interesting applications of the MPL where some interesting algorithm was made possible by template metaprogramming. I can look up the MPL reference docs online, thank you very much.

To summarize - this is a pretty good introduction to template metaprogramming, but seems to be suited for the unlikely position of someone who wants or needs to use this technique, but isn't academically interested in it, and so mainly needs a walkthrough of the set of standard functions available as part of the MPL.

This book is a bad choice for those interested in template metapgramming and wondering if it might help them, but want to learn more about it and its applications first.
Was this review helpful to you?
19 of 21 people found the following review helpful
5.0 out of 5 stars Necessary book for template library writers. January 20, 2005
Format:Paperback|Verified Purchase
This book describes the boost MPL library. Its a very useful guide to this library and it includes enough examples to walk you through how to use it.

Secondly the book explains Meta-programming. This is a new concept to a lot of C++ programmers and old hat to LISP programmers. The C++ pre-compiler is constrained to integral types as constraints. But there are a host of tricks you can use within that constraint to build libraries that adapt to their calling structure. Thus generating code that is as efficient as hand written. Of course with your compiler, your milage may vary.

The other great thing about using this library, MPL, is that where you would write repeticious template code for every parameter in a template'd library like Tuples, you can automate with it with MPL.

Meta programming is a pretty new concept within the C++ community and this book will give us a common language to talk about it. There are other resources on the net, but this book pulls them together.

Highly recommended for expert C++ programmers, C++ Library writers and intermediate programmers study'ing to become experts. If you finished Andrei's book, "Modern C++ Design", this is a great next book to buy and own.
Comment | 
Was this review helpful to you?
7 of 8 people found the following review helpful
5.0 out of 5 stars An excellent treatment of the topic October 18, 2010
This was a pivotal book for me. I'd been working with C++, both as a programmer and a compiler writer, for almost 20 years. Yet despite an intimate familiarity with templates, the notions of template metaprogramming had always eluded me. By chapter 3 of this book, however, it felt as though a heavy curtain had been lifted. I actually became excited to wake up each day at the thought of reading the next chapter! Imagine that, from a C++ book.

The material is well-paced, lucidly written, and doesn't stray into minutae any longer than necessary. I would recommend it to anyone who wants to find out more about applying metaprogramming techniques to their own code. I'm already thinking of news ways I can use it. The only caveat I would mention is that your background will in large part determine this book's value to you. Coming from a C++ background, and lately having an intense interest in Haskell, I found many of this book's points "popped out" at me, showing connections between runtime C++ and compile-time functional programming (in the strange meta-language that is compile-time C++). But had I not had that prior experience, and been struggling with the very issues this book solves, some of it may not have had as great an impact. Be ready to do more research if you find the material a bit rough going. There are some profound implications between its pages that may not be obvious at first, but they're there.
Comment | 
Was this review helpful to you?
Most Recent Customer Reviews
4.0 out of 5 stars Really great book, but terrible Kindle edition
I very rarely write reviews, but I feel compelled to do so in this case, because
this has been so painful. Read more
Published 15 months ago by Martin Blais
3.0 out of 5 stars 8 years out of date is a long time in this field
High time for a revision, don't you think? Boost is at 1.51. C++11 is out. Or is there some other book that is up to date?
Published 20 months ago by Dave Yost
5.0 out of 5 stars The best C++ book I've read
I divide programming books in two types: reference and value books. A reference book will give you some trivia on the given topic and a good reference book will do this in a manner... Read more
Published on April 8, 2012 by Snagglepuss
3.0 out of 5 stars Good for gurus, too much for anyone else
This is basically thorough documentation for the Boost MPL. Not for the faint of heart (when it comes to C++ templates), and probably not of too much use except to C++... Read more
Published on December 5, 2008 by mtlimber
4.0 out of 5 stars Unique material
Abrahams and Gurtovoy address a distinctive and little-known set of C++ features: behaviors of the template mechanism that go far beyond swapping "double" for "float. Read more
Published on November 27, 2008 by wiredweird
5.0 out of 5 stars It's about Metaprogramming
The operative word in the title is "Metaprogramming". If your looking for C++ template programming, your better off going elsewhere. Read more
Published on October 21, 2008 by T. Dobroth
5.0 out of 5 stars Very good book
I am not a meta programming expert, however I learned a lot from this great book. I suggest first to read Alexandrescu's Modern C++ first and a learn basic techniques then get this... Read more
Published on July 6, 2008 by Cumhur Guzel
4.0 out of 5 stars In depth, but not intuitive
I knew in purchasing this book that most of the time it would just be talking about the Boost MPL. That shouldn't be too bad - the Boost MPL is pretty much the fore-running... Read more
Published on May 20, 2008 by Mr Squiggle
2.0 out of 5 stars disaster !!!
metaprogramming is understandably a difficult topic. but it could have been made easier to understand with a talented writer. obviously these two authors have no such talent. Read more
Published on April 27, 2008 by Roger
1.0 out of 5 stars No Target Audience Specified
I would really like to learn about the boost mpl and I thought this book might help. Well, I'm learning, but only because I'm working very hard to untangle the mess presented. Read more
Published on February 1, 2008 by Dash Riprock
Search Customer Reviews
Only search this product's reviews

What Other Items Do Customers Buy After Viewing This Item?


There are no discussions about this product yet.
Be the first to discuss this product with the community.
Start a new discussion
First post:
Prompts for sign-in

Look for Similar Items by Category