I have a boost::any object, and I would like to check its type.
typedef boost::any Value;
Value a = 12;
if(a.type() == typeid(int)) {
std::cout << boost::any_cast<int>(a) << std::endl;
}
This is easy enough when the type is defined, however how would I achieve a the same result when the type is not defined (i.e. because its value has not been set yet).
Value b;
if(b is undefined) {
std::cout << "b is not defined" << std::endl;
}
boost::any::emptywill returntrueif there is no value.Demo
Alternatively, you can use
boost::any::typelike you did in the first example and, if there's no value, it will returntypeid(void):Demo 2