Type Inference in C++ (auto and decltype)
In a programming language like C or C++, we declare any variable and explicitly declare the data type of the variable at the compile time. But type inference means we use some keywords by which we need not to declare the data type of the variable or function explicitly. It saves a lot of time for a programmer to write the data type of the variable. It increases the compile time of the program slightly, but this does not affect the run time of the program.
Here, we will declare the following keywords which are used for type inference in C++:
The auto keyword is used before any variable or any function, and it automatically gets the data type of the variable or the function from its initial value. It basically gets the expression of the initial value of the value or return value of the function then it determines the data type of the variable.
To declare any variable auto, we have to initialize it at the declaration time only. Otherwise, it gives a compile-time error.
C++ Example 1:
In the above example, we have declared three variables a, b and c with the auto keyword. Variable a is of type integer, b is the type of character, and c is the type of double.
We use the typeid operator which returns the data type of the variable through its name() function.
In the output, we got i which represents integer for variable a, c which means character for variable b and d, which represents the double for the variable c.
C++ Example 2:
In the above code, we have declared three auto variables but did not initialize them at declaration time, so it is giving the compile time error.
The decltype() also works on the same concept as auto works, but it is like an operator which takes an expression as an argument. It evaluates the expression, and then it sets the data type of the variable or an entity.
C++ Example 1:
In the above code, we have two functions where one function has a character return type, and another function has an integer return type. We used the decltype() keyword and passed both functions as an argument, and whatever it returns will be the datatype, so we declared the new variables for these data types.
Now the return type of fxn1 and data type of var1 will be the same, and the return type of fxn2 and data type of var2 is also the same. So we use the typeid operator to get the data type of both the variables.
C++ Example 2:
In the above code, we have two variables: var1 has an integer data type, whereas var2 has the data type character. We have declared the other two variables, var3 and var4, where var3 has the data type same as var1 because we use the decltype keyword to initialize its data type and var4 has the same data type as var2 because of using the decltype keyword. Now we printed the data type with the help of the typeid keyword.
Note: The decltype keyword gives the data type of the variable or entity at compile time, whereas typeid gives the data type at run time.
If we use inheritance, and get the data type of the class, then the decltype will give a pointer to the base class, whereas typeid will return the pointer to the derived or children class.