Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typeinfo / typeid output

Tags:

c++

types

gcc

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

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.

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;
}

Is there a symbol list anywhere?

like image 476
aLostMonkey Avatar asked Oct 07 '10 16:10

aLostMonkey


People also ask

What does Typeid return?

The typeid operator returns an lvalue of type const type_info that represents the type of our value.

What does Typeid return in C++?

The typeid operator returns an lvalue of type const std::type_info that represents the type of expression expr. You must include the standard template library header <typeinfo> to use the typeid operator. Classes A and B are polymorphic; classes C and D are not.

What is the use of Typeid () function?

The typeid operator allows the type of an object to be determined at run time. The result of typeid is a const type_info& . The value is a reference to a type_info object that represents either the type-id or the type of the expression, depending on which form of typeid is used.

Is Typeid slow?

typeid() can be much faster than other type checks if all the stars are aligned, or it can be extremely slow. typeid() requires the compiler to determine if a pointer is null, so if you already know your pointer is non-null, it's more efficient to use typeid() on a reference instead.


1 Answers

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*);
    //...
}
like image 191
UncleBens Avatar answered Oct 14 '22 01:10

UncleBens