Boost logo

Boost Users :

From: Iulian M (eti_at_[hidden])
Date: 2006-02-08 06:28:08


Hello,

I'm trying to use boost::any to store a group of types. For value types
everything works as expected. The problem is that i need to store some
polymorphic types also. The following code explains what i'm trying to
accomplish.

#include <boost/any.hpp>

class A {}; //base calss
class B: public A {}; // derived class

int main()
{
        B *b = new B(); //create derived class
        boost::any n(b); // place inside boost::any
        
        /*
         other part of code where i only know n contains a pointer to a class           
derived from A
        this will throw bad_any_cast: typeid(A*) != typeid (B*)
        */
        A* a = boost::any_cast<A*>(n);  
}

Is there any way of making boost::any_cast work work ok with "convertible"
types? Or is there a way of storing different types of objects and having the
possibility of retrieving them as compatible objects ?

I've looked into any.hpp and found that it uses a typeid() equality test to
see if the cast can be made. Can't there be a is_convertible test insted , at
least for the case when is holding pointer to objects?

Iulian

-- 
Blessed is the man who, having nothing to say,
abstains from giving wordy evidence of the fact.
		-- George Eliot



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net