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";
}
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.
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:
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.
Typelists are a way of passing "lists of parameters" to template meta programs that "execute" as part of the compilation process.
As such, they can be used to generate some sort of "union" type, but this is only one possible use.
For a "real-world" example: We used typelists as a way to generate the "QueryInterface" method automatically when implementing COM objects in the Comet library.
It allowed you to write code like this:
In this example, "make_list" was a template used to generate a "type list", which the implement_qi template could then "enumerate over" to generate the appropriate QueryInterface code.