What is the Use of decltype in C++?

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

The c++ decltype is used to inspect the declared type of an entity or the type of an expression. The auto keyword lets you declare a variable with a particular type whereas decltype lets you extract the type from the variable so decltype is sort of an operator that evaluates the type of passed expression.

The auto allows us to declare the variable but the decltype allows us to get the type of the variable that is being passed in the expression of the method.

What is C++ decltype?

The c++ decltype is an operator in the c++ language that is used for examining the data type of the given expression. This feature belongs to the C++11 and is also known as the C++0x. This specifier takes an expression as an operand. You can think of a variable that is defined with decltype(expression) as having its type or derived type replaced by the compiler, for example, if there is a variable defined using the decltype expression then its type will be replaced by the compiler. The information of type is provided during the compiling time and execution time as well. During the compilation time, the type information is provided by the decltype and during the run-time of the program, the type information is provided by the TypeId. Due to this, if there is any object of a derived class containing the reference of the class or pointer that is pointing to it, here the typeid will provide the reference or the pointer instead of the type of the derived type. For example:

Output:

About

The type inference is defined as specifying the data type of a particular statement automatically in a programing language. Before C++ 11, the possible values of runtime expressions are restricted by the manual declaration of each data type. By using the inferences, we can reduce the length of the code because the compiler can automatically detect the data type of the expression. So, we do not need to write the data type of the variable every time. The code size can be reduced by not writing unnecessary statements that the compiler already understands. The compilation time gets increased because all the types of the data types are specified while in the compilation phase, the time of program execution does not get affected by this. All these things take place during the compile time not at run-time. So, the run-time doesn't get affected but the compile time gets increased.

C++ decltype Keyword The c++ decltype keyword is used for inspecting the data type of an expression that is being declared or for inspecting the type of the given expression. It inspects the declared type of an entity or the type of an expression. auto lets you declare a variable with a particular type whereas decltype lets you extract the type from the variable so decltype is sort of an operator that evaluates the type of passed expression.

Syntax of C++ decltype

The syntax of the c++ decltype is as follows:

  • The c++ decltype takes only one parameter which is the expression. Expressions are sequences of operators and operands that are used for the purposes such as computing a value from the operands or designating objects or functions.

And this returns the data type for the provided expression as the return value. There are some guidelines of the compiler for the expression parameter.

  1. If the expression parameter has access to the member of the class that it belongs to, then the type of the object can be recognized by the expression decltype(expression). And if there is no such object exists or if there is function overloading for a specified group, then the compiler shows an error message. 2. If there is any function overloading during the call of the method or an operator, the returns the data type which will be evaluated by the compiler.

C++ decltype Uses

The c++ decltype is a specifier that is used for yielding the type of an expression. The decltype stands for declared type of an expression or an entity. Using this, we can extract the data type of expression that is passed as a parameter. Let us see some examples to see how the c++ decltype is used in programming in the below section.

Example 1. In this example, we use the decltype to return the type of expression.

Output:

Explanation: In the above example, first, we created two variables of different data types. The first variable is function1 and it is of int type. And the second variable is function2 which is of the char type. Then in the main program, we used the decltype specifier and assigned the two variables function1 and function2. Then we used two cout functions along with the typeid and passed the values a and b to it that returns the result after comparing the data type of a and b and the return type of function1 and function2.

Example 2. Given below is one more example of decltype to demonstrate its use in c++.

Output:

Explanation: In the above example, we initialized a variable a of the int type and assigned the value 7 to it. Then we used the decltype specifier and passed the variable a to it. Then we assigned the a+7 value to g. After that, we used the cout to print the output.

What Does decltype Return in the Case of Entity and Expression?

If the argument is an unparenthesized id-expression or an unparenthesized class member access expression, then decltype yields the type of the entity named by this expression. If there is no such entity, or if the argument names a set of overloaded functions, the program is ill-formed. The syntax of c++ decltype in the entity case will be like this:

C++ decltype vs typeid Refer

Now let us see some differences between the decltype and typeid and understand them in a better way.

decltypetypeid
The c++ decltype provides the type information during the compilation of the program.The typeid provides the type information during the runtime of the program.
If the base class of the program is referring (pointing) to the derived class object of the program, then the decltype provides the base class pointer or base class reference as the type.If the base class of the program is referring (pointing) to the derived class object of the program, then the decltype provides the derived type reference or derived type pointer type.

To learn about auto in c++, click here- Auto in c++

Conclusion

  • The c++ decltype is used for determining the type of an expression that is being passed as the parameter in the program.
  • The syntax of the c++ decltype is- decltype( expression ).
  • The auto allows us to declare a variable that is of some specific data type.
  • The decltype is used to extract the type of the variable.
  • If there is any function overloading during the call of the method or an operator, the return type will be decltype(expression).
  • The c++ decltype provides the type information during the compilation of the program.
  • The typeid provides the type information during the runtime of the program.