正文
直接上代码
#include <type_traits>
#include <typeinfo>
#include <memory>
#include <string>
#include <cstdlib>
#include <iostream>
#ifndef _MSC_VER
#include <cxxabi.h>
#endif
using namespace std;
template <class T>
string type_name()
{
typedef typename remove_reference<T>::type TR;
unique_ptr<char, void (*)(void *)> own(
#ifndef _MSC_VER
abi::__cxa_demangle(typeid(TR).name(), nullptr, nullptr, nullptr),
#else
nullptr,
#endif
free);
string r = own != nullptr ? own.get() : typeid(TR).name();
if (is_const<TR>::value)
r += " const";
if (is_volatile<TR>::value)
r += " volatile";
if (is_lvalue_reference<T>::value)
r += "&";
else if (is_rvalue_reference<T>::value)
r += "&&";
return r;
}
int main()
{
int a = 1;
const int &b = a;
cout << type_name<decltype(b)>() << endl;
}
定义了一个模板函数type_name(),可以对传入的模板类型T进行类型判断,结合指针、左值/右值引用、常类型,准确得出变量的类型。在调用该函数时使用了decltype关键字,传入待确定的变量,然后输出变量类型。
以上运行的结果为
int const&
- 如果要输出int指针的类型:
int main()
{
int a = 1;
int* b = &a;
cout << type_name<decltype(b)>() << endl;
}
结果为:
int*
可以看出该函数得出的结果是非常准确而又精简的。
与传统方法的对比
传统输出C++类型的方法是使用typeid(var).name(),其弊端在于输出的结果不完整或者过于冗长。
例如:
1. 传统方法打印int类型
#include <iostream>
int main()
{
int a = 10;
std::cout << typeid(a).name() << std::endl;
}
运行结果为:
i
只得到了int的缩写i。
2. 传统方法打印string类型
#include <iostream>
#include <string>
int main()
{
std::string a = "abc";
std::cout << typeid(a).name() << std::endl;
}
运行结果为:
NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
过于冗长,不便于看出类型名称。
3. 传统方法打印引用类型
#include <iostream>
int main()
{
int a0 = 23;
int& a = a0;
std::cout << typeid(a).name() << std::endl;
}
运行结果为:
i
并没有准确地输出我们想要的结果。
总结
使用了稍微复杂的模板函数,可以准确输出C++的变量类型,而非大多数人提到的type_id(var).name()打印出来的结果有各种不恰当的地方。
961

被折叠的 条评论
为什么被折叠?



