C++ generic programming technique invented (or at least utilised and popularised) by
Andrei Alexandrescu, much visible in his new book
Modern C++ Design and in the
Loki library which goes with the book.
A typelist is a dummy type that abstracts the notion of a list of types. They're used to perform computation on types (not values!) at compile time. The basic mechanism is quite simple, and is familiar to any Lisp programmer: it's just a car/cdr list terminated by a special sentinel value (the ugly capitalisation is part of Alexandrescu's coding style, unfortunately):
class NullType {}; // Sentinel value
template <typename H, class T>
struct Typelist {
typedef H Head;
typedef T Tail;
};
He then provides
macros to build typelists with varying numbers of arguments:
#define TYPELIST_1(T1) Typelist<T1,NullType>
#define TYPELIST_2(T1,T2) Typelist<T1, TYPELIST_1(T2)>
#define TYPELIST_3(T1,T2,T3) Typelist<T1, TYPELIST_2(T2,T3)>
// ... goes on to TYPELIST_50 like this, and you could
// add your own if you needed more!
OK, so we can build lists of types.
BUT what can we do with them? Unleash the power of C++ templates, of course. See some examples of computations with typelists.