Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best introduction to C++ template metaprogramming? [closed]

[Answering my own question]

The best introductions I've found so far are chapter 10, "Static Metaprogramming in C++" from Generative Programming, Methods, Tools, and Applications by Krzysztof Czarnecki and Ulrich W. Eisenecker, ISBN-13: 9780201309775; and chapter 17, "Metaprograms" of C++ Templates: The Complete Guide by David Vandevoorder and Nicolai M. Josuttis, ISBN-13: 9780201734843.

alt textalt textalt textalt text

Todd Veldhuizen has an excellent tutorial here.

A good resource for C++ programming in general is Modern C++ Design by Andrei Alexandrescu, ISBN-13: 9780201704310. This book mixes a bit of metaprogramming with other template techniques. For metaprogramming in particular, see sections 2.1 "Compile-Time Assertions", 2.4 "Mapping Integral Constants to Types", 2.6 "Type Selection", 2.7 "Detecting Convertibility and Inheritance at Compile Time", 2.9 "NullType and EmptyType" and 2.10 "Type Traits".

The best intermediate/advanced resource I've found is C++ Template Metaprogramming by David Abrahams and Aleksey Gurtovoy, ISBN-13: 9780321227256

If you'd prefer just one book, get C++ Templates: The Complete Guide since it is also the definitive reference for templates in general.


Andrei Alexandrescu's Modern C++ Design book covers a lot of this and other tricks for speedy and efficient modern C++ code and is the basis for the Loki library.

Also worth mentioning is the Boost libraries, which heavily use these techniques and are usually of very high quality to learn from (although some are quite dense).


Modern C++ Design, a brilliant book and design pattern framework by Alexandrescu. Word of warning, after reading this book I stopped doing C++ and thought "What the heck, I can just pick a better language and get it for free".


Two good books that spring to mind are:

  • Modern C++ Design / Andrei Alexandrescu (It's actually 7 years old despite the name!)
  • C++ Templates: The Complete Guide / Vandevoorde & Josuttis

It's quite an in-depth field, so a good book like one of these is definitely recommended over websites. Some of the more advanced techniques will have you studying the code for some time to figure out how they work!


Modern C++ is one of the best introductions I've read. It covers actual useful examples of template metaprogramming. Also take a look at the companion library Loki.


There won't be a large list of books, as the list of people with a lot of experience is limited. Template metaprogramming started for real around the first C++ Template Programming Workshop in 2000, and many of the authors named so far attended. (IIRC, Andrei didn't.) These pioneers greatly influenced the field, and basically what should be written is now written. Personally, I'd advice Vandevoorde & Josuttis. Alexandrescu's is a tough book if you're new to the field.