[any] Converting between types

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

Iulian M wrote:
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
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(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?
There's no easy way for the boost.any code to work with polymorphic types as you expect since there's no current way to know what types are convertible at runtime. (Perhaps when there's a boost.reflect) There's an easy workaround though, which is the same as you and the compiler would use if you had, for example, a "A* v". You can do the polymorphic conversion yourself, you just need to do it at injection time instead of extraction time: int main() { B *b = new B(); //create derived class boost::any n(static_cast(b)); // place inside boost::any A* a = boost::any_cast(n); } HTH. -- -- Grafik - Don't Assume Anything -- Redshift Software, Inc. - http://redshift-software.com -- rrivera/acm.org - grafik/redshift-software.com -- 102708583/icq - grafikrobot/aim - grafikrobot/yahoo

On Wednesday 08 February 2006 17:41, Rene Rivera wrote:
There's no easy way for the boost.any code to work with polymorphic types as you expect since there's no current way to know what types are convertible at runtime. (Perhaps when there's a boost.reflect)
Actually you gave me the solution. It just happens that in my project there is a type registry ( a registry where different objects register information about their type, information including base types ) - not quite a complete reflection system but has the info i need. I'll have to use unsafe_any_cast witch i've seen that it's not in the public interface of boost.any but i'll just have to live with it ( and hope it will never change ). it goes something like this 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 if( TypeManager::type( n.type() )->findBaseClass(a)) { A* a=unsafe_any_cast(n); } else throw ... } Thanks, Iulian -- I used to be an agnostic, but now I'm not so sure.

On Wed, 08 Feb 2006 13:41:57 -0200, Rene Rivera
There's no easy way for the boost.any code to work with polymorphic types as you expect since there's no current way to know what types are convertible at runtime. (Perhaps when there's a boost.reflect)
I think dynamic_any already works as the OP needs. Bruno

Bruno Martínez wrote:
On Wed, 08 Feb 2006 13:41:57 -0200, Rene Rivera
wrote: There's no easy way for the boost.any code to work with polymorphic types as you expect since there's no current way to know what types are convertible at runtime. (Perhaps when there's a boost.reflect)
I think dynamic_any already works as the OP needs.
Which is available where? -- -- Grafik - Don't Assume Anything -- Redshift Software, Inc. - http://redshift-software.com -- rrivera/acm.org - grafik/redshift-software.com -- 102708583/icq - grafikrobot/aim - grafikrobot/yahoo

On Wed, 08 Feb 2006 20:18:18 -0200, Rene Rivera
Bruno Martínez wrote:
On Wed, 08 Feb 2006 13:41:57 -0200, Rene Rivera
wrote: There's no easy way for the boost.any code to work with polymorphic types as you expect since there's no current way to know what types are convertible at runtime. (Perhaps when there's a boost.reflect)
I think dynamic_any already works as the OP needs.
Which is available where?
Here, although some pages are broken http://cpp-experiment.sourceforge.net/ Bruno

On Thursday 09 February 2006 14:35, Bruno Martínez wrote:
I think dynamic_any already works as the OP needs.
Which is available where?
Here, although some pages are broken
I've tryed dynamic_any but does not compile with latest boost. Also tryed modifying it but no luck. boost.mpl seems to have changed interface since dynamic_any was written. I also got very familiar with boost.any these days and realized it would be very nice if it had a policy based design that would allow the user to change the holder class and the way it is cast to the required type. -- Others will look to you for stability, so hide when you bite your nails.
participants (3)
-
Bruno Martínez
-
Iulian M
-
Rene Rivera