What is Decltype in C++
It looks at an entity’s or an expression’s stated type. Decltype is a kind of operator that examines the type of given expression, as opposed to “auto,” which allows you to define a variable with a certain type but allows you to retrieve the type from the variable. Type information is provided by Decltype during compilation and by TypeId during execution. As a result, if an object of a derived class has a base class reference or pointer pointing to it, typeid will provide the derived type reference or pointer rather than type, which will be provided by decltype.
When it is necessary to know the dynamic type of an object, the operator typeid is used. However, the actual name that is returned is somewhat compiler-dependent.
The general syntax C++ provides for decltype is as follows:
Syntax:
The decltype only takes one parameter, which is an expression. The decltype returns the expression parameter’s type. Using the following guidelines, the compiler depends upon the type of expression parameter.
- When the expression parameter is an access to a class member or an identifier, the type of the object identified by the expression is decltype(expression). If such an object does not exist or if the expression argument specifies a group of overloaded functions, the compiler issues an error message.
- If the input expression calls a method or an operator function with many overloads, the return type of a given function is decltype(expression).
Example 1
The implementation of the above discussion is given by the decltype code. Since more than one variable, function, class, etc., cannot have the same name in the identical scope. The namespace is therefore introduced to resolve this issue. After this, we have established the function “my_func1” which returns the integer value “5” as we have initially specified the int data type to the function “my_func1”.
Again, we have established a function as “my_func2” and assigned a char data type to it. This function returns the character value “i”. Next, we have constructed the program’s int main(), where we have deployed the decltype modifier. The decltype modifier takes the “my_func1” and “my_func2” as an argument, respectively. To the decltype, we have also defined the two variables as “a” and “b”.
The variables “a” and “b” have the same data types as the return type of “my_func1” and “my_func2”. Then, we have a cout statement where the typeid takes the variables “a” and “b” as input and will return the type of these variables.
using namespace std;
int my_func1() { return 5;}
char my_func2() { return 'l'; }
int main()
{
decltype(my_func1()) a;
decltype(my_func2()) b;
cout<<typeid(a).name() <<endl;
cout<<typeid(b).name() <<endl;
return 0;
}
As the type of “my_func1” is int and “my_func2” is char. So, in the output, the type “i” and “c” is returned, which indicates the int and char type.
Example 2
One additional illustration of decltype use is provided below, which is slightly different from the previous one. We have directly constructed the main() of this particular program after the header section. Inside the program’s int main(), we have declared the variable “num” and assigned a number “10” to it. Then, we have called the decltype modifier, which takes the variable “num” as a parameter. With the decltype modifier, we have also set the variable “i” which has the type int the same as the type of the variable “num”.
Then, we added the num and the number “10” by using the plus operator inside the “i” variable. After this, we have a cout command where the typeid is invoked that takes the variable “i” as an input and provides its type.
using namespace std;
int main()
{
int num = 10;
decltype(num) i = num + 10;
cout<< "decltype : " <<typeid(i).name();
return 0;
}
As we provide the int type to the variable “num” in the above code snippet. The output displays the word “i” which is the int type value of the variable “num”.
Example 3
The minimum number numbers are returned by the C++ template method min type(), which is seen below. Any integral type may be used for the two numbers. The minimal type of two is used to determine the return type.
With the auto modifier, we have created the function SearchMin() and passed the variables x and y as a parameter inside it. Then, we assigned the variable names to the decltype keyword. The decltype takes the variable with the conditional expression. The function searches for the minimum value and returns the type of variable which has the minimum value. Next, within the int main(), we have passed the value to the SeatchMin function through the cout command.
using namespace std;
template
auto SearchMin(T1 x , T2 y) ->decltype(x < y ? x : y)
{
return (x < y) ? x: y;
}
int main()
{
cout<<SearchMin(5, 4.55) <<endl;
cout<<SearchMin(6.5, 2) <<endl;
return 0;
}
The output displayed the minimum values below. As from the first SearchMin statement, 4.55 is returned from the double type. Then the second statement, 2, is obtained from the int data type.
Conclusion
That’s all about the C++ decltype. We have discussed it briefly with the definition, its syntax, and its functionality in this particular article. Here, we have shown the decltype working with the running example code in C++. All the instances are distinct from each other. The instances all differ from one another. The decltype script with the auto prefix is demonstrated in the last example. The automatic type deduction keyword, auto, is used in C++11 and beyond.