Best introduction to C++ template metaprogramming? [closed]
Asked Answered
G

8

122

Static metaprogramming (aka "template metaprogramming") is a great C++ technique that allows the execution of programs at compile-time. A light bulb went off in my head as soon as I read this canonical metaprogramming example:

#include <iostream>
using namespace std;

template< int n >
struct factorial { enum { ret = factorial< n - 1 >::ret * n }; };

template<>
struct factorial< 0 > { enum { ret = 1 }; };

int main() {
    cout << "7! = " << factorial< 7 >::ret << endl; // 5040
    return 0;
}

If one wants to learn more about C++ static metaprogramming, what are the best sources (books, websites, on-line courseware, whatever)?

Goods answered 21/9, 2008 at 21:50 Comment(2)
lightbulb went 'off' or 'on' ?Maybellmaybelle
Off. Definitely off.Dipsomaniac
G
121

[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 text alt text alt text alt 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.

Goods answered 21/9, 2008 at 21:50 Comment(7)
Link to Todd Veldhuizen's piece has gone stale.Garvey
I believe its this: www10.informatik.uni-erlangen.de/~pflaum/pflaum/ProSeminar/…Vicariate
link fixed now, thanks!Goods
The link is broken again!Harneen
link fixed, thanks @HarneenGoods
Thank you @jwfearn, I read the article, although I found this more interesting: youtube.com/watch?v=Am2is2QCvxY (it has two parts). It is a talk by Walter E. Brown: Modern Template Metaprogramming: A CompendiumHarneen
@Paul, thanks for the links to Walter's presentations. Pure gold!Goods
S
25

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

Streetlight answered 21/9, 2008 at 21:56 Comment(0)
M
12

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

Mu answered 21/9, 2008 at 21:56 Comment(3)
"What the heck, I can just pick a better language and get it for free" Ummm, what do you mean? I'm especially confused by "for free". And what other language did you had in mind?Jennifer
C++ template metaprogramming gives you all kind of new capabilities like passing types or list of types as arguments etc. Most of these capabilities are present in dynamically typed languages like python, with nicer syntax.Adios
If you're looking for a language that you can write correct programs quickly, Python is better than C++. If you need some of the other things C++ provides, adding Lisp-like capability to C++ may be better than trying to C++-ify another language.Gildea
S
7

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!

Sadonia answered 21/9, 2008 at 21:59 Comment(0)
U
5

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.

Univocal answered 21/9, 2008 at 21:54 Comment(0)
S
5

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.

Soupandfish answered 21/9, 2008 at 22:40 Comment(0)
E
4

google Alexandrescu, Modern C++ Design: Generic Programming and Design Patterns Applied

Esprit answered 21/9, 2008 at 21:53 Comment(0)
P
4

Veldhuizen's original papers were good. If you up for a whole book, then there's Vandevoorde's book "C++ Templates Complete Guide". And when you're ready for the master's course, try Alexandrescu's Modern C++ Design.

Pomiferous answered 21/9, 2008 at 21:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.