Auto is introduced in c++11, which brings convenience to everyone.
Specific use and precautions for reference:
51647865
Https://www.cnblogs.com/lenmom/p/7988635.html
Usage:
The first is to automatically infer the type of the variable from the initialization expression at the time of the variable declaration. Suitable for types of lengthy and complex, template types, etc.
The second is a placeholder for the return value of a function when declaring a function
Precautions :
1. Variables that use the Auto keyword must have an initial value. Similar references
2. Function parameters and template parameters cannot be declared as auto.
3. Use the AUTO keyword to declare the type of a variable and not automatically derive the top-level cv-qualifiers and reference type unless the declaration is displayed
When using the Auto keyword for type deduction, if the initialization expression is a reference type, the compiler removes the reference unless the declaration is displayed
When using auto keyword for type deduction, the compiler automatically ignores the top-level const unless the declaration is displayed
See examples
intMain () {Auto Fnum=3.14; Auto Str ("Hello"); cout<<"fnum Type:"<< typeid (fnum). Name () <<Endl; cout<<"STR Type:"<< typeid (str). Name () <<Endl; Vector<int>VEC; Auto V_it=Vec.begin (); cout<<"v_it Type:"<< typeid (v_it). Name () <<Endl; intInt_ori =5; int&ori_b =Int_ori; Auto a=Ori_b; A=Ten; cout<<"Int_ori:"<< Int_ori <<"Ori_b:"<< Ori_b <<"A:"<< a <<Endl; Auto&b =Ori_b; b= -; cout<<"Int_ori:"<< Int_ori <<"Ori_b:"<< Ori_b <<"A:"<< a <<"B:"<< b <<Endl; Const intCint_a = -; Auto A2=cint_a; AutoConstA3 =cint_a; cout<<"A2 Type:"<< typeid (A2). Name () <<"A3 Type:"<< typeid (A3). Name () <<Endl; System ("Pause"); return 0;}
Description
The Auto &b display declares a reference, so the original value is changed. But a is not a reference to an independent variable that is only assigned, so it is not modified by B
The last variable A3 is a const int type such as the modified value will be error, and A2 is int can be modified
C++11 AUTO Type Practice