By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,404 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

A new approach to specifying typelists?

P: n/a
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 NewItemstruct 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

Nov 2 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.