Introduction to C++ auto
In C++, an auto keyword is used to specify that the variable’s data type will automatically be deducted from its initializer. In the case of functions, the auto keyword before the function name or the auto return type signifies that the return type of that function will be evaluated from the return type expressions at runtime. From C++ 11, this keyword was introduced; before that, the programmer needed to assign each variable’s data type explicitly at compile time. The automatic deduction of the datatype from the expression is called Type Inference in C++. This feature reduces the overhead and saves a lot of time for the programmer to declare the things which the compiler is already aware of.
There is no specific syntax for using the auto keyword in a program. Instead, we need to simply write the ‘auto’ keyword before the variable name or the function return type.
1. Use of auto keyword as the datatype of the variable after evaluation of expression:
auto var1 = 3.767;
auto var 2 = 34;
2. Use of auto keyword in function return type:
auto sum(int var1, var2)
auto sub(double var1, double var2)
How does the auto keyword work in C++?
The Keyword auto is used normally like other keywords in C++. It is used before the name of the variable or the function to infer the return type of both of them. When the auto keyword is used, it is evaluated at the compilation time. Since the compiler has to do this, it increases the compilation time to a few seconds, which is almost negligible.
One of the advantages of using the auto keyword is that it reduces the extra overhead on the programmer to define the data type so that he/she can focus on other important tasks as well. In the case of functions, using the auto keyword helps to create generic functions, i.e., we can pass different parameters to the function with their datatype as being auto. So the issue of creating separate functions for different data types can be easily solved by creating only one single function.
Although the auto keyword can be used in the function return type, it is usually not used by the programmers in the case of simple functions, as it sometimes creates problems as the return type of function is very useful and returned to the caller, which then performs specific tasks according to the program requirements. The caller may misinterpret what is returned from the function, which can lead to unexpected results.
Some important points to be kept in mind while working with auto keyword:
- The keyword auto can be used along with the modifiers like const, pointer (*), reference (&), etc.
- When the auto return type is used in functions, all the return statements of that function must need to return the same data type; otherwise, it will throw an error in the program.
- The keyword auto can also be used to iterate the values of the complex data structures like vectors, sets, etc., in C++. Programmers don’t have to worry about the long and complex iterator declaration. It can be defined as ‘auto’ simply.
- Multiple declarations of variables are not possible in the case of defining their auto. For example, auto a =10, b= 344.5;
- Function parameters can also be declared as auto. For example, auto func(auto a, auto b).
- Declaring a variable as ‘auto,’ which stores the return value of a function, must be avoided as it creates confusion at times. For example, auto-answer = funct().
Examples of C++ auto
Some of the examples showing the usage of auto keyword in C++ program are given below:
Pushing auto variables in vector and iterating to the elements of the vector using auto variable
using namespace std;
// declaring the name variables as auto
auto name1 = "komal";
auto name2 = "yashi";
auto name3 = "rashi";
//pushing the names defined above in vector
// printing the values of vector on console using 'i' as an auto variable to iterate
cout << "Here is the vector data" << endl;
for (auto i = vec1.begin() ; i != vec1.end() ; i++)
cout << *i << endl;
In the above code, vector ‘vec1’ is declared of the string type. All the name variables, i.e., name1, name2, name 3, are declared as auto will further be evaluated as a string by the compiler. To iterate through the whole vector to print the values on the console, iterator ‘i’ is used, which is declared as auto. Values are printed on the console using the * i, which means the value holding at ‘i’.
Using variables of different data type and performing their addition by defining them as auto
using namespace std;
// declaring the variables as auto
auto var1= 100;
auto var2 = 199.990;
auto result = var1+ var2;
cout << "Result of addition is " << result <<endl;
Explanation: In the above code, variables var1 and var2 are declared as an auto with the values 100 and 199.990. These values will be evaluated as int and double for the compiler itself. The addition of the values is stored in a variable that is again declared as an auto which will be interpreted as double by the compiler and printed on the console.
Using the function return type as auto
using namespace std;
//function body with the return type and the parameters declared as ‘auto’
auto mod(auto var1, auto var2)
auto result = var1% var2;
auto var1 = 25;
auto var2 = 8;
// Value returned in ‘result’ is also auto type
auto result = mod(var1,var2);
cout<<"The result of above operation is " << result <<endl;
return 0 ;
Explanation: In the above program, a function to find the modulus of 2 numbers is created with the return type and the parameters being defined as ‘auto,’ which will be deduced to int type by the compiler. Values returned from the function are stored in the variable ‘result,’ which is also declared as ‘auto’ (will again be deduced to int by the compiler).
The above description clearly explains what an auto keyword is in C++ and how it is used to deduce the data type of variables and function return types. In C++ 11, many new keywords were introduced, which provided the facility to programmers to leave the task of type deduction to the compiler itself. However, though it is one of the very useful keywords, it is error-prone, especially in functions. So one needs to understand it completely before using it in a program.
This is a guide to C++ auto. Here we discuss How does the auto keyword work in C++ and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –