Boost logo

Boost Users :

Subject: [Boost-users] bad_any_cast being thrown but no idea why
From: John Yamokoski (yamokosk_at_[hidden])
Date: 2009-10-31 17:15:38


I have been pulling my hair out over this bug for a day or two now.
Here's the synopsis. I have a boost::any object which is storing a
std::string. Any test I do to check if indeed the any object is
holding onto a std::string object verifies what I already know. For
instance, the following function always returns true for my boost::any
object:

bool is_string(const boost::any& operand)
{
        return ( operand.type() == typeid(std::string) );
}

Also if I just print out the typeinfo information for the boost::any
object is also shows that its holding onto a std::string:

std::stringstream ss;
ss << "any object: " << obj.type().name() << ", a string: " <<
typeid(std::string).name();

yields,

any object: Ss, a string: Ss

However, whenever I try and actually cast my object to a string
(boost::any_cast<std::string>(...)), I always get
"boost::bad_any_cast: failed conversion using boost::any_cast".

Could this possibly be a compiler issue? I saw one other thread where
"-fvisibility=hidden" was causing some problems for
boost::program_options and std::string. In my situation, I have a
library which is responsible for creating those boost::any objects.
Then its in a separately compiled library where I am getting the
problems.


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