I am looking for a way to check boost::any data type. If i assign an int to boost::any like:
boost::any num1 = 5;
And then use typeid(num1).name() , i want my output to be "int" so that i can use it in a if else statement in the future. However for some reason it keeps giving me this as the data type: N5boost3anyE
#include <typeinfo>
#include <iostream>
#include <boost/any.hpp>
using namespace std;
int main()
{
int a;
double b;
float c;
bool d;
int16_t e;
int32_t f;
cout << " int: " << typeid(a).name() << endl;
cout << "sizeof(): " << sizeof(a) << endl << endl;
cout << " double: " << typeid(b).name() << endl;
cout << "sizeof(): " << sizeof(b) << endl << endl;
cout << " float: " << typeid(c).name() << endl;
cout << "sizeof(): " << sizeof(c) << endl << endl;
cout << " bool: " << typeid(d).name() << endl;
cout << "sizeof(): " << sizeof(d) << endl << endl;
cout << " int16_t: " << typeid(e).name() << endl;
cout << "sizeof(): " << sizeof(e) << endl << endl;
cout << " int32_t: " << typeid(f).name() << endl;
cout << "sizeof(): " << sizeof(f) << endl << endl;
boost::any num1 = 5;
cout << "typeid(num1).name(): " << typeid(num1).name() << endl;
boost::any num2 = "dog";
cout << "typeid(num2).name(): " << typeid(num2).name() << endl;
}
Output:
johns-Air:cpp jdoe$ ./main
int: i
sizeof(): 4
double: d
sizeof(): 8
float: f
sizeof(): 4
bool: b
sizeof(): 1
int16_t: s
sizeof(): 2
int32_t: i
sizeof(): 4
typeid(num1).name(): N5boost3anyE
typeid(num2).name(): N5boost3anyE
CodePudding user response:
You need to use boost::any
's member function type()
which returns a const std::type_info&
of the contained value
cout << "typeid(num1).name(): " << num1.type().name() << endl;