ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++返回一个变量类型的名称

2021-08-29 18:03:10  阅读:168  来源: 互联网

标签:返回 typeid return 变量 int C++ __ include type


typeid关键字

运行时获知变量类型名称,可以使用 typeid(变量).name()

但对于某些编译器只会返回

 

 

 

 

 

 

#include<iostream>
#include<typeinfo>
#include<cxxabi.h>

using namespace std;

template<typename type>
inline string type_of(type &x)
{
    return abi::__cxa_demangle(typeid(x).name(),0,0,0);
}

 

 

 

 

 1 #include<iostream>
 2 #include<typeinfo>
 3 #include<cxxabi.h>
 4 
 5 using namespace std;
 6 
 7 template<typename type>
 8 inline string type_of(type &x)
 9 {
10     return abi::__cxa_demangle(typeid(x).name(),0,0,0);
11 }
12 
13 unsigned long long a;
14 static short b;
15 __int128 c;
16 
17 inline void d(int e,float f)
18 {
19     return;
20 }
21 
22 struct node{
23     char g;
24     char h[100];
25     signed i;
26 }j[200];
27 
28 int k;
29 int *l=&k;
30 int **m=&l;
31 
32 signed main()
33 {
34     register int n;//寄存器变量只能在栈空间中使用 
35     cout<<"a:  "<<type_of(a)<<endl;
36     cout<<"b:  "<<type_of(b)<<endl;
37     cout<<"c:  "<<type_of(c)<<endl;
38     cout<<"d:  "<<type_of(d)<<endl;
39     cout<<"j:  "<<type_of(j)<<endl;
40     cout<<"j[1].g:  "<<type_of(j[1].g)<<endl;
41     cout<<"j[100].h:  "<<type_of(j[100].h)<<endl;
42     cout<<"j[201].h[101]:  "<<type_of(j[201].h[101])<<endl;
43     cout<<"j[201].i:  "<<type_of(j[201].i)<<endl;
44     cout<<"k:  "<<type_of(k)<<endl;
45     cout<<"l:  "<<type_of(l)<<endl;
46     cout<<"m:  "<<type_of(m)<<endl;
47     cout<<"n:  "<<type_of(n)<<endl;
48     return 0;
49 }

 

标签:返回,typeid,return,变量,int,C++,__,include,type
来源: https://www.cnblogs.com/void-null/p/15203118.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有