Note this question was originally posted in 2009, before C++11 was ratified and before the meaning of the
auto
keyword was drast
GCC has a special use of auto
for nested functions - see here.
If you have nested function that you want to call before its definition, you need to declare it with auto
.
The new meaning of the auto keyword in C++0x is described very nicely by Microsoft's Stephan T. Lavavej in a freely viewable/downloadable video lecture on STL found at MSDN's Channel 9 site here.
The lecture is worth viewing in its entirety, but the part about the auto keyword is at about the 29th minute mark (approximately).
"auto" supposedly tells the compiler to decide for itself where to put the variable (memory or register). Its analog is "register", which supposedly tells the compiler to try to keep it in a register. Modern compilers ignore both, so you should too.
auto
is a storage class specifier, static
, register
and extern
too. You can only use one of these four in a declaration.
Local variables (without static
) have automatic storage duration, which means they live from the start of their definition until the end of their block. Putting auto in front of them is redundant since that is the default anyway.
I don't know of any reason to use it in C++. In old C versions that have the implicit int rule, you could use it to declare a variable, like in:
int main(void) { auto i = 1; }
To make it valid syntax or disambiguate from an assignment expression in case i
is in scope. But this doesn't work in C++ anyway (you have to specify a type). Funny enough, the C++ Standard writes:
An object declared without a storage-class-specifier at block scope or declared as a function parameter has automatic storage duration by default. [Note: hence, the auto specifier is almost always redundant and not often used; one use of auto is to distinguish a declaration-statement from an expression-statement (6.8) explicitly. — end note]
which refers to the following scenario, which could be either a cast of a
to int
or the declaration of a variable a
of type int
having redundant parentheses around a
. It is always taken to be a declaration, so auto
wouldn't add anything useful here, but would for the human, instead. But then again, the human would be better off removing the redundant parentheses around a
, I would say:
int(a);
With the new meaning of auto
arriving with C++0x, I would discourage using it with C++03's meaning in code.
I use this keyword to explicitly document when it is critical for function, that the variable be placed on the stack, for stack-based processors. This function can be required when modifying the stack prior to returning from a function (or interrupt service routine). In this case I declare:
auto unsigned int auiStack[1]; //variable must be on stack
And then I access outside the variable:
#define OFFSET_TO_RETURN_ADDRESS 8 //depends on compiler operation and current automatics
auiStack[OFFSET_TO_RETURN_ADDRESS] = alternate_return_address;
So the auto
keyword helps document the intent.
In old compiler, auto was one way to declare a local variable at all. You can't declare local variables in old compilers like Turbo C without the auto keyword or some such.