1. append操作
append物可以是NullType、Type T、另一个Typelist
template struct Append;
template <> struct Append // NullType
{
typedef NullType Result;
};
template struct Append // Type T
{
typedef Typelist Result;
};
template // Typelist
struct Append >
{
typedef Typelist Result;
};
template
struct Append , T>
{
typedef Typelist
typename Append ::Result>
Result;
};
2. erase操作
template struct Erase;
template
struct Erase
{
typedef NullType Result;
};
template
struct Erase , T>
{
typedef Tail Result;
};
template
struct Erase , T>
{
typedef Typelist
typename Erase ::Result>
Result;
};
3. 去重操作
先对尾部去重,再从结果里移除Head,合并即可(呵呵,编译器辛苦了)
template struct NoDuplicates;
template <> struct NoDuplicates
{
typedef NullType Result;
};
template
struct NoDuplicates< Typelist >
{
private:
typedef typename NoDuplicates ::Result L1;
typedef typename Erase ::Result L2;
public:
typedef Typelist Result;
};
4. 反转操作
先对尾部反转,再将Head append到结果上即可
template struct Reverse;
template <>
struct Reverse
{
typedef NullType Result;
};
template
struct Reverse< Typelist >
{
typedef typename Append<
typename Reverse ::Result, Head>::Result Result;
};