Boost logo

Boost Users :

Subject: Re: [Boost-users] bad_any_cast being thrown but no idea why
From: John Yamokoski (yamokosk_at_[hidden])
Date: 2009-11-01 11:58:36


>>>
>>> Hi John -
>>>
>>> The problem is that the RTTI id's are different between your lib
>>> and the executable. You can use the undocumented and very useful
>>> unsafe cast:
>>>
>>>       boost::unsafe_any_cast< std::string>( ... )
>>>
>>> Of course, this creates a problem since it is not safe and if the
>>> any doesn't contain a std::string there will be a surprise.
>>>
>>>
>>
>> Michael,
>>
>> Well its great to hear there's some sort of explanation for this.
>> Unfortunately, from my compiler, I get:
>>
>> no matching function for call to 'unsafe_any_cast(const boost::any&)'
>>
>> I believe I have installed Boost version 1.40. I will gladly downgrade
>> to an earlier version if unsafe_any_cast was finally eliminated in
>> 1.40. Otherwise is there another way to deal with RTTI ID issues? This
>> is the first I have heard of them. Ahhh.. so close though.
>>
>
> John -
>
> It is still in the interface:
>
> https://svn.boost.org/trac/boost/browser/tags/release/Boost_1_40_0/boost/any.hpp#L219
>
> Did you remember the template parameter?
>
>        boost::unsafe_any_cast< std::string>( ... )
>
> Do you have a small code snippet that reproduces the problem?
>
> michael
>

Got it! I wasn't passing in a pointer to the any object.. it should have been:

boost::unsafe_any_cast< std::string>( &obj )

Now its working! Michael thanks so much - you have saved some of my hair! ;)

btw, still curious about this RTTI stuff. Is there another way to
handle this other than doing unsafe casts?

J.D.


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