tags:

views:

498

answers:

6

What is the goal of the "auto" keyword in C? With C++ 0x it got new meaning but does it mean that my code will break if I port C code over to a C++ 0x compiler?

+26  A: 

It will break if your code contains the auto keyword. In nearly 30 years of C and C++ programming I've never come across any that did. The keyword was introduced in the first C compiler to specify local function variables, but compilers almost immediately became clever enough not to require it, and very little code that uses it will survive today - that's why C++0x chose to recycle it rather than introduce a new keyword which would cause portability problems.

The purpose of the auto keyword in C++0X is to allow the compiler to work out the type of a variable, where this is possible:

vector <int> v;
auto it = v.begin():

the compiler can see that v.begin() must return a vector<int>::iterator and so can create a variable of that type, saving a lot of keyboarding or typedef creation.

anon
That's awesome, I hate typing out vector iterators ;)
identitycrisisuk
Why will it break code that contains `auto`? Isn't the old meaning still valid?
Motti
@mottii To be honest, I'm not sure - a compiler can certainly see that `auto int i;` is not auto being used in the new sense, but I'm not sure if the standard requires them to do so.
anon
@Motti: It will break any code that used `auto` in the old meaning, unless it was being used in a case where `int` was implicit. Think of `auto` as the type; is `int float` valid? Then neither is `auto float`, or `auto int`. However, the exception I stated above is that in classic C, `auto i = 0;` created an automatically allocated variable, who's type is implicitly `int`, named `i`, initialized to 0. This case will happen to work with the new meaning. (`auto` will deduced to be an `int`, because the type of the initializer, 0, is `int`.)
GMan
@Motti For what it's worth g++ in c++0x mode barfs on auto int i;
anon
Motti
@GMan, I see I was wrong, thanks for pointing this out.
Motti
According to the C++03 standard, `auto` can be used to disambiguate "ambiguous" code for humans. As in the following code `auto int(n);` to tell the human that it's a variable declared and not a cast to `int`. But the compiler really doesn't need it :)
Johannes Schaub - litb
C++0X FCD, C.1.5: "7.1.6.4: Change: The keyword auto cannot be used as a storage class specifier." page 1205 of PDF N3092.
Nathan Ernst
+6  A: 

In C, auto specified automatic storage duration (as opposed to static, extern, register). Since this is the default, I have never seen auto used in any code. I haven't done much C, though.

sbi
+1  A: 

It is rarely used; it meant a local variable. Modern compilers such as VS2010 C++ give it a new meaning.

Mitch Wheat
This new meaning is from the C++0x standard as mentioned above
Stewart
+1  A: 

This answer is wrong, see following question, I'm leaving the answer here as a reference.


AFAIK C++0x's use of auto doesn't contradict C traditional usage of auto. In C auto is used together with the type.

auto char c1 = 'a'; // OK, old meaning of auto is still valid
auto c2 = 'b'; // OK, new meaning of auto (deduce c2 is a char)

The only place where it can change the meaning of the code is when auto was used together with the implicit int rule (if not type is specified -> it's an int) in which case the second line in my example used to have c2 of type int and now it's of type char.

Motti
Doesn't compile with g++, FWIW.
anon
Thanks @Neil, I asked a new question to find out who's right (I can't find anything the C++0x draft)
Motti
I surprised they didn't allow for this given all of the other warts in C++ for the sake of C compatibility. ;)
Judge Maygarden
+3  A: 

Bjarne Stroustrup mentions in his C++0x FAQ about auto:

"The old meaning of auto ("this is a local variable") is redundant and unused. Several committee members trawled through millions of lines of code finding only a handful of uses -- and most of those were in test suites or appeared to be bugs."

So I assume, that compilers wil not be forced by the standard to implement the old meaning of auto.

Lucas
A: 

I just came across some code that would not compile in VS2010.

int foo( int auto );

To me, it was pretty stupid to have a parameter called 'auto' in the first place.

mattja