Boost logo

Boost Users :

From: Kenny Riddile (kfriddile_at_[hidden])
Date: 2008-06-22 13:00:27


Ovanes Markarian wrote:
>> Does anyone have a suggestion as to how I can get a value out of a boost::any for which I don't know the specific type stored in it?
> if you know the set of types you operate on consider using
> boost::variant which supports visitor pattern for dispatching of
> currently stored type.
>
>
>> A subclass method is returning the boost::any, which is conceptually a bool, but different subclasses may implement it as a bool, int, int64_t, etc. I can just static-cast them to bool if I can get the value out of the boost::any, but boost::any_cast gives a bad_any_cast unless I cast it to exactly the right type (which as I noted, the super-class doesn't know)
>>
>
> Another approach could be to introduce an additional level of
> indirection. Another Holder type of your types and implement for this
> type operator<<. Than you can always retrieve from boost::any your
> holder_type instance and apply to your holder_type instance
> lexical_cast.
>
>
> Hope that helps. It is difficult to make suggestions, because we don't
> if you are allowed to modify the interface.
>
> Best Regards,
> Ovanes
If you have "the Boost book" (Beyond the C++ Standard Library: An
Introduction to Boost), I think chapter 6 implements something like this
to allow streaming of boost::any types.


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