1.1. Discovering types
The typeid operator enables you to discover the type of an expression. To obtain the type of an expression, you simply write typeid (expression) , and this results in an object of type type_info that encapsulates the type of the expression. Suppose that you have defined variables x and y that are of type int and type double , respectively. The expression typeid(x*y) results in a type_info object representing the type of x*y , which by now you know to be double. Because the result of the typeid operator is an object, you can ’ t write it to the standard output stream just as it is. However, you can output the type of the expression x*y like this:
cout < < "The type of x*y is " < < typeid(x*y).name() < < endl;
This will result in the output:
the type of x*y is double
When you use the typeid operator, you must add a #include directive for the type info header file to your program:
#include < typeinfo >
This provides the definition for the type_info type that the typeid operator returns. You won’t need to use the typeid operator very often, but when you do need it, it is invaluable.