Auto keyword,
Auto Keyword:
1. C ++ 98: The standard auto keyword is used in the same way as the C language to indicate automatic variables. It is a type modifier about the storage location of variables. It is usually not written because the default storage of local variables is auto.
1 void foo (void) 2 {3 int a; // The variable is stored in the stack Zone 4 auto int B; // The automatic variable is stored in the stack Zone 5 static int c; // static variables, stored in the data Zone 6 register int d; // register variables, stored in registers 7}
2. In C ++ 11 standard, the auto keyword does not represent the storage type of variables, but is used for type derivation.
(2.1) basic usage of auto
1 void foo (void) 2 {3 auto a = 1; // a is int type 4 auto B = new auto (2 ); // B is int * type 5 auto const * c = & a; // c is const int * type 6 static auto d = 4.0;/* d is double type, in the old syntax, auto type variable 7 is stored in the stack zone, and static type variables are stored in the static 8 zone. Both variables cannot be used at the same time, but in the new syntax, 9 auto is no longer used as the storage type indicator. There is no conflict between 10 and static keywords. You can use it with */11 auto e; // error, in C ++ 11 standard, the auto variable must be initialized 12 auto const * f = & a, g = 4.0; // error, the Type derivation cannot have ambiguity 13 auto const * h = & a, I;/* error. Although auto can be obtained from & a, it indicates the int type 14, however, I still need to display initialization */15 auto int j = 3; // error. auto cannot be combined with any other type specifiers for 16}
(2.2) use auto in combination with pointer or reference
1 void foo (void) 2 {3 int a = 0; 4 auto * B = & a; // B is int * type 5 auto c = &; // c is int * type 6 auto & d = a; // d is int & type 7 cout <& d <''<& a <endl; // 8 auto e = d with the same address; // e is of the int type. When an expression has a reference attribute, auto discards the reference attribute, 9 cout <& e <<<'' <& a <endl; // auto const f = a with 10 different addresses; // f is the 11 cout of int const type <++ f <endl; // error, f has the regular attribute 12 auto g = f; 13 cout <+ + g <endl; // the value of g is 1. When the expression has a CV limit, auto discards its CV limit of 14 auto const & h =; 15 auto & I = h; // I is int const & type 16 cout <++ I <endl; // error. If auto is used with reference or pointer, the CV limitation of the expression is retained 17 auto * j = & h; // j is int const * type 18 cout <++ * j <endl; // error, if auto is used with a reference or pointer, the CV limitation of the expression will be retained 19}
(2.3) Restrictions on auto usage
(2.3.1) auto cannot be used for function parameters.
Void foo (auto a) // error
{
Cout <typeid (a). name () <endl;
}
Use the function template instead of auto, as shown below:
Template <typename T>
Void foo (T a = T ())
{
Cout <typeid (a). name () <endl;
}
(2.3.2) non-static data members of the class cannot contain the auto type.
Class
{
Public:
Int m_x = 0;
// Non-static data members of the class cannot contain the auto type
Auto m_y = 1; // error
Static auto const m_z = 2;
};
(2.3.3) auto cannot be used as a real parameter of the template type.
1 template <typename T> 2 class B 3 {4 public: 5 B (T const & arg): m_var (arg) {} 6 T m_var; 7 }; 8 void main () 9 {10 B <int> b1 (0); // true11 B <auto> b2 = b1; // error, auto cannot be used for the template type parameter 12}
(2.3.4) auto cannot be used for array elements.
Int arr1 [10];
Auto arr2 [10] = arr1; // error, auto cannot be used for array elements
Auto arr3 = arr1; // true, arr3 is of the int * type, and arr1 represents the first address of the array.
Auto & arr4 = arr1; // true, arr4 is of the int (&) [10] type, and arr1 represents the whole array.
Cout <typeid (arr4). name () <endl; // int [10]
(2.4) when to use auto
(2.4.1) Reduce template type parameters through auto
1 class A 2 {3 public: 4 A (int arg = 0): m_var (arg) {} 5 int get (void) const 6 {7 return m_var; 8} 9 void set (int arg) 10 {11 m_var = arg; 12} 13 private: 14 int m_var; 15}; 16 17 class B18 {19 public: 20 B (const char * arg): m_var (arg) {} 21 const char * get (void) const22 {23 return m_var; 24} 25 void set (const char * arg) 26 {27 m_var = arg; 28} 29 private: 30 const char * m_var; 31}; 32 33 // template <typename V, typename X> 34 template <typename X> 35 void foo (X const & x) 36 {37/V var = x. get (); 38 auto var = x. get (); 39 cout <typeid (var ). name () <endl; 40} 41 42 void main (void) 43 {44 A (1234); 45 // foo <int> (); 46 foo (a); // reduce the type parameter of the template using auto 47 48 B B ("abcd"); 49 foo (B); 50}
(2.4.2) simplify complex writing with auto
1 void foo (void) 2 {3 multimap <string, int> msi; 4 msi. insert (make_pair ("Zhang Fei", 100); 5 msi. insert (make_pair ("zhao Yun", 90); 6 msi. insert (make_pair ("Guan Yu", 80); 7 msi. insert (make_pair ("Zhang Fei", 95); 8 msi. insert (make_pair ("zhao Yun", 85); 9 msi. insert (make_pair ("Guan Yu", 75); 10 pair <multimap <string, int >:: iterator, multimap <string, int >:: iterator> range1 = msi. pai_range ("Zhang Fei"); 11 int sum1 = 0; 12 for (multimap <str Ing, int >:: iterator it = range1.first; it! = Range1.second; ++ it) 13 {14 sum1 + = it-> second; 15} 16 cout <sum1 <endl; 17 18 auto range2 = msi. pai_range ("Zhang Fei"); 19 int sum2 = 0; 20 for (auto it = range2.first; it! = Range2.second; ++ it) 21 {22 sum2 + = it-> second; 23} 24 cout <sum2 <endl; 25}