
There is a bug in the sample code which cause me a lot of grief in understanding how boost::any functions, this morning. At: http://www.boost.org/doc/html/any/s02.html The code: --- bool is_string(const boost::any & operand) { return any_cast<std::string>(&operand); } should read: bool is_string(const boost::any & operand) { return any_cast<std::string *>(&operand); } --- Notice the addition of the asterisk. Thanks, Michael Goldshteyn

On Wed, 23 Mar 2005 09:01:51 -0600 "Michael Goldshteyn" <mgoldshteyn@earthlink.net> wrote:
There is a bug in the sample code which cause me a lot of grief in understanding how boost::any functions, this morning. At:
http://www.boost.org/doc/html/any/s02.html
The code: --- bool is_string(const boost::any & operand) { return any_cast<std::string>(&operand); }
should read:
bool is_string(const boost::any & operand) { return any_cast<std::string *>(&operand); } ---
Notice the addition of the asterisk.
The original example looks fine to me. Why do you think it is incorrect?

"Jody Hagins" <jody-boost-011304@atdesk.com> wrote in message news:20050323104956.15cd7630.jody-boost-011304@atdesk.com...
On Wed, 23 Mar 2005 09:01:51 -0600 "Michael Goldshteyn" <mgoldshteyn@earthlink.net> wrote:
There is a bug in the sample code which cause me a lot of grief in understanding how boost::any functions, this morning. At:
http://www.boost.org/doc/html/any/s02.html
The code: --- bool is_string(const boost::any & operand) { return any_cast<std::string>(&operand); }
should read:
bool is_string(const boost::any & operand) { return any_cast<std::string *>(&operand); } ---
Notice the addition of the asterisk.
The original example looks fine to me. Why do you think it is incorrect?
The original example casts from an any pointer to an std::string, using any_cast, and returns the result implicitly cast to a boolean. So: 1. What does it mean to cast from an any pointer to an std::string, using any_cast? 2. What does it mean to implicitly convert a string, the value returned from any_cast<string> to a boolean?

On Wed, 23 Mar 2005 15:37:34 -0600 "Michael Goldshteyn" <mgoldshteyn@earthlink.net> wrote:
"Jody Hagins" <jody-boost-011304@atdesk.com> wrote in message news:20050323104956.15cd7630.jody-boost-011304@atdesk.com...
On Wed, 23 Mar 2005 09:01:51 -0600 "Michael Goldshteyn" <mgoldshteyn@earthlink.net> wrote:
There is a bug in the sample code which cause me a lot of grief in understanding how boost::any functions, this morning. At:
http://www.boost.org/doc/html/any/s02.html
The code: --- bool is_string(const boost::any & operand) { return any_cast<std::string>(&operand); }
should read:
bool is_string(const boost::any & operand) { return any_cast<std::string *>(&operand); } ---
Notice the addition of the asterisk.
The original example looks fine to me. Why do you think it is incorrect?
The original example casts from an any pointer to an std::string, using any_cast, and returns the result implicitly cast to a boolean. So:
1. What does it mean to cast from an any pointer to an std::string, using any_cast?
In the referenced example, any_cast<std::string>(&operand); will return a std::string const * if the object inside the any is a std::string, and 0 otherwise. For example... #include <boost/any.hpp> #include <string> #include <cstring> #include <cassert> int main(int, char *[]) { // Stuff a ptr-to-const-char into any boost::any any = (char const *)"Hello"; assert(boost::any_cast<char const *>(&any) != 0); assert(std::strcmp( *boost::any_cast<char const *>(&any), "Hello") == 0); assert(boost::any_cast<std::string>(&any) == 0); std::string hello = "hello"; // Stuff a std::string into the any any = hello; assert(boost::any_cast<std::string>(&any) != 0); assert(*boost::any_cast<std::string>(&any) == hello); // Stuff a ptr-to-std::string into the any any = &hello; assert(boost::any_cast<std::string>(&any) == 0); assert(boost::any_cast<std::string*>(&any) != 0); assert(**boost::any_cast<std::string*>(&any) == hello); return 0; }
2. What does it mean to implicitly convert a string, the value returned from any_cast<string> to a boolean?
Hmmm. Let's see if making the example more explicit will help. The return type of boost::any_cast<std::string>(&any) is a "std::string const *" so the original example is equivalent to... bool is_string(const boost::any & operand) { std::string const * ptr_to_string = boost::any_cast<std::string>(&operand); return ptr_to_string != 0; } Does that make sense, or just make it more confusing?!?

"Jody Hagins" <jody-boost-011304@atdesk.com> wrote in message news:20050323172054.5743c220.jody-boost-011304@atdesk.com...
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 uses: 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 fails 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. Thanks, Michael Goldshteyn

On Wed, 23 Mar 2005 16:42:39 -0600 "Michael Goldshteyn" <mgoldshteyn@earthlink.net> wrote:
In any case, I now understand what it is doing. Perhaps this difference from dynamic cast should be documented more explicitly.
It is actually a little more sneaky than that. However, until changes are made (I believe some ref overloads are planned for 1.33), casting a ptr-to-any always returns a ptr-to-the-real-type and casting a ref-to-any always returns a COPY of the real-type, or throws an exception if the conversion can not be done.
participants (2)
-
Jody Hagins
-
Michael Goldshteyn