A new approach to specifying typelists?

D

Dave Branton

Hi,

I've been using Loki-style typelists extensivley in my code for a while
now, and have found them extremely useful, although somewhat annoying
to specify due to all the close angle brackets required.

Recently, though, I started using this syntax:

////////////////////////////////////////////////////////////////////////////
struct TypelistEnd {};

template<class _head, class _tail = TypelistEnd>
struct Typelist
{
typedef _head Head;
typedef _tail Tail;

template<class t>
struct Add
{
typedef Typelist<t, Typelist<_head, _tail> > Type;
};
};

//////////////////////////////////////////////////////////////////////////

Which allows the following construct to be used to specify
a typelist.

typedef Typelist
<Type1>::Add
<Type2>::Type::Add
<Type3>::Type::Add
<Type4>::Type::Add
<Type5>::Type::Add
<Type6>::Type::Add
<Type7>::Type tTypelist;

Which is much more convenient to use than the recusive approach,
provided
you don't mind that the list will end up in the opposite order.

A more complex approach allows the list to be constructed in the
correct order:

//////////////////////////////////////////////////////////////////////////

struct TypelistEnd {};
template<class TList, class NewItem> struct Append;

template<class _head, class _tail = TypelistEnd>
struct Typelist
{
typedef _head Head;
typedef _tail Tail;

template<class t>
struct Add
{
typedef typename Append<Typelist<_head, _tail>, t >::Result Type;
};
};

template<class H, class NewItem>
struct Append<Typelist<H, TypelistEnd>, NewItem>
{
typedef Typelist<H, Typelist<NewItem, TypelistEnd> > Result;
};

template<class H, class T, class NewItem>
struct Append<Typelist<H, T>, NewItem>
{
typedef Typelist<H, typename Append<T, NewItem>::Result > Result;
};


Is this new? Or am I just re-inventing something? Or is it, in some
way, bad?

-dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top