What is auto keyword in C++?

Filed Under: C++
Auto Keyword Featured Image

Introduced in C++ 11, the auto keyword is a part of the “Type Inference” feature. Type Inference refers to the deduction of data-type of variables and functions at compile time. Therefore, the programmer is not obliged to specify the exact data-types and leave it to the compiler.

In this article, we will go through benefits as well as mistakes made using the auto keyword.

How does the auto keyword work?

Mainly, variables and functions make use of the auto keyword for automatic deduction of the data-type.

With respect to variables, the initializer determines the data-type. However, the return statement determines the returning data-type of the functions. Let us take a quick look at some of the examples.

1. Auto used with variables

#include<bits/stdc++.h>
using namespace std;

int main(){
	auto i = 15;
	auto it = &i;
	cout<<i<<endl;
	cout<<*it<<endl;
	return 1;
}

Output:

15
15

In the above code snippet, the auto keyword determines the data-type of the variables. For instance, i is an integer and it is a pointer to an integer.

Note: The ‘auto’ keyword can be accompanied by * or & which play a part in the type inference.

Even though we did not specify it, the compiler managed to find it out using the right side of the statement also called initializers.

Note: A variable declared using auto must have an initializer otherwise the compiler will throw an error.


2. Auto used with functions

#include<bits/stdc++.h>
using namespace std;

auto func(){

	int x = 15;

	x += 10;

	return x;
}

int main(){

	cout<<func()<<endl;

	return 1;
}

Output:

25

In the above code, the return statement of the func() function, helps the compiler to identify the returning type of the function. Since the function returns x, which is an integer, the compiler immediately understands that auto must be replaced by an integer.


Some important uses of the auto keyword

In addition to the basic usage of auto with variables, we can apply the keyword to complex situations.

1. Auto used for iterators

Many complex data structures need iterators for extracting their values. A programmer can minimize the loss of time, by skipping the lengthy declaration of the iterator.

vector<map<int, vector<int>>> v;
...
...
for(auto it: v){
	...
	...
}

The iterator it is initialized by the vector v during the functioning of the for loop.


2. Auto used with function parameters

C++ requires the programmers to provide data-types for the parameters in the function declaration. However, this can be simplified by using the auto keyword.

#include<bits/stdc++.h>
using namespace std;

void func(auto x, auto st){
	cout<<x<<" "<<st<<endl;
}

int main(){
	int x = 10;
	string st = "abcd";
	func(x, st);
	return 1;
}

Output:

10 abcd

The deduction of data-types happens during the function call. Consequently, the matching of variables occurs, and the compiler figures out the exact data-types for all the arguments.


Common mistakes with the auto keyword

Any programmer can make mistakes with the auto keyword. Therefore, some of the common mistakes are listed below.

1. Multiple declarations using auto keyword

Usually, in order to save time, we might initialize multiple variables in a single statement. This method might cause problems if we use it along with the auto keyword.

Below is an incorrect way of multiple declaration.

auto x = 10, y = 10.0;

This leads to an inconsistent deduction. Since one of them is an integer, while the other is a double value.

Therefore, the following is the correct way to declare more than one variables with a single auto keyword.

auto x = 10, *y = &x;

At compile time, the above auto can be replaced by int, defining x as an integer and y as a pointer to an integer.


2. Similarity in integer and boolean

Since boolean values can be initialized as numbers, during a code review, people can misunderstand when a variable initialized by auto is either integer or boolean.

auto x = 0;

The above variable x can be either an integer or a boolean value. Therefore, in this scenario auto keyword is a bad programming concept.


3. Storing values returned by functions

During code revisions, a variable storing a value from a function call might cause confusion when declared using auto. That is because, the observer will need to go through the function to make out the data-type being returned.

auto x = some_function();
auto y = some_class();

The above statements should be:

int x = some_function();
some_class y = some_class();

The latter lines make the code easier to follow. Moreover, it might reduce any error caused by some wrong value returned.


Conclusion

The use of auto reduces the time spent on basic code. Therefore, the programmer can focus on the intensive part of the program. A program must always be written in a neat and clear way so that the reader can interpret it easily.

Thank you for reading. Feel free to comment below for any queries or feedback.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages