Deduction of the type of Item 2 auto in objective Modern C ++, objective tivemodernc
Note:
You also need to learn
↑Fragments in such a boxNot from the original bookBut my own understanding.
Item 2 Understand
AutoType deduction-
AutoType Derivation
Before C ++ 11,AutoKeywords have always been used to declare variables of the Automatic Storage type, basically have no practical effect, status andExportKeyword (used to export a template outside the compilation unit, which is also canceled in C ++ 11) is similar.
In C ++ 11,AutoFinally, no waste of materials is available.VarKeyword effect. You can automatically export the type of the variable, which can be less than a few words:
var map = Dictionary<string, List<string>>();
When usingAutoWhen declaring a variable, the declared type can beParamTypeExample:
Template <typename T> void f (ParamType param); // example of Item1 auto x = 27; const auto cx = x; const auto & rx = x;
WhereAutoInstead of the originalTThe location of this type of parameter, suchConst auto &Is formedParamTypeThis part. To export data.AutoIndicates the type.
Similarly,AutoThere are also three situations:
The expression of Case 2 is the same as the type derivation in Item1:
Auto x = 27; const auto cx = x; const auto & rx = x; auto & uref1 = x ;//Bind to the left value,Uref1IsInt &Auto & uref2 = cx ;//BindConstLeft,Uref2IsConst int &Auto & uref3 = 27 ;//Bind to the right value,Uref3IsInt &&
The universal reference is used on a type expression that does not determine the type and requires type derivation. specify this type as a reference, and then the compiler automatically exports the most appropriate reference type that complies with the rule.
In addition, the degradation of array and function names can also be usedAuto &Avoid:
Const char name [] = "R. N. Briggs"; // type: const char [13] auto arr1 = name ;//Degraded:Const char *Auto & arr2 = name ;//No degradation:Const char (&) [13]
AutoNotes
C ++ adds a new initialization syntax, that is, using{}. But C ++ 11 has also increased.Initializer_listThis template allows youAutoNote the following when deriving data types:
Auto x1 = 27; // x1 is intauto x2 (27); // same as above, use () to initialize auto x3 = {27}; // x3 is std :: initializer_list <int>, whose value is {27} auto x4 {27}; // same as above
"Braced-init-list" inAutoAnd in the for loop with colons (Range-based for loop ),Initializer_list.
Reference http://en.cppreference.com/w/cpp/utility/initializer_list
At the same time, different types of variables may causeInitializer_list <T>Unable to proceedTType derivation:
Auto x5 = {1, 2, 3.0 };//Error
X5CauseAutoDeducedInitializer_list <T>, Resulting inTFailed to deduce.
Processing of the braces initiator isAutoDifferent from the template in type derivation:
Auto x = {11, 23, 9}; // oktemplate <typename T> void f (T param); f ({11, 23, 9 });//Error. The template does not recognize the braces! Cannot be deducedT
However, you can specifyParamIsInitializer_list:
template<typename T>void f(std::initializer_list<T> initList);f({ 11, 23, 9 }); // T -> int, ParamType -> std::initializer_list<int>
Auto assumes that the initial list of braces is initializer_list, but the template does not.
In C ++ 14,Auto keywordIt can be used to derive the function return value and to declare the form parameter type of the lambda expression. In these two ways,Auto keywordThe template type deduction rules are implemented, instead of the auto type deduction rules.AutoCannot be deducedInitializer_list:
Auto createInitList () {return {1, 2, 3 };//AutoAn error occurred while deriving the returned value.} Std: vector <int> v; auto resetV = [& v] (const auto & newValue) {v = newValue ;};//AutoUsed to declareLambdaExpression parameter typeResetV ({1, 2, 3 });//Same failure
Note