One
auto
Keywords in previous lives
Starting with the C language, the auto
keyword is treated as a storage type modifier for a variable that represents an automatic variable (local variable). It cannot be used alone, or the compiler will give a warning.
#include <stdio.h>int main () {int a = 123 Span class= "K" >auto int b = 234 Span class= "K" >auto c = 345printf (\n" Span class= "P", abc ); return 0;}
Compile run Result:
$ gcc main.cmain.c:7:7: warning: type specifier missing, defaults to ‘int‘ [-Wimplicit-int] auto c = 345; ~~~~ ^1 warning generated.$ ./a.out a = 123, b = 234, c = 345
Two
auto
Key words of this life
In the C + + 11 standard, a new type derivation attribute has been added, which, given the auto
very few uses of the keyword, gives it the ability to re-assert the variable that the type was deduced by the compiler. In C + + 11, a auto
variable with a definition cannot be decorated with another type modifier, and the type of the variable is determined automatically by the compiler based on the initialization data. auto
A variable of type must be initialized.
#include <iostream>int main () {int a = 21 Span class= "K" >auto b = a//typeid can get the name of the variable or data type std::cout << typeid (b). Name () << std::endl return 0;}
1. When compiling using the C + + 98 standard, a warning appears:
$ clang++ main.cpp -std=c++98main.cpp:6:2: warning: ‘auto‘ type specifier is a C++11 extension [-Wc++11-extensions] auto b = a; ^1 warning generated.$ ./a.out i #输出结果为整数类型
Use C + + 11 for compilation:
$ clang++ main.cpp -std=c++11$ ./a.out i
2. However, if it is still auto
used as a storage type modifier, a warning will appear under the C + + 11 standard:
#include <iostream>int main () {int a = 21 Span class= "K" >auto int b = a Span class= "C1" >//typeid can get the name of the variable or data type std::cout << typeid (b). Name () << std::endl return 0;}
Compiling with C + + 98:
$ clang++ main.cpp -std=c++98$ ./a.out i
Instead of using C + + 11 compilation, a warning appears and is no longer allowed as a storage type modifier:
$ clang++ main.cpp -std=c++11main.cpp:6:2: warning: ‘auto‘ storage class specifier is not permitted in C++11, and will not be supported in future releases [-Wauto-storage-class] auto int b; ^~~~~1 warning generated.
3. A variable of auto
type must be initialized, and the definition variable cannot be used alone in C + + 98 auto
.
$ clang++ MAIN.CPP-STD=c++98main.cpp:6:2:warning: ' auto ' type specifier is a c++11 extension [-wc++11-extensions ' B ' with type ' auto ' requires an initializer auto B ^1 warning and 1 error generated. $ clang++ main.cpp main.cpp:6:2:warning: ' auto ' type specifier is a c++11 extension [-wc++11-extensions "auto B ^main.cpp:6:7:error: Declaration of variable ' B ' with type ' auto ' requires an initializer Auto B ^1 warning and 1 error generated.
Third, expansion
It can also be used in C + + decltype
to get the type of a variable or expression and to define a new variable.
#include <iostream>int main () {int a = 21decltype (a) bstd::cout << typeid (b Name () << std::endl return 0;}
Compile run Result:
$ clang++ main.cpp -std=c++98$ ./a.out i$ clang++ main.cpp -std=c++11$ ./a.out i
It is important to note that the standard only changes the use of Auto in C + +, but it does not affect auto in the C language!
This document is organized by the Changsha Camp David Education.
Use of the C + + Tutorial Auto keyword