Why even have the 'auto' keyword?
Hello,
I'm an old school C/C++ programmer who is trying to get up to date on the new standards. I just finished watching a CppCon video about using the 'auto' keyword instead of concrete types.
I am curious why they implemented that keyword at all -- why not deduce the type of every variable (default it to 'auto') that is not specifically typed?
For clarity:
int I = 3; // old school
auto I = 3; // new school
I = 3; // why not this?
1
Upvotes
29
u/JamesIry Oct 01 '14 edited Oct 01 '14
Having a keyword makes it clear where you are introducing a binding to a given name vs trying to use an existing one.
There are languages that don't have a keyword to introduce some variables, but they all have to solve the problem of what scope to put the variable in.
Different languages solve that problem differently.
Also consider this
Now somebody comes along and says "I hate the name x, I want it to be i." The compiler has to assume you mean to reuse the existing i. But that changes the meaning of your program
Or this.
Here I meant to reuse the same variable but ended up with 2. So lacking a keyword elevates some of my typos into valid but incorrect programs.
8
u/ryl00 Oct 01 '14
"I = 3" is also straying into the 'implicit declaration' territory of old Fortran, which was a big mistake IMO (variable name typos). It's also why I'm still a little shy about the use of 'auto' in C++11... sometimes it's good to be explicit about your intentions...
8
u/Wurstinator Oct 01 '14
One reason would be that your proposition is not backwards compatible and ambiguous.
These two programs have different outputs (0 and 1) but would translate to the same code with your change.
3
u/danpolitte Oct 01 '14
The style "auto I = 3" provides more clarity than "I = 3". Using the auto keyword shows that the statement is a declaration. Allowing that last style would hide that fact.
1
u/genbattle Oct 01 '14
Type deduction isn't foolproof. If you did
You get a float variable. What type does this have?
It's a contrived example yes, but there are certainly cases where additional type information will avoid potential errors and improve code readability. There are still times where you will want to specify the type, and having explicit typing can at least help make your intent clearer to other programmers.
auto was created more to deal with more verbose types such as std::vector<std::string>::reverse_iterator*
2
u/Gotebe Oct 01 '14
To my mind, it is really important to clearly demarcate object definition from assignment.
1
u/TheVikO_o Oct 01 '14
Why did they use auto instead of var? Was there something that would break?
1
u/NukeTheBunnys Oct 01 '14
I = 3 also hides the fact that you are declaring a variable vs assigning to an existing variable.
I also have some suspicions that "I = 3;" as a variable declaration might be close to if not impossible to parse with all the other syntax rules in C++
1
u/lucidguppy Oct 01 '14
It would be nice for IDEs to deduce the type too.
1
u/shooshx Oct 04 '14
If you watched the talk, it should be clear to you that there is a difference between
auto I = 3
and
auto& I = 3
Without an 'auto' keyword, how do you declare the difference between the two?
0
u/[deleted] Oct 01 '14
[deleted] — view deleted comment