Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need to explicitly write the 'auto' keyword?

Tags:

c++

c++11

auto

I am moving towards C++11 from C++98 and have become familiar with the auto keyword. I was wondering why we need to explicitly declare auto if the compiler is able to automatically deduce the type. I know C++ is a strongly typed language and this is a rule but was it not possible to achieve the same outcome without explicitly declaring a variable auto?

like image 427
Farsan Rashid Avatar asked May 23 '18 08:05

Farsan Rashid


People also ask

Why do we use auto keyword?

The auto keyword is a simple way to declare a variable that has a complicated type. For example, you can use auto to declare a variable where the initialization expression involves templates, pointers to functions, or pointers to members.

What is auto datatype in C++?

The auto keyword in C++ automatically detects and assigns a data type to the variable with which it is used. The compiler analyses the variable's data type by looking at its initialization. It is necessary to initialize the variable when declaring it using the auto keyword.

What is auto keyword in C with example?

The auto keyword declares automatic variables. For example: auto int var1; This statement suggests that var1 is a variable of storage class auto and type int. Variables declared within function bodies are automatic by default.

What can I use instead of auto in C++?

auto is a C++11 feature that deduces the type of the variable (at compile-time). In this instance, it deduces auto to be an int, so i is a reference to an int. Your code would behave exactly the same if you replaced "auto" with "int".


2 Answers

Dropping the explicit auto would break the language:

e.g.

int main() {     int n;     {         auto n = 0; // this shadows the outer n.     } } 

where you can see that dropping the auto would not shadow the outer n.

like image 88
Bathsheba Avatar answered Oct 13 '22 06:10

Bathsheba


Your question allows two interpretations:

  • Why do we need 'auto' at all? Can't we simply drop it?
  • Why are we obliged to use auto? Can't we just have it implicit, if it is not given?

Bathsheba answered nicely the first interpretation, for the second, consider the following (assuming no other declarations exist so far; hypothetically valid C++):

int f(); double g();  n = f(); // declares a new variable, type is int; d = g(); // another new variable, type is double  if(n == d) {     n = 7; // reassigns n     auto d = 2.0; // new d, shadowing the outer one } 

It would be possible, other languages get away quite well with (well, apart from the shadowing issue perhaps)... It is not so in C++, though, and the question (in the sense of the second interpretation) now is: Why?

This time, the answer is not as evident as in the first interpretation. One thing is obvious, though: The explicit requirement for the keyword makes the language safer (I do not know if this is what drove the language committee to its decision, still it remains a point):

grummel = f();  // ...  if(true) {     brummel = f();   //^ uh, oh, a typo... } 

Can we agree on this not needing any further explanations?

The even bigger danger in not requiring auto, [however], is that it means that adding a global variable in a place far away from a function (e.g. in a header file) can turn what was intended to be the declaration of a locally-scoped variable in that function into an assignment to the global variable... with potentially disastrous (and certainly very confusing) consequences.

(cited psmears' comment due to its importance - thanks for hinting to)

like image 27
Aconcagua Avatar answered Oct 13 '22 04:10

Aconcagua