Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use typelists

I read about typelists in 'Modern C++ Design' and I understood it as some kind of union for types. By putting diffrent, non-related types in a typelist, one can use it to represent more than one type at once, without inheritance. I tested typelist in some simple functions with primitive types, but I couldn't get any of them to work.

Could someone tell me if my unterstanding of typelists is right and give an simple real world example how to use typelists in every day average code? Thanks in advance.

Btw, I'm using Windows and Visual Studio 2005 and its compiler.

EDIT: my examples are gone, I use a sandbox project in vs to test those things. But it was quiet similar to code in Dobbs tutorial:

void SomeOperation(DocumentItem* p)
{
    if (TextArea* pTextArea = dynamic_cast<TextArea*>(p))
    {
        ... operate on a TextArea object ...
    }
    else if (VectorGraphics* pVectorGraphics =
        dynamic_cast<VectorGraphics*>(p))
    {
        ... operate on a VectorGraphics object ...
    }
    else if (Bitmap* pBitmap = dynamic_cast<Bitmap*>(p))
    {
        ... operate on a Bitmap object ...
    }
    else
    {
        throw "Unknown type passed";
    }
}

This works but I don't see the advantage over inheritance which is capable to do the same. And dynamic cast don't work on primitive types. Is it possible to use it as a return value like:

typedef Typelist<int, string> mylist
mylist myfunction() {
    if(foo == bar)
        return 5;

    return "five";
}
like image 638
DaClown Avatar asked May 23 '09 16:05

DaClown


People also ask

What are the different types of lists in HTML?

HTML Lists 1 Unordered HTML List. An unordered list starts with the <ul> tag. Each list item starts with the <li> tag. 2 Ordered HTML List. An ordered list starts with the <ol> tag. ... 3 HTML Description Lists. HTML also supports description lists. ... 4 HTML List Tags. For a complete list of all available HTML tags, visit our HTML Tag Reference.

How do you write a list of items in HTML?

Each list item starts with the <li> tag. An ordered list starts with the <ol> tag. Each list item starts with the <li> tag. HTML also supports description lists. A description list is a list of terms, with a description of each term.

How do you group a list in HTML?

HTML lists allow web developers to group a set of related items in lists. An unordered list starts with the <ul> tag. Each list item starts with the <li> tag. An ordered list starts with the <ol> tag. Each list item starts with the <li> tag.

What is a description list in HTML?

A description list is a list of terms, with a description of each term. The <dl> tag defines the description list, the <dt> tag defines the term (name), and the <dd> tag describes each term: For a complete list of all available HTML tags, visit our HTML Tag Reference.


1 Answers

The typelists are generic compile-time collections of types. If you use dynamic_cast, you are missing the point, because it should not be needed, because it is a static, compile time concept.

This works but I don't see the advantage over inheritance which is capable to do the same.

You cannot make any existing type inherit from anything you want. This is simply not feasible, because this existing type may be a built in type or a type from a library. Think of the typelists as extensions of lists of types (e.g. in std::pair) for any reasonable number of types (instead of just 2).

The typelists can be used to create a facility to pass around a set of arguments to a function. This is a piece of code that calls generalized functors of 5 parameters (another concept from Modern C++ design) with the arguments supplied in a tupe (yet another one) with the typelist that defines types of objects held in the tuple:

//functor is just a holder of a pointer to method and a pointer to object to call this 
//method on; (in case you are unfamiliar with a concept)
template<class R, class t0, class t1, class t2, class t3, class t4>
R call(Loki::Functor<R,LOKI_TYPELIST_5(t0, t1, t2, t3, t4
    )> func,
    Loki::Tuple<LOKI_TYPELIST_5(t0, t1, t2, t3, t4)> tuple)
{
    ///note how you access fields
    return func(Loki::Field<0>(tuple), Loki::Field<1>(tuple),
        Loki::Field<2>(tuple), Loki::Field<3>(tuple),
        Loki::Field<4>(tuple));
}

//this uses the example code
#include<iostream>
using namespace std;

int foo(ostream* c,int h,float z, string s,int g)
{
    (*c)<<h<<z<<s<<g<<endl;
    return h+1
}

int main(int argc,char**argv)
{
    Loki::Functor<int,LOKI_TYPELIST_5(ostream*, int, float, string, int)> f=foo;
    //(...)
    //pass functor f around
    //(...)
    //create a set of arguments
    Loki::Tuple<LOKI_TYPELIST_5(ostream*, int, float, string, int)> tu;
    Field<0>(tu)=&cout;
    Field<1>(tu)=5;
    Field<2>(tu)=0.9;
    Field<3>(tu)=string("blahblah");
    Field<4>(tu)=77;
    //(...)
    //pass tuple tu around, possibly save it in a data structure or make many 
    //specialized copies of it, or just create a memento of a call, such that 
    //you can make "undo" in your application; note that without the typelist 
    //you would need to create a struct type to store any set of arguments;
    //(...)
    //call functor f with the tuple tu
    call(f,tu);
}

Note that only with other concepts like tuples or functors the typelists start to be useful. Also, I have been experiencing Loki for about 2 years in a project and because of the template code (a lot of it) the sizes of executables in DEBUG versions tend to be BIG (my record was 35 MB or so). Also there was a bit of hit on the speed of compilation. Also recall that C++0x is probably going to include some equivalent mechanism. Conclusion: try not to use typelists if you don't have to.

like image 167
poliklosio Avatar answered Sep 21 '22 22:09

poliklosio