c++ - Is there any reason to use the 'auto' keyword in C++03? -
note This question was originally posted in 2009, before C ++ 11 was confirmed and there was considerable change before the meaning of
auto
keywords.Auto
means only related to C ++ 03 - it is specified as a storage category - and not C ++ 11- This is an automatic type of deduction if you are looking for advice on using C ++ 11
Auto
, this question is not relevant to that question. .
For the longest time I thought there was no reason to use the static
keyword because the declared variables outside the block area were completely global Then I came to know that declaring a variable in block-area as a "variable" would give it a permanent duration and declare it outside the block-zone (in the programming area), it will give the file-scope (Only used in that compilation unit Is done).
So it leaves me with only one keyword that I (probably) still can not fully understand: auto
keywords other than local variables Have other meanings? Whatever it does, is it not for you, wherever you want to use it? How does a auto
variable behave in the field of the program? What is a stable auto
variable in the file-scope? Is this keyword any purpose other than existing for perfection ?
auto
a storage class specifier, static
, register
and extern
you can only use one of these four in the announcement.
Local variables (without static
) have the automatic storage period, which means that they remain from the beginning of their definition until the end of their block. Auto-inserting in front of them is futile because it is default anyway.
I do not know for any reason to use C ++ in the older versions that is the built-in int rule, you can use it to declare a variable
< Pre> int main (zero) {auto i = 1; }
i
is within the scope of, to make it a valid syntax or to disorganize the assignment expression. But it does not work in C ++ anyway (you have to specify a type). Quite a fun, C ++ standard writes:
In Block Scope, the declared object or function parameter is declared without storage-class-specifiers, by default the automatic storage period is. [NOTE: Therefore, the auto specifier is almost always redundant and is not often used; One use of Auto Expressation statement (6.8) is clearly to differentiate between a declaration-statement. -note note]
which refers to the following scenario, which is either a
or a variable a
type int
is unnecessary bracket around a
. It is always taken as an announcement, therefore, automatically
does not add anything useful here, but rather for human, rather than that again, humans will be better a
To remove the redundant brackets around, I will say.
int (a); The new meaning of auto
is coming with C ++ 0x, I discourage it from using code in the code with the meaning of C ++ 03.
Comments
Post a Comment