Is there a reason to use the auto keyword for C ++ 03?

Note this question was originally published in 2009 before C ++ 11 was ratified and before the meaning of the auto keyword was radically changed. The answers given refer only to the value of C ++ 03 auto - that the specified storage class, and not the value of C ++ 11 auto - is an automatic type inference. If you are looking for tips on when to use C ++ 11 auto , this question is not relevant to this question.

For a long time, I thought it didn't make sense to use the static in C, because variables declared outside the scope of the block were implicitly global. Then I discovered that declaring a variable as static within a block area would give it a constant duration, and declaring it outside the block area (in the program area) would give it a file area (it can only be accessed in this compilation block).

So, this leaves me with only one keyword that I (perhaps) have not yet fully understood: auto . Is there any other meaning besides the "local variable"? Everything that he does is implicitly done for you, wherever you want to use it? How does the auto variable behave in the program area? What of the static auto variable in the file area? Does this keyword have any purpose other than that existing for completeness?

+84
c ++ keyword c ++ 03
Jun 25 '09 at 22:05
source share
10 answers

auto is the storage class specifier, static , register and extern too. You can use only one of these four in your ad.

Local variables (without static ) have an automatic storage duration, which means that they live from the very beginning of their definition to the end of their block. Putting a car in front of them is redundant, as this is the default.

I do not know why to use it in C ++. In older versions of C that have an implicit int rule, you can use it to declare a variable

 int main(void) { auto i = 1; } 

To make it valid syntax or disambiguate from an assignment expression in case i is in scope. But that still doesn't work in C ++ (you need to specify a type). Pretty funny, the C ++ standard writes:

An object declared without a storage class specifier in the block area or declared as a function parameter defaults to automatic storage duration. [Note: therefore, the auto-specifier is almost always redundant and not often used; one use of auto is to explicitly distinguish a declaration statement from an expression-expression (6.8). -end note]

This applies to the following scenario, which can either be a to int or a variable declaration of type int with redundant parentheses around a . It is always considered an ad, so auto will not add anything useful here, but instead will be for the person. But, again, it would be better for a person to remove the extra parentheses around a , I would say.

 int(a); 

With the new auto value arriving with C ++ 0x, I would discourage using it with C ++ 03, which means code.

+73
Jun 25 '09 at 22:13
source share

In C ++ 11, auto has a new meaning: it allows you to automatically infer the type of a variable.

Why is it ever useful? Consider a basic example:

 std::list<int> a; // fill in a for (auto it = a.begin(); it != a.end(); ++it) { // Do stuff here } 

auto creates an iterator of type std::list<int>::iterator .

This can make very complex code much easier to read.

Another example:

 int x, y; auto f = [&]{ x += y; }; f(); f(); 

There auto deduced the type needed to store the lambda expression in a variable. Wikipedia has a good coverage on the topic.

+86
Jun 14 2018-12-12T00:
source share

The keyword auto has no purpose at the moment. You are absolutely right that it simply repeats the default storage class for a local variable, and a really useful alternative is static .

It has a new meaning in C ++ 0x. This gives you some idea of ​​how useless it was!

+34
Jun 25 '09 at 22:06
source share

GCC has a special use of auto for nested functions - see here .

If you have a nested function that you want to call before defining it, you need to declare it with auto .

+7
Jun 26 '09 at 7:34
source share

"auto" supposedly tells the compiler to decide for itself where to put the variable (memory or register). Its counterpart is "register", which supposedly tells the compiler to try to keep it in the register. Modern compilers ignore both, so you should too.

+3
Jun 25 '09 at 23:07
source share

I use this keyword to explicitly document when it is important for a function to have a variable pushed onto the stack for processors based on the stack. This function may be required when changing the stack before returning from the 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; 

In this way, the auto keyword helps document intention.

+3
Jun 14 2018-12-12T00:
source share

According to Straustrup, in the C programming language (4th edition, spanning C 11), the use of "auto" has the following main reasons (section 2.2.2) (Stroustrup words are quoted):

one)

The definition is in a large area where we want to make the type clearly visible to readers of our code.

With the help of "auto" and the necessary initializer, we can immediately find out the type of variable!

2)

We want to be clear about the range or the accuracy of the range (e.g. double, not float)

In my opinion, a case that fits here looks something like this:

  double square(double d) { return d*d; } int square(int d) { return d*d; } auto a1 = square(3); cout << a1 << endl; a1 = square(3.3); cout << a1 << endl; 

3)

Using 'auto', we avoid redundancy and writing long type names.

Imagine some long type name from a named template:

(code from section 6.3.6.1)

 template<class T> void f1(vector<T>& arg) { for (typename vector<T>::iterator p = arg.begin(); p != arg.end(); p) *p = 7; for (auto p = arg.begin(); p != arg.end(); p) *p = 7; } 
+2
Jun 06 '13 at 0:23
source share

In the old compiler, auto was one way to declare a local variable in general. You cannot declare local variables in older compilers such as Turbo C without the auto keyword or some of them.

+1
Jun 07 '10 at 20:31
source share

The new meaning of the auto keyword in C ++ 0x is very well described by Microsoft Stephan T. Lavavej in the freely viewed / downloaded STL video lecture found on MSDN channel 9 here .

The whole lecture is worth a look, but part of the auto keyword is approximately equal to the 29th minute mark (approximately).

+1
Nov 19 '10 at 18:22
source share

Is there any other meaning for "auto" than "local variable"?

Not in C ++ 03.

Everything that he does is implicitly done for you, wherever you want to use it?

Nothing in C ++ 03.

How does an automatic variable behave in a program area? What of a static automatic variable in a file area?

The keyword is not allowed outside the function / method body.

Does this keyword have any purpose [in C ++ 03] different from the existing one for completeness?

Amazing yes. C ++ design criteria included a high degree of backward compatibility with C. C having this keyword, and there was no real reason to prohibit it or override its value in C ++. Thus, the goal was one less incompatibility with C.

Does this keyword have any purpose in C other than the existing one for completeness?

I only found out about this recently: the ease of porting old programs from B. C evolved from B, whose syntax was very similar to C. However, B had no types. The only way to declare a variable in B is to specify your storage type ( auto or extern ). Like this:

auto i;

This syntax still works in C and is equivalent

int i;

since in C, the storage class defaults to auto and defaults to int . I assume that every single program that originated in B and was ported to C was literally filled with auto variables at that time.

C ++ 03 no longer allows implicit C-style ints, but it retained the auto keyword, which did not contain more time, because, unlike implicit ints, it was not known that it caused any problems in the C syntax.

0
Sep 30 '15 at 16:45
source share



All Articles