Boost logo

Boost :

From: Remy Blank (remy.blank_REMOVE_THIS_at_[hidden])
Date: 2002-11-22 04:36:41

Hello Boosters,

I am trying to use dynamic_any to store either objects or pointers to
(polymorphic) objects.

I am able to extract a pointer to the base class of a contained object:

class B { };

class D: public B { };

void Test()
        any d(D());

        B* pb = extract<B>(&d);

This is alread quite useful. However the following code does not work:

void Test2()
        D d;
        any pd(&d);

        B* pb = extract<B*>(pd); // pb = 0

        char c;
        any AnyChar(c);

        int i = extract<int>(AnyChar); // bad_extract

So I have to know the exact type of the contained object and cannot
rely on implicit conversions (D* -> B* or char -> int) to work.
This is quite understandable from the fact that any wraps non-class
types into unrelated classes.

Now my question: is there a way to make these implicit conversions work?

My best answer at the moment: explicitly register conversion functions
from one type to the other in a map<pair<type_info const&, type_info const&>, void* (*)(void*)>
and look up the right function based on the type contained and the
type requested.

But there has to be a better way, hasn't it?

Best regards.

Remove anti-spam sequence in reply address for a timely response.

Boost list run by bdawes at, gregod at, cpdaniel at, john at