Updated April 17, 2023
Introduction to typeid in C++
In C++, typeid is an operator that is used to retrieve the runtime or dynamic type information of an object. Objects can be a variable type, object type, or expression type. To use the typeid operator in a program, one needs to include the library header <typeinfo>. It returns the lvalue of type const type_info to represent the type of value. Expression of typeid is an lvalue expression (lvalue has the address which is accessible by the program. For example, variable names, functions, class members, union, etc.). It is basically applied to the objects where the true type cannot be determined with the provided static information.
Syntax
Below given is the basic syntax of using the typeid in the C++ program:
typeid(expression)
Or
typeid(type)
Where,
Expression: In this, the whole expression is passed as a parameter to retrieve the runtime information of the type of it. The expression is first evaluated, and then its type is provided based on the final result.
Type: In this, the variable or the object is passed as a parameter to retrieve the dynamic type of it. There is no need for evaluation in it, unlike the expression parameter. So directly, the type of information is obtained as a final result.
Return value: It returns the runtime or dynamic type information of an object, which is an lvalue of type const type_info. If the type is a reference type, it returns the type_info object of the reference type.
How typeid works in C++?
Below given are some of the important points describing the working of typeid in the C++ program:
- If the expression passed as a parameter in typeid operator is of the base type, but the object is of the type which is derived from the base class, then the result is the type_info reference of the derived class.
- If the expression passed as a parameter in the typeid operator is dereferencing a pointer and the pointer value is either null or pointing to an onvalid object, then the bad_typeid error will be thrown.
- If the expression passed in type expression is neither a dereferencing pointer nor an object of the base class, it returns the type_info reference as a static type of expression. In this case, references are ignored while evaluating.
- In case when we need only the class information, typeid is used instead of dynamic_cast.
- Operand typeid is useful when one needs to find the type of expressions associated with the calculation of the runtime information like
The reference of an object of polymorphic class type or the dereference of the pointer.
- Operand typeid cannot be applied to the incomplete type. So if the object is under construction or destruction, then it returns the std:: type_info of the class being constructed or destroyed.
Examples of typeid in C++
Below given are some of the examples illustrating the use of typeid in the programs:
Example #1
Getting the typeid of the simple objects like int, float, char and comparing their types
Code:
#include <iostream>
#include <typeinfo>
using namespace std;
int main()
{
int i, j;
float f;
char c, *d;
double e;
//Getting the type using the typeid operator and passing the variables
const type_info& ti1 = typeid(i);
const type_info& ti2 = typeid(j);
const type_info& ti3 = typeid(f);
const type_info& ti4 = typeid(c);
const type_info& ti5 = typeid(d);
const type_info& ti6 = typeid(e);
const type_info& ti7 = typeid(*d);
// Printing the types of the variables of different data type on the console
cout << ti1.name() << endl;
cout << ti2.name() << endl;
cout << ti3.name() << endl;
cout << ti4.name() << endl;
cout << ti5.name() << endl;
cout << ti6.name() << endl;
cout << ti7.name() << endl;
//making comparisons in the types retrieved above
if (ti1 == ti2)
{
cout<< "Both i and j are of same type " << endl;
}
else
{
cout << "Both i and j are of different types" << endl;
}
if(ti5== ti7)
{
cout << "Both pointer and its address are of same type" << endl;
}
else
{
cout << "Both pointer and its address are of different types" << endl;
}
return 0;
}
Output:
Example #2
Getting the type of the objects for both polymorphic and non-polymorphic base class using typeid operator
Code:
#include <iostream>
#include <typeinfo>
//using polymorphic base class B1
class B1 {
public:
virtual void fun() {}
};
//using non-polymorphic base class B2
class B2 {};
class D1 : public B1 {};
class D2 : public B2 {};
using namespace std;
//main function
int main() {
D1* d1 = new D1;
B1* b1 = d1;
D2* d2 = new D2;
B2* b2 = d2;
//Printing the type of above class objects on the console
cout << typeid( d1 ).name() << endl;
cout << typeid( b1 ).name() << endl;
cout << typeid( *d1 ).name() << endl;
cout << typeid( *b1 ).name() << endl;
cout << typeid( d2 ).name() << endl;
cout << typeid( b2 ).name() << endl;
cout << typeid( *d2 ).name() << endl;
cout << typeid( *b2 ).name() << endl;
}
Output:
Example #3
Getting the type by the evaluation of expressions used as a parameter in typeid operator.
Code:
#include <iostream>
#include <typeinfo>
using namespace std;
int main()
{
int i = 13;
float j = 15.6;
double x = 3.14;
char c = 'y';
// Using the expression as a parameter in typeid
const type_info& t_id1 = typeid(i * x);
const type_info& t_id2 = typeid(i * j);
const type_info& t_id3 = typeid(i * c);
const type_info& t_id4 = typeid(x * c);
//Printing the type of the above calculated expressions on the console
cout << "type of t_id1 expression is "
<< t_id1.name() << endl;
cout << "type of t_id2 expression is "
<< t_id2.name() << endl;
cout << "type of t_id3 expression is "
<< t_id3.name() << endl;
cout << "type of t_id4 expression is "
<< t_id4.name() << endl;
return 0;
}
Output:
In this way, typeid is calculated by passing either the type of expression as a parameter in the typeid operator. In the case of simple data types like int, float, double, etc., typeid has resulted without any calculation whereas, in the case of expressions, expressions are evaluated first, and then their type has resulted.
Conclusion
The above description clearly explains what is typeid in C++ and how it is used to find the dynamic type of objects. The return type of typeid depends on the objects of which type is to be calculated. It throws many errors and results in unexpected outputs. So one needs to have a good understanding of this operator before using it in programs.
Recommended Articles
This is a guide to typeid in C++. Here we discuss How typeid works in C++ and Examples along with the codes and outputs. You can also look at the following article to learn more –