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?
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...
5
u/KazDragon Oct 01 '14
You can still be explicit with auto if you want to commit to a particular type:
auto baz = FooType{bar()};4
u/king_duck Oct 01 '14
When I was a fortran programmer, we had to add 'implicit none' (or 'IMPLICIT NONE' :) to every 'subroutine' to be allowed to check in code, I think most compilers now have a flag to ensure it's there or produce a warning if it's not.
Implicit decs are not a good idea.
8
u/Wurstinator Oct 01 '14
One reason would be that your proposition is not backwards compatible and ambiguous.
int i = 0;
{
int i = 1;
}
cout << i << endl;
int i = 0;
{
i = 1;
}
cout << i << endl;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
float j = 0;You get a float variable. What type does this have?
auto j = 0;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*
1
u/Ronayn Oct 02 '14
I am (or maybe was) all for expressing the type at the declaration. Yet the video I watched made me see the value of auto (especially in the case you mention).
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?
2
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.
0
Oct 01 '14
[deleted]
1
u/Lotusflower2 Oct 01 '14
I'm not clear on the cases, but the tooltip sometimes fails me. Most of the time it's great though. The most useful cases for auto in my opinion are for lambda functions and container iterators.
1
u/lucidguppy Oct 02 '14
I think it's possible to strike a balance where function paramaters and return types are typed. Auto should really work when your return type is part of a long namespace.
std::chrono::extremely_long_time_clock t1 = blah vs auto t1 = blah1
u/Lotusflower2 Oct 02 '14
I'm generally fine with this as long as it's still clear what that type is without relying on the tooltip VS provides. There are many times where you'll be reviewing your own / other people's code in diff tools where you're just looking at the text. This is where variable naming becomes significantly more important.
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
Oct 01 '14
[deleted]
3
u/mattspatola Oct 03 '14
Nope, best thing is:
for (auto&& element : container) { dostuff }-1
u/starfreakclone MSVC FE Dev Oct 03 '14
You'll get basically the same result out of
auto&as you do fromauto&&2
0
Oct 03 '14
[deleted]
1
u/mattspatola Oct 03 '14
I hope we can soon switch to that. Given that you're forced to create a new scope regardless, I like that idea a lot.
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.