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";
}