>

Whoops, I got the If backwards. Should be:

template <typename T>

struct Last

{

typedef typename If<

HasNext<T>::value,

Last<typename Link<T>::next_type>,

Identity<T>
>::type::type type;

};

Tom

Thanks, this was quite enlightning. I had already realized at some

point that the none-chosen branch was also instantized, had forgotten

about it by the time I posted my original post, and realized it again

yesterday, but I would never have come up with the idea to circumvent

it by using this delayed evaluation trick. Many thanks for pointing

this one out, I'm sure it will come in handy in other circumstances as

well :)

I also found a different solution yesterday. I planned to post it, so

people with similar questions will be able to find more through google

than I did. I just hadn't gotten around to do that yet, so I'll do it

now. My solution also solves it by stopping the recursion on the

not-taken branch (when HasNext is false), but I did it by defining a

template specialization for Last on NullType:

template <typename T>

struct Last {

typedef typename Link<T>::next_type next;

typedef typename Last<next>::type last;

typedef typename If<HasNext<T>::value, last, T>::type type;

};

template <>

struct Last<NullType{

typedef NullType type;

};

Note that I split up the nested templates into smaller steps, it seems

my compiler likes that better (g++ 3.3 and 4.0) - or I am just not

understanding all of templates syntax yet, which is also highly

possible ;) At any rate, I don't think that this solution is any worse

or better than the solution given by Tom Widmer.