c++ - 如何打印类型名称(主题模板)
问题描述
嘿家伙这是我在文件 h 上的代码,我需要创建需要打印类型的方法。
#include <iostream>
#include <typeinfo>
using namespace::std;
template <class T>
class Set {
T* group;
int size_group;
public:
Set():group(NULL),size_group(0){};
Set(T*,int);
Set(const Set<T>&);
~Set();
void PrintObj()const;
bool isThere(const T&)const;
void getType()const;
};
template <class T>
void Set<T>::getType() const{
cout << "The type is: " << typeid(*this).name() << endl;
}
主要的:
#include "Set.h"
int main() {
int arr[] = {1,4,5,6,3,2};
int arr2[] = {5,2,6};
Set<int> j(arr,(sizeof(arr)/sizeof(arr[0]))),l(arr2,sizeof(arr2)/sizeof(arr2[0])),k;
j.getType();
}
输出:
The type is: 3SetIiE
我如何打印类型名称?这对我来说有点胡言乱语
解决方案
你看到的是一个被破坏的名字。您可以使用boost::core::demangle
它来对其进行分解(https://www.boost.org/doc/libs/1_73_0/libs/core/doc/html/core/demangle.html)。
std::cout << boost::core::demangle(typeid(*this).name()) << std::endl;
推荐阅读
- groovy - 在常规中,检查数组中是否至少一个元素不为空的最佳方法是什么?
- haskell - 在决定一棵树是否完整时,“方程有不同数量的参数”
- zeplin - 如何修复 Zeplin 中的间距功能”它显示:“NaNpx”而不是精确的 px 值
- json - JMeter 脚本到 Postman 的转换
- visual-studio-2013 - Visual Studio 调试器未加载正确的二进制文件
- scala - 在使用 scala-js-d3 应用更新之前过滤数据
- javascript - 尝试更改元素的样式并单击
- c++ - 在使用 nextafter C++ stl 时遇到问题
- python - 如何检查 url 在 Scrapy 中是否有效?
- java - Android自动备份不适用于不同的设备