CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Nov 2002
    Location
    Sofia, Bulgaria
    Posts
    661

    Lightbulb Curious template list

    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:

    Code:
    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:

    Code:
    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.
    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Curious template list

    Try Alexi Alexandrescu's book Modern C++ Design
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Join Date
    Nov 2002
    Location
    Sofia, Bulgaria
    Posts
    661

    Re: Curious template list

    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!
    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Curious template list

    A word of warning: Visual C++ 6 can't cope with it. Most of the tricks are beyond its meagre template capabilities (including typelists).

  5. #5
    Join Date
    Nov 2002
    Location
    Sofia, Bulgaria
    Posts
    661

    Re: Curious template list

    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 )

    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...
    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames

  6. #6
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Curious template list

    Quote Originally Posted by SeventhStar
    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.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  7. #7
    Join Date
    Jan 2001
    Posts
    588

    Re: Curious template list

    What would something like this be used for?

  8. #8
    Join Date
    Nov 2002
    Location
    Sofia, Bulgaria
    Posts
    661

    Re: Curious template list

    Quote Originally Posted by SuperKoko
    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


    Quote Originally Posted by Bob Davis
    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.
    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames

  9. #9
    Join Date
    Sep 2004
    Posts
    519

    Re: Curious template list

    Quote Originally Posted by Bob Davis
    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:
    Code:
    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:

    Code:
    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...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...glance&s=books)

    Hope this helps

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured