Jon Stewart on 22 Oct 2003 02:07:24 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

[ALACPP] emendation to previous bullshit spoken by yours truly


When we were reading _Modern C++ Design_, I complained once or twice that 
it was unfortunate that typelists necessitated template partial 
specialization support.

This statement is indeed wrong. Notice:

template<class T, class U>
struct Typelist {
	typedef T Head;
	typedef U Tail;
};

class NullType {};

#define TYPELIST_1(T1) Typelist< T1, NullType >
#define TYPELIST_2(T1, T2) Typelist< T1, TYPELIST_1(T2) >
// etc.


The definition of a typelist does not involve template partial spec. 
However, implementations of typelist "functions" are trickier. A simple 
Length isn't bad:

template <class TList> struct Length;

template<> struct Length<NullType> {
	enum { value = 0 };
};

template <class TList>
struct Length {
	enum { value = 1 + Length<TList::Tail>::value };
};

but others can be considerably trickier (than their partial spec 
counterparts):


template <class TList, unsigned int i>
struct TypeAt {
	typedef typename _detail::calc_at<i>::template inner<TList>::Ret 
Ret;
};

with:

namespace _detail {

template<uint index> struct calc_at;

template<>
struct calc_at<0> {
	template <class TList>
	struct inner {
		typedef TList::Head Ret;
	};
};

template<uint index>
struct calc_at {
	template <class TList>
	struct inner {
		typedef typename calc_at<index-1>::template 
inner<TList::Tail>::Ret Ret;
	};
};
} // namespace _detail


Some may notice that I've basically ripped this off from boost::tuple. 
But, hey, it works!



Jon
-- 
Jon Stewart                                 Advanced Los Angeles C++
stew1@xxxxxxxxxxx                           http://www.alacpp.org
_______________________________________________
alacpp mailing list
alacpp@xxxxxxxxxxx
http://lists.ellipsis.cx/mailman/listinfo/alacpp