typeinfo / typeid输出 [英] typeinfo / typeid output

查看:260
本文介绍了typeinfo / typeid输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正试图调试一段简单的代码,并希望看到一个特定的变量类型在程序中如何改变。

I'm currently trying to debug a piece of simple code and wish to see how a specific variable type changes during the program.

我使用的是typeinfo头文件,所以我可以利用typeid.name()。我知道typeid.name()是编译器特定的,因此输出可能不是特别有帮助或标准。

I'm using the typeinfo header file so I can utilise typeid.name(). I'm aware that typeid.name() is compiler specific thus the output might not be particularly helpful or standard.

我使用 GCC ,但我找不到潜在的输出列表,尽管搜索,假设有一个typeid输出符号的列表。我不想根据输出做任何类型的转换或操作任何类型的数据,只需跟随它的类型。

I'm using GCC but I cannot find a list of the potential output despite searching, assuming a list of typeid output symbols exist. I don't want to do any sort of casting based on the output or manipulate any kind of data, just follow its type.

#include <iostream>
#include <typeinfo>

int main()
{ 
    int a = 10;
    cout << typeid(int).name() << endl;
}

有符号列表吗?

推荐答案

我不知道这样的列表是否存在,但是你可以让一个小程序打印出来: p>

I don't know if such a list exists, but you can make a small program to print them out:

#include <iostream>
#include <typeinfo>

#define PRINT_NAME(x) std::cout << #x << " - " << typeid(x).name() << '\n'

int main()
{
    PRINT_NAME(char);
    PRINT_NAME(signed char);
    PRINT_NAME(unsigned char);
    PRINT_NAME(short);
    PRINT_NAME(unsigned short);
    PRINT_NAME(int);
    PRINT_NAME(unsigned int);
    PRINT_NAME(long);
    PRINT_NAME(unsigned long);
    PRINT_NAME(float);
    PRINT_NAME(double);
    PRINT_NAME(long double);
    PRINT_NAME(char*);
    PRINT_NAME(const char*);
    //...
}

这篇关于typeinfo / typeid输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆