May I know is there any way to determine parent class from boost::any?
#include <iostream>
#include <boost/any.hpp>
class a {
public:
virtual ~a() {}
};
class b : public a {
};
bool is_class_a(const boost::any& any)
{
return boost::any_cast<a>(&any) != 0;
}
bool is_class_a_v2(const boost::any& any)
{
try
{
boost::any_cast<a>(any);
return true;
}
catch(const boost::bad_any_cast &)
{
return false;
}
}
int main()
{
std::cout<< is_class_a(b())<< std::endl; // return 0. but i wish to return 1.
std::cout<< is_class_a(a())<< std::endl; // return 1.
std::cout<< is_class_a_v2(b())<< std::endl; // return 0. but i wish to return 1.
std::cout<< is_class_a_v2(a())<< std::endl; // return 1.
}