Stephen Gross wrote:
In the following snippet, I've got a simple inherited system (base and derived). I create a boost::any and assign it a derived instance. Then I try to any_cast it to the base. It compiles, but at runtime it segfaults. Is this expected behavior?
Given your example, yes it's expected. The "any_cast<base>(&a)" returns NULL and dies when it tries to dereference that NULL.
Is there a way to make this work?
Not with value based objects in the boost::any. But you can store managed objects instead. For example: ==== void test1() { boost::shared_ptr<foo1> f(new foo1); boost::any a = boost::static_pointer_cast<base>(f); f = boost::dynamic_pointer_cast<foo1>( boost::any_cast< boost::shared_ptr<base> >(a)); if (f) f->go(); } ==== -- -- Grafik - Don't Assume Anything -- Redshift Software, Inc. - http://redshift-software.com -- rrivera/acm.org - grafik/redshift-software.com -- 102708583/icq - grafikrobot/aim - Grafik/jabber.org