C++ typeid Keyword
Example
Check if the type of a variable is int:
int x = 5;
double y = 9.99;
cout << (typeid(x) == typeid(int)) << "\n";
cout << (typeid(y) == typeid(int));
Definition and Usage
The typeid keyword returns information about the type of a variable or expression. To use it, include the <typeinfo> header.
The most common use is to compare two types with ==. In the example above, typeid(x) == typeid(int) is true (1), because x is an int. y is a double, so the second comparison is false (0).
There is also a name() function, like typeid(x).name(), but the text it returns is different on different compilers.
Related Pages
Read more about data types in our C++ Data Types Tutorial.