typeid operator in C++ with examples

In this tutorial, you will learn how to use the typeid operator in C++. The typeid operator in C++ is available in the <typeinfo> library.

The syntax is :

typeid(variable);

This will return the type-information of a variable during runtime as a reference to an object of the class type_info.

To get the type-information you have to use the following syntax:

const type_info& type1 = typeid(variable);

But since it is a reference you can not directly print type1 with the help of std::cout.

We use the function name() on the reference, for example,

int i;

const type_info& type1 = typeid(i);

cout << "Type is " << type1.name() << endl;

This will print “i” for integer, “f” for float, “c” for character, “d” for double, “s” for short and “b” for bool i.e. boolean.

In this case, you will get the output as:

Type is i

Now let’s use this to compare two variables and identify whether they are of the same type or not in a C++ program.

typeid operator in C++ Progam:

#include <iostream>
#include <typeinfo>

using namespace std;

int main()
{
        int i = 5;
        float f = 5.55;

        const type_info& type1 = typeid(i);
        const type_info& type2 = typeid(f);

        if (type1 == type2)
                cout << "i and f are of same data type" << endl;
        else
                cout << "i and f are of different data type" << endl;

        //now let's create another integer k and compare it with i

        int k = 7;

        const type_info& type3 = typeid(k);

        if (type3 == type1)
                cout << "k and i are of same data type" << endl;
        else
                cout << "k and i are of different data type" << endl;

        return 0;
}

Another way to use the typeid operator

We can even use mathematical expressions instead of variables in the typeid operator. For example,

int i = 5;

double x = 7.70;

Let expression 1 = (i*x)

Let expression 2 = (3.21 + 2*i)

The result of (i*x) is 38.50 which is a double data type value,

The result of (3.21 + 2*i) is 13.21 which is also a double data type value.

Now let’s compare these expressions with the help of typeid operator in a C++ program:

#include <iostream>
#include <typeinfo>

using namespace std;

int main()
{
        int i = 5;
        double x = 7.70;

        const type_info& type1 = typeid(i*x);
        const type_info& type2 = typeid(3.21 + 2*i);


        if (type1 == type2)
        {
        cout << "The 2 expressions are of the same data type" << endl;

        cout <<"Type of the first expression is  : " <<  type1.name() << endl;
        cout <<"Type of the second expression is : " <<  type2.name() << endl;

        }

        else

         {
                cout << "The 2 expressions have different data type" << endl;

                cout <<"Type of the first expression is : " << type1.name() << endl;
                cout <<"Type of the second expression is : " << type2.name() << endl; 

         }
         
        return 0;
}

 

Output for the above code

The 2 expressions are of the same data type
Type of the first expression is  : d
Type of the second expression is : d

Also see:
Difference between GCC and G++ in C++

Leave a Reply

Your email address will not be published. Required fields are marked *