Type Inference In C++ Auto And Decltype - C++ Programming Tutorial
C++ Course / Miscellaneous / Type Inference In C++ Auto And Decltype

Type Inference In C++ Auto And Decltype

BLUF: Mastering Type Inference In C++ Auto And Decltype is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Type Inference In C++ Auto And Decltype

C++ is renowned for its efficiency. Learn how Type Inference In C++ Auto And Decltype enables low-level control and high-performance computing in the tutorial below.

In languages such as C or C++, when declaring a variable, we typically specify the data type at compile time. However, type inference allows us to omit explicit data type declarations by using certain keywords. This feature streamlines the coding process by eliminating the need to specify data types for variables, thus improving efficiency for programmers. While this may marginally impact compile time, it has no bearing on the program's runtime performance.

In this section, we are going to define the subsequent keywords that are employed for type deduction in C++:

1. auto

The auto keyword is placed in front of a variable or function to automatically infer its data type from the initial value. It analyzes the expression of the initial value or return value of the function to determine the data type of the variable.

When declaring a variable as auto, it must be initialized at the time of declaration to avoid encountering a compile-time error.

C++ Example 1:

Example

#include <iostream>
using namespace std;
int main() {
    
    auto a=45;
    auto b='z';
    auto c=5.6;
    cout<<"data type of the variable a is: "<<typeid(a).name()<<endl;
    cout<<"data type of the variable b is: "<<typeid(b).name()<<endl;
    cout<<"data type of the variable c is: "<<typeid(c).name()<<endl;
    return 0;
}

Output:

Explanation

In the previous instance, we have initialized three variables namely a, b, and c using the auto keyword. Variable a has an integer data type, variable b is characterized by a character data type, and variable c is defined with a double data type.

We utilize the typeid operator to retrieve the data type of the variable using its name method.

In the result, we obtained the variable a assigned as an integer, variable b set as a character, and variable c defined as a double.

C++ Example 2:

Example

#include <iostream>
using namespace std;
int main() {
    
    auto a;
    auto b;
    auto c;
    a=45;
    b='z';
    c=56.63;
    cout<<"data type of the variable a is: "<<typeid(a).name()<<endl;
    cout<<"data type of the variable b is: "<<typeid(b).name()<<endl;
    cout<<"data type of the variable c is: "<<typeid(c).name()<<endl;
    return 0;
}

Output:

Explanation

In the provided code snippet, three automatic variables were declared without initialization at the declaration point, resulting in a compilation error.

2. decltype

The decltype function operates similarly to how auto functions, acting like an operator that accepts an expression as a parameter. It assesses the expression provided and assigns the data type to the variable or entity accordingly.

C++ Example 1:

Example

#include <iostream>
using namespace std;
   char fxn1(){
        return 'c';
    }
    
    int fxn2(){
        return 565;
    }
int main() {
    
    decltype(fxn1()) var1;
    decltype(fxn2()) var2;
    cout<<"data type of the fxn1 is: "<<typeid(var1).name()<<endl;
    cout<<"data type of the fxn2 is: "<<typeid(var2).name()<<endl;
    return 0;
}

Output:

Explanation

In the provided code snippet, there are two functions present. One function returns a character type, while the other function returns an integer type. By utilizing the decltype keyword and passing both functions as arguments, the resulting datatype will be determined. Subsequently, new variables were declared based on these determined data types.

Now, the return type of function 1 and the data type of variable 1 will match, while the return type of function 2 and the data type of variable 2 will also match. Therefore, we can employ the typeid operator to retrieve the data type of both variables.

C++ Example 2:

Example

#include <iostream>
using namespace std;
int main() {
    
    int var1 =56;
    char var2='c';
    
    decltype(var1) var3 = var2*5;
    decltype(var2) var4 = var2+5;
    cout<<"data type of the var3 is: "<<typeid(var3).name()<<endl;
    cout<<"data type of the var4 is: "<<typeid(var4).name()<<endl;
    return 0;
}

Output:

Explanation

In the provided code snippet, we encounter a pair of variables: var1 is assigned an integer data type, while var2 is assigned a character data type. Additionally, we've instantiated two more variables, var3 and var4. var3 is assigned the identical data type as var1 by utilizing the decltype keyword for data type initialization, whereas var4 inherits the data type of var2 through the application of the decltype keyword. Subsequently, we have output the data type using 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 inheritance is employed and the class's data type is retrieved, decltype will provide a pointer to the base class, while typeid will yield a pointer to the derived or subclass.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience