How to determine the type of variable

How to correctly identify the type of a variable in C ++. I tried this to determine the type of a variable:

int a = 5; std::cout << typeid(a).name() << std::endl; 

And instead of the expected output of int, it gives you:

 i 

I am very confused why this is happening. Somehow it gives you only the first letter of the type declaring the variable. Int is not the only one ... and this:

  char a = 'A' std::cout << typeid(a).name() << std::endl; 

Example Program

Is there an easy workaround? Any help would be appreciated!

+6
source share
3 answers

There are two problems in the code:

Firstly, typeid(..).name() returns a specific implementation string, it can be any valid string, it can return "" for each type, it can even return different values ​​for each program execution (although I believe that value cannot change at runtime). GCC (and Clang?) Returns unreadable names, while Visual C ++ returns reasonable (in this case, int )

Second, if type a is a polymorphic type, typeid(a) returns a typeid corresponding to the dynamic type of a rather than the type used to declare a , use typeid(decltype(a)) instead.

Unfortunately, there is no standard way to get a type name in such a way that it is human-readable or the correct C ++ syntax. (see Cancel the result of std :: type_info :: name if you want it to work in GCC)

EDIT Using Boost, you can try std::cout << boost::typeindex::type_id<decltype(a)>().pretty_name() << std::endl; , see Obtaining Plausible and Distorted Type Names

+12
source

I mean, even if this is not clear. For example, if int is 32432423423403095590353095309530953, then it will always be the same. Therefore, I can easily set a function to return the type in which this variable ...

The results you get already do this. Perhaps this will help explain how exactly the C ++ implementation that you use gets the lines you see.

g ++ implements typeid(...).name() so that it returns a "ABI malformed" type name. This is a special way of representing the types that are used in compiled object files and libraries. If you compile C ++ code into an assembly, you will see β€œcharacters” that identify which functions or data the resulting assembly code is associated with. For example, take the function:

 int foo(double a, char b) { return a + b; } 

compile it into an assembly and you will see something like the following:

 _Z3foodc: .LFB0: .cfi_startproc movsbl %dil, %edi cvtsi2sd %edi, %xmm1 addsd %xmm1, %xmm0 cvttsd2si %xmm0, %eax ret .cfi_endproc 

The first line here is the "garbled" character, which is used to identify the int foo(double,char) function. It contains "Z3foo", which represents the name of the function, and then "d", which represents the type of the first argument, and "c", which represents the type of the second argument. This symbol is used to identify a function in a binary object file, in library indices, other compiled objects that want to link this function, etc.

You can also unmount characters using the C ++ comparison tool. This tool scans any text that you pass to it, looking for things that match the syntax algorithm, and converts them into something more than what these types are called in C ++ source code.

g ++ implements the Itanium C ++ ABI scheme. It is used by most unix platform compilers.

So, back to your code, guess how the type 'int' is represented in these characters.

The Itanium ABI indicates an additional function for dismantling. Here is an example of using it.

+3
source

Type names should not be what you expect from them. See this answer for the actual type name. Note. It works only with gcc and with an additional installation on clang.

With Visual C ++, you should call UnDecorateSymbolName, but apparently the ints are called int there

+2
source

All Articles