Auto and decltype, autodecltype
1. the compiler determines the type of the variable by analyzing the expression type. Therefore, the variable defined by auto must have an initial value.
Auto I = 10; // OK, I is an integer auto j; // error, which must be initialized during definition. J = 2;
2. auto can declare multiple variables in a statement, but the initial value types of all variables in this statement must be the same.
Auto I = 0, * P = & I; // OK, I is an integer, p is an integer pointer auto a = 2, B = 3.14; // error, the a and B types are inconsistent.
3. auto ignores the top-level const, while the bottom-level const is retained.
Const int a = 2, & B = a; auto c = a; // a is an int type, not a const int, that is, the top-level constauto d = &; // d is a pointer to the const int, that is, the underlying const is retained.
If you want the auto type to be a top-level const, you must specify the following:
Const auto e = a; // e is of the const int type.
4. When array is used as the initial value of the auto variable, the inferred variable type is Pointer rather than array.
Int a [10] = {1, 2, 4, 5, 6, 7, 8, 9, 0} auto B = a; // B is of the int * type, point to the first element of the array int c [2] [3] = {1} auto d = c; // d is int (* d) [3] type array pointer for (auto e: c) // e is int * type, not int (*) [3] for (auto & f: c) // f is int (& f) [3] // ********************************* * ************** decltype () c; // c is an array composed of 10 integer numbers. c [10]
The decltype and auto function types are slightly different:
1. decltype determines the variable type based on the expression type, but Initialization is not required during definition.
Int a = 2; decltype (a) B; // B is int type B = 3; int & c = a; decltype (c) d =; // d is int & type, so Initialization is required during definition
2. The unreference pointer operation will get the reference type.
Int a = 2, * B = a; decltype (* B) c = a; // dereference, c is int & type, so it must be initialized
3. Add () to the expression used by decltype to obtain the reference of this type.
Int a = 2; decltype (a) B = a; // B is int & type, not int type, must initialize decltype (a) c; // c is int type
4. When decltype is used as the variable hour group, an array of the same type is obtained, instead of a pointer.
Int a [2] = {1, 2} decltype (a) B = {3, 4} // int B [2] Type
5. When the variable used by decltype is a function, the obtained function type is not a function pointer.
Int fun (int a); decltype (fun) * f (); // function f returns int (*) (int), that is, the function pointer, and decltype (fun) is int (int) Type