Boost logo

Boost :

From: Michael Goldshteyn (mgoldshteyn_at_[hidden])
Date: 2005-03-23 17:42:39

"Jody Hagins" <jody-boost-011304_at_[hidden]> wrote in message

> Does that make sense, or just make it more confusing?!?

OK, now I understand where my confusion was coming from. I associated the
behavior of boost::any_cast with the way dynamic_cast works. Dynamic cast

dynamic_cast<Derived &>(base); // A bad_cast exception gets thrown if the
conversion is not possible
dynamic_cast<Derived *>(pBase); // A zero is returned if the conversion

now, with any_cast:

any_cast<std::string>(myAny); // Throws bad_any_cast if not possible,
similar to dynamic_cast with references
any_cast<std::string>(&myAny); // Attempts to cast &myAny to a string
pointer, returning zero on failure.

The final example is what confused me, when I tried to relate it to
dynamic_cast. I think that the template argument is counterintuitive. Maybe
it should have been implemented as:

any_cast<std::string *>(&myAny); // Instead, in order to more resemble
dynamic_cast behavior with pointers

In any case, I now understand what it is doing. Perhaps this difference from
dynamic cast should be documented more explicitly.


Michael Goldshteyn

Boost list run by bdawes at, gregod at, cpdaniel at, john at