template metaprogramming: type lists of templates

Ok, I said in an earlier post that I wasn't going to cover metaprogramming, but I have to post just this one.. it's a bit of a useful technique, and I haven't seen it described anywhere else. I've been playing around with template metaprogramming in my spare time, and I ran accross an interesting C++ metaprogramming solution for a certain problem that, as far as I can tell, is rather novel. The issue that I had was that for various reasons, I wanted to store unspecified templates in a typelist (meaning that I wanted to use them as a template parameter so that I could specialize the types later). However, the classic type list uses typedefs, and you can't put an un-specialized template into a typedef. The way I got around this problem is to use a type factory. The type factory is a templatized class that declares a typedef of a concrete type using the type factor's template parameters:

template <class a, class b>

struct factory {

  typedef MyType<a, b> result;


To put these into a typelist, then, we just create a custom typelist that takes a template template parameter, and defines head as a type factory for the template template parameter type:



  template <class, class> class t_head,

  typename next







  template <class a, class b>

  struct head_factory {

    typedef t_head<a, b> result;


  typedef next tail;


It takes a lot of structure to create, but the practical upshot of it is that it seems to do a pretty good job of breaking up exponential expansions of type names in certain scenarios. One other downside which you may have noticed is that you have to create variations for templates that take different numbers of arguments. On the other hand, once you've done that, you can create a list that contain factories for templates that use different numbers of template parameters.