是否有编译器以可读的方式返回类型的名称(或提供该功能或工具的库)。本质上,我想要的是对应于您在源代码中编写的类型表达式的字符串。
发布于 2012-07-29 21:19:15
typeid(var).name()就是你要找的东西。尽管不同的编译器输出是不同的...例如,对于gcc,int的输出是i,对于unsigned,输出是j。下面是一个小测试程序:
#include <iostream>
#include <typeinfo>
struct A { virtual ~A() { } };
struct B : A { };
class C { };
class D : public C { };
int main() {
B b;
A* ap = &b;
A& ar = b;
std::cout << "ap: " << typeid(*ap).name() << std::endl;
std::cout << "ar: " << typeid(ar).name() << std::endl;
D d;
C* cp = &d;
C& cr = d;
std::cout << "cp: " << typeid(*cp).name() << std::endl;
std::cout << "cr: " << typeid(cr).name() << std::endl;
int e;
unsigned f;
char g;
float h;
double i;
std::cout << "int:\t" << typeid(e).name() << std::endl;
std::cout << "unsigned:\t" << typeid(f).name() << std::endl;
std::cout << "char:\t" << typeid(g).name() << std::endl;
std::cout << "float:\t" << typeid(h).name() << std::endl;
std::cout << "double:\t" << typeid(i).name() << std::endl;
}发布于 2018-10-26 12:50:12
在搜索这个问题时,我刚刚在boost库中找到了我需要的东西:boost::core::demangle
在我的例子中,它是用于捕获异常的:
try {
//...
} catch (std::exception& e) {
boost::core::scoped_demangled_name demangled(typeid(e).name());
std::cerr << "ERROR: " << demangled.get() << ": " << e.what() << std::endl;
}发布于 2018-10-05 00:15:49
下面是我用来检查类型的一个小类。如果需要,您可以向其添加更多类型。它不能检查数组类型、映射或任何太高级的东西。如果需要,您可以添加更多类型,甚至可以添加您自己的自定义类型。这只是一个小样本。
此外,getType函数就是您要查找的函数。根据匹配的类型,您可以自定义输出字符串。我省略了include部分,所以你只需要找到正确的include文件,因为这个库是我更大的库的一部分。
/* header.hpp */
class _cType
{
public:
_cType();
~_cType();
template<class T, class U>
static bool isSame(T &vl, U &vr);
template<class T>
static bool isInt(T &v);
template<class T>
static bool isUInt(T &v);
template<class T>
static bool isFloat(T &v);
template<class T>
static bool isLong(T &v);
template<class T>
static bool isDouble(T &v);
template<class T>
static bool isBool(T &v);
template<class T>
static bool isString(T &v);
template<class T>
static bool isChar(T &v);
template<class T>
static std::string getType(T &v);
};
extern _cType Type;/* source.cpp */
_cType::_cType(){};
template<class T, class U>
bool _cType::isSame(T &vl, U &vr){
return ( typeid(vl) == typeid(vr) );
}
template<class T>
bool _cType::isInt(T &v){
return typeid(v) == typeid(int);
}
template<class T>
bool _cType::isUInt(T &v){
return typeid(v) == typeid(unsigned int);
}
template<class T>
bool _cType::isFloat(T &v){
return typeid(v) == typeid(float);
}
template<class T>
bool _cType::isLong(T &v){
return typeid(v) == typeid(long);
}
template<class T>
bool _cType::isDouble(T &v){
return typeid(v) == typeid(double);
}
template<class T>
bool _cType::isBool(T &v){
return typeid(v) == typeid(bool);
}
template<class T>
bool _cType::isString(T &v){
return typeid(v) == typeid(std::string);
}
template<class T>
bool _cType::isChar(T &v){
return typeid(v) == typeid(char);
}
template<class T>
std::string _cType::getType(T &v){
if ( typeid(v) == typeid(int) ) return "int";
else if ( typeid(v) == typeid(unsigned int) ) return "unsigned int";
else if ( typeid(v) == typeid(float) ) return "float";
else if ( typeid(v) == typeid(long) ) return "long";
else if ( typeid(v) == typeid(double) ) return "double";
else if ( typeid(v) == typeid(char) ) return "char";
else if ( typeid(v) == typeid(std::string) ) return "std::string";
else if ( typeid(v) == typeid(bool) ) return "bool";
else return std::string("User defined or unknown type: ") + typeid(v).name();
}
_cType::~_cType(){};
_cType Type;https://stackoverflow.com/questions/11709455
复制相似问题