Click to See Complete Forum and Search --> : Curious template list


SeventhStar
December 1st, 2005, 09:31 AM
Hello,
I recently heard of a new interesting way to use templates, making a type list that gets disassembled during compilation.
Here is what I got from the info:

template <typename _A, typename _B>
struct type_list
{
typedef _A a;
typedef _B b;
};

struct end_type_list {};


Then with this you can declare a pseudo-variable that holds some types like this:

type_list <int, type_list<double, type_list<string, end_type_list>>> tlist;
As I see you must use RTTI to make this work...

This thing got my attention and I wish to learn more of it. Can someone point me to an internet resource about this usage of templates? I don't know the name of this technique and searching for it in google turned out useless...

Thanks in advance for any help.

Graham
December 1st, 2005, 01:26 PM
Try Alexi Alexandrescu's book Modern C++ Design

SeventhStar
December 1st, 2005, 03:26 PM
Thanks.
I took a look at some reviews of the book and it turned out it has this and a lot of other "cool" techniques in it.
I just ordered it.
Can't wait till it arrives tomorrow!

Graham
December 1st, 2005, 03:43 PM
A word of warning: Visual C++ 6 can't cope with it. Most of the tricks are beyond its meagre template capabilities (including typelists).

SeventhStar
December 1st, 2005, 04:16 PM
Yes I know. I first heard about it on a VS2003 presentation back in 2002 as a new feature of the 98% ISOC++ coverage of vc7.1. I was so pleased with this and other things about the compiler and environment that bought it without hasitation, and I have used it eversince. (I hope this is ok to say in the forum. I am not associated with Microsoft or anything, but am really in love with msdev2003 and vc7.1 :blush: )

So about the typelist:
Recently I was appointed to make a script parser. I was thinking of a way to do it and remembered the presentation, and my thoughts back then - that the typelist would be great for such a task. So I started looking for it... and you know the rest... :)

SuperKoko
December 2nd, 2005, 04:52 AM
As I see you must use RTTI to make this work...

I don't see any RTTI in type lists.

It is fundamentally a compile time thing.

Bob Davis
December 2nd, 2005, 07:21 AM
What would something like this be used for?

SeventhStar
December 2nd, 2005, 07:47 AM
I don't see any RTTI in type lists.

It is fundamentally a compile time thing.

Well when I posted this thread I had only a vague idea of what it was and using RTTI, was just am assumption, becaouse I could think of a way to find the list size. Now, after reading the book, I know that RTTI is not needed :)



What would something like this be used for?

I intend to use it to pass a various number of parameters of various types to a function. Thus parsing variables from a script is made relatevly easy.

marten_range
December 3rd, 2005, 11:25 AM
What would something like this be used for?

For instance boost::variant (which basically is a type-safe union that accepts C++ classes as well) is implemented using type-lists.
For instance:

boost::variant<int, std::string, double> m_my_variant;


Now boost::variant in the case above uses a type-list to keep track of what values of what types m_my_variant can hold. Also, it exposes this type-list as part of its interface so that generic code written by me (for instance) can traverse the types. I have used this specifically when writing a small MIDL parser (using boost::spirit). The parser pushed data on top of a stack of boost::anys. Later I found myself wanting to pop a value of the stack if it was any of for instance three different types (for instance: enum_holder, method_holder, property_holder). Using boost::variant and some meta-programming the end-result looked a bit like this:


typedef boost::variant<enum_holder, method_holder, property_holder> to_be_popped_type;

void f(stack & s)
{
boost::optional<to_be_popped_type> const l_popped(s.pop<to_be_popped_type>());

if( l_popped )
{
// do something
}
}


I also have experimented with an version of ATL that uses type-lists instead of macros to generate the implementation QueryInterface. The cool thing here is since you can iterate through these lists and sort them (in compile-time) you can implement divide-and-conquer algorithms to do fast lookup of guids.

As mentioned by others in this thread: Modern C++ Design: Generic Programming and Design Patterns Applied (http://www.amazon.com/exec/obidos/tg/detail/-/0201704315/qid=1133630229/sr=8-1/ref=pd_bbs_1/103-9421707-9928656?v=glance&s=books&n=507846) is an excellent source for inspiration and ideas. There is also a software library based on these ideas (by the author himself) called Loki: http://sourceforge.net/projects/loki-lib/

In boost there is a whole library called MPL that is dedicated to metaprogramming and type-lists. (www.boost.org) and there is also a book on that: C++ Template Metaprogramming : Concepts, Tools, and Techniques from Boost and Beyond (http://www.amazon.com/exec/obidos/tg/detail/-/0321227255/qid=1133630398/sr=2-1/ref=pd_bbs_b_2_1/103-9421707-9928656?v=glance&s=books)

Hope this helps