Boost Users :
From: Scott McMurray (me22.ca+boost_at_[hidden])
Date: 2008-06-22 11:48:18
On Sun, Jun 22, 2008 at 07:22, Chris Ross <cross+boost_at_[hidden]> 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? 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)
This is the classic example of "why you didn't really want to use any
in the first place". If you're going to do anything with it (other
than pass it around), then you know something about its type, so any
more general than you really need. It's just like in unityped (aka
Dynamically Typed) languages.
Perhaps you can pass a boost::function<bool(any)> around with it?
That or just add some kind of is_something to the abstract class?
Barring that, I think you're stuck with a big if-else cascade checking
for types, but that's really not what you want, since then you're more
or less turning the any into a bad version of variant.
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