Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Identify type of a variable

How do i properly identify a type of variable in c++. I tried this to identify a type of variable :

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

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

i

I Am Very confused on why that is happening.. Its somehow giving you only the first letter of the type you are declaring the variable. Int is not the only one... also this:

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

Example Program

Is there a simple workaround to this? Any Help would be appreciated!

like image 645
amanuel2 Avatar asked Apr 29 '16 22:04

amanuel2


People also ask

How do you determine the variable type?

The typeof() method checks a variable's data type provided as a parameter. It returns the data type of that variable.

What are the 4 types of variables?

You can see that one way to look at variables is to divide them into four different categories ( nominal, ordinal, interval and ratio).

What are the 5 types of variables?

There are different types of variables and having their influence differently in a study viz. Independent & dependent variables, Active and attribute variables, Continuous, discrete and categorical variable, Extraneous variables and Demographic variables.

Which command is used to identify variables type?

To check the type of any variable data type, we can use the type() function.


2 Answers

There are two problems with your code,

Firstly typeid(..).name() returns an implementation defined string, it can be any valid string, it could return "" for every type, it could even return different values for each program execution (though I believe the value can't change during execution). GCC (and Clang?) return unreadable names, whereas Visual C++ returns reasonable ones (in this case int)

Secondly if the type of a is a polymorphic type, typeid(a) will return the typeid corresponding to the dynamic type of a and not the type that was used to declare a, instead use typeid(decltype(a)).

Unfortunately there is no standard way of getting the name of a type in a way that is human readable or correct C++ syntax. (see Unmangling the result of std::type_info::name if you want a way that works in GCC)

EDIT Using Boost, you could try std::cout << boost::typeindex::type_id<decltype(a)>().pretty_name() << std::endl;, see Getting human readable and mangled type names

like image 147
Isaac Avatar answered Sep 21 '22 18:09

Isaac


I Mean even if it is not understandable. For example if int is 32432423423403095590353095309530953, then its always gonna be same. So i can easily set a function to return what type the variable is...

The results you're getting already fulfill that. Perhaps it would help to explain how exactly the C++ implementation you're using gets the strings you're seeing.

g++ implements typeid(...).name() such that it returns the "ABI mangled" name of the type. This is a special way of representing types that is used in compiled object files and libraries. If you compile C++ code into assembly you'll see "symbols" that identify what function or data the resulting assembly code is related to. For example, take the function:

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

compile it to assembly, and you'll 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 'mangled' symbol that is used to identify the function int foo(double,char). It contains "Z3foo" that represents the function name, and then 'd' that represents the type of the first argument and 'c' that represents the type of the second argument. This symbol is used to identify the function in the binary object file, in library indices, by other compiled objects that want to link to this function, etc.

You can also demangle symbols using the c++filt tool. This tool scans through any text you pass it looking for things that conform to the mangling syntax, and converts them into something more like the way those types are named in C++ source code.

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

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

The Itanium ABI specifies an additional function for demangling. Here's an example using it.

like image 42
bames53 Avatar answered Sep 23 '22 18:09

bames53