I l@ve RuBoard Previous Section Next Section

3.3 Linearizing Typelist Creation

Right off the bat, typelists are just too LISP-ish to be easy to use. LISP-style constructs are great for LISP programmers, but they don't dovetail nicely with C++ (to say nothing about the spaces between >s that you have to take care of). For instance, here's a typelist of integral types:



typedef Typelist<signed char,


      Typelist<short int,


         Typelist<int, Typelist<long int, NullType> > > >


   SignedIntegrals;


Typelists might be a cool concept, but they definitely need nicer packaging.

In order to linearize typelist creation, the typelist library (see Loki's file Typelist.h) defines a plethora of macros that transform the recursion into simple enumeration, at the expense of tedious repetition. This is not a problem, however. The repetition is done only once, in the library code, and it scales typelists to a large library-defined number (50). The macros look like this:



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


#define TYPELIST_4(T1, T2, T3, T4) \


  Typelist<T1, TYPELIST_3(T2, T3, T4) >


...


#define TYPELIST_50(...) ...


Each macro uses the previous one, which makes it easy for the library user to extend the upper limit, should this necessity emerge.

Now the earlier type definition of SignedIntegrals can be expressed in a much more pleasant way:



typedef TYPELIST_4(signed char, short int, int, long int)


   SignedIntegrals;


Linearizing typelist creation is only the beginning. Typelist manipulation is still very clumsy. For instance, accessing the last element in SignedIntegrals requires using SignedIntegrals::Tail::Tail::Head. It's not yet clear how we can manipulate typelists generically. It's time, then, to define some fundamental operations for typelists by thinking of the primitive operations available to lists of values.

    I l@ve RuBoard Previous Section Next Section