Boost logo

Boost Users :

Subject: Re: [Boost-users] [any] Is it possible to support polymorphism in boost::any?
From: Germán Diago (germandiago_at_[hidden])
Date: 2010-09-06 12:31:50


I had exactly the same problem with some code of mine.

The code was a factory like this

typedef Factory<std::function<boost::any ()> > Factory_t;

I wanted to store objects derived from a class (in this case, but I also wanted
to support types not rooted in the same object hierarchy) and I was
surprised that
it failed. I think the right thing would do to work under these
assumptions as well.

2010/9/6 Tang Jiang Jun <tangjiangjun_at_[hidden]>:
> Hello,
> It seems that boost::any does not support polymorphism in the following
> sample.
> class A {};
> class B : public A {};
> void foo( boost::any& _any )
> {
> try
> {
> A* p = any_cast< A* >( _any );  <-- Only succeed when _any contain a pointer
> to a object that is exactly class A, but not any object that
> is descendant of class A, like B.
> do_some_thing( p );
> }
> catch( const bad_any_cast& )
> {
> }
> }
> void goo()
> {
> A* pa = new B;
> boost::any a = pa;
> foo( a );
> }
> Is it possible to make it support this polymorphism usage? Because
> boost::any is a type-safe replacement of void*, I hope that boost::any has
> the same polymorphic behavior as void*.
> Regards,
> Tang Jiang Jun
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>


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