如何获取 boost::any 变量的数据类型?
How can I get the data type for boost::any variable?
我正在寻找检查 boost::any 数据类型的方法。
如果我将一个 int 分配给 boost::any,例如:
boost::any num1 = 5;
然后使用 typeid(num1).name() ,我希望我的输出为“int”,以便将来可以在 if else 语句中使用它。但是由于某种原因,它一直给我这个作为数据类型: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;
}
输出:
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
您需要使用 boost::any
的成员函数 type()
其中 returns 包含值 const std::type_info&
的
cout << "num1.type().name(): " << num1.type().name() << endl;
我正在寻找检查 boost::any 数据类型的方法。 如果我将一个 int 分配给 boost::any,例如:
boost::any num1 = 5;
然后使用 typeid(num1).name() ,我希望我的输出为“int”,以便将来可以在 if else 语句中使用它。但是由于某种原因,它一直给我这个作为数据类型: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;
}
输出:
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
您需要使用 boost::any
的成员函数 type()
其中 returns 包含值 const std::type_info&
的
cout << "num1.type().name(): " << num1.type().name() << endl;