C/C++ 输出变量类型的方法讨论

使用typeid().name()输出变量类型

对于简单的变量类型,我们可以通过 typeid().name()来输出

#include <iostream>
#include <typeinfo> //所需头文件
using namespace std;

int main(){
    bool a;
    char b;
    int c;
    float d;
    double e;
    long f;
    cout<<typeid(a).name()<<endl;
    cout<<typeid(b).name()<<endl;
    cout<<typeid(c).name()<<endl;
    cout<<typeid(d).name()<<endl;
    cout<<typeid(e).name()<<endl;
    cout<<typeid(f).name()<<endl;
    return 0;
}

执行上述程序,您将会得到以下结果:b c i f d l

其含义为对应变量类型的简写。但是这种方式只适用于基础变量类型,当遇到结构体容器适配器(eg. vector) 时,将产生不易理解的结果,例如下述程序所示:

#include <iostream>
#include <typeinfo>
#include <stack>
#include <vector>
#include <string>
using namespace std;

int main(){
    enum color {RED, BLUE, WHITE, BLACK};
    struct {int a; char b; double c;} Student[10]; //匿名结构体数组 
    string str = "Mr_Xzz";
    stack<int> stackTest;
    vector<vector<float> > vecTest;
    
    cout<<typeid(color).name()<<endl;
    cout<<typeid(Student).name()<<endl;
    cout<<typeid(str).name()<<endl;
    cout<<typeid(stackTest).name()<<endl;
    cout<<typeid(vecTest).name()<<endl;
    return 0;
}

执行上述程序,您将会得到以下结果:
Z4mainE5color
A10_Z4mainEUt_
Ss
St5stackIiSt5dequeIiSaIiEEE
St6vectorIS_IfSaIfEESaIS1_EE

关于typeid().name()输出的格式为 [指针][名称空间][类别][模板]
[指针]:若是指针则输出P。
[名称空间]:若是std则输出St,若是自定义的名称空间则输出字符数及它的名字,并在开头加N,在结尾加E。
[类别]:若是自定义的名称空间则输出字符数及它的名字
[模板]:类型模板以I开头,以E结尾;常数模板以L开头,以E结尾。只有整型变量(int、char之类的)才能做为常数模板,浮点数不行。
引用自:https://www.cnblogs.com/zjushuiping/archive/2012/09/07/2675680.html

这里看不懂没关系,总之这说明gcc的输出是经过修饰的,而如果您用的是vc的编译环境,则不会出现上述修饰,直接返回完整类型名称,为了去掉gcc的修饰,可以使用__cxa_demangle

使用__cxa_demangle输出变量类型

其核心代码为:

#include <cxxabi.h> // 所需头文件 

char* realName = abi::__cxa_demangle(typeid(color).name(), nullptr, nullptr, nullptr);

仍然用之前的数据类型,这次使用__cxa_demangle,对比输出效果

#include <iostream>
#include <typeinfo>
#include <stack>
#include <vector>
#include <string>
#include <stdlib.h>
#include <cxxabi.h> // 所需头文件 
using namespace std;

int main(){
    enum color {RED, BLUE, WHITE, BLACK};
    struct {int a; char b; double c;} Student[10]; 
    string str = "Mr_Xzz";
    stack<int> stackTest;
    vector<vector<float> > vecTest;
     
    // char* realName = abi::__cxa_demangle(typeid(color).name(), nullptr, nullptr, nullptr);
    
    cout<<typeid(color).name()<<" => "<<abi::__cxa_demangle(typeid(color).name(), NULL, NULL, NULL)<<endl;
    cout<<typeid(Student).name()<<" => "<<abi::__cxa_demangle(typeid(Student).name(), NULL, NULL, NULL)<<endl;
    cout<<typeid(str).name()<<" => "<<abi::__cxa_demangle(typeid(str).name(), NULL, NULL, NULL)<<endl;
    cout<<typeid(stackTest).name()<<" => "<<abi::__cxa_demangle(typeid(stackTest).name(), NULL, NULL, NULL)<<endl;
    cout<<typeid(vecTest).name()<<" => "<<abi::__cxa_demangle(typeid(vecTest).name(), NULL, NULL, NULL)<<endl;
    
    return 0;
}

执行上述程序,可得结果如下:

  • Z4mainE5color => main::color
  • A10_Z4mainEUt_ => main::{unnamed type#1} [10]
  • Ss => std::string
  • St5stackIiSt5dequeIiSaIiEEE => std::stack<int, std::deque<int, std::allocator<int> > >
  • St6vectorIS_IfSaIfEESaIS1_EE => std::vector<std::vector<float, std::allocator<float> >, std::allocator<std::vector<float, std::allocator<float> > > >

__cxa_demangle可以实现复杂类型的直观输出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值