目录
1. 前言
在模板编程中经常要跟不同的模板类型打交道,编译器的类型推导规则比较复杂,大家在编写代码时难免会出现自己以为的模板参数类型和编译器推导出来的类型不一致的时候。
能够主动获取一个类型的类型名称,并能够输出出来,将会非常方便。
本文将介绍一款方便的小工具帮助大伙便捷地获取一个类型的类型名称。
2. 代码演示
首先需要安装Boost库,可以参考这个安装教程,非常简单。
下面是代码演示:
#include <iostream>
#include <string>
#include <boost/type_index.hpp>
using namespace std;
template<typename T>
class TypeGetter
{
static string getTypeName()
{
return boost::typeindex::type_id_with_cvr<T>().pretty_name();
}
public:
static string name;
};
template<typename T>
string Typ