Boost logo

Boost Users :

From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2008-06-22 11:49:35

> 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

Hope that helps. It is difficult to make suggestions, because we don't
if you are allowed to modify the interface.

Best Regards,

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at