I am trying to teach myself template metaprogramming and I have been

trying to create lists of related types. I am however stuck when I want

to make a template that gives me the last type in a list. I started by

using a linked list of types with templates like:

struct MyClass1 {};

struct MyClass2 {};

struct MyClass3 {};

struct NullType {};

template <typename T>

struct Link {

typedef NullType next_type;

};

template <>

struct Link<MyClass1{

typedef MyClass2 next_type;

};

template <>

struct Link<MyClass2{

typedef MyClass3 next_type;

};

etc.

I have also devised a simple template to check if a particular template

has a next type or not:

template <typename T>

struct IsNullType {

enum { value = false };

};

template <>

struct IsNullType<NullType{

enum { value = true };

};

template <typename T>

struct HasNext

{

typedef typename Link<T>::next_type next;

enum { value = !IsNullType<next>::value };

};

Next I wanted to make a template that will give me the last element in

the list, i.e

Last<MyClass1>::type would be a typedef for MyClass3 in the example

above. What I am looking for is something equivalent to the following

pseudo-code:

type Last(input_type) {

if (has_next(input_type)) return Last(input_type->next);

else return input_type;

}

This suggests the need for recursive templates. Unfortunately, I can't

seem to get the syntax right. Can anybody point me back in the right

direction? I am aware that there are libraries that have already solved

this, like the Boost mpl, but I am more interested in the general

techniques involved to tackle something like this than a recommendation

of which library to use to solve this particular problem. Any help will

be greatly appreciated.

best regards Mark