This article records some of my own understanding and experience when I read effective modern C + +.
Item1: Template type derivation
1) The reference property cannot pass in a template function by passing a value parameter . This means that if a template function requires a parameter of type reference, it must be declared as reference in the template declaration, otherwise the result of the type deduction will not have the reference attribute, even if the template function is called with a variable of type reference.
2) The constant and volatile properties cannot be passed into the template function through the pass-through parameters , but they can be passed through the reference parameter.
3) If the Universal reference parameter is declared in the template, then the Lvalue argument is deduced as an lvalue reference, and the rvalue argument is deduced as Universal reference (&&).
4) The array type variable and the function type variable will be deduced into the corresponding pointer type variable by the value parameter. If the template is declared as a reference parameter, it is inferred that the array reference (containing the size information of the arrays) and the function reference are deduced.
Item2:auto type derivation
1) The Auto type deduction rule is the same as the template type deduction rule, in addition to handling parameters initialized with {}.
2) The template cannot deduce a parameter that was initialized by {} unless it is declared as a parameter of type std::initializer_list<t>.
#include <iostream>
Template<typename t>
void F1 (T param) {return;}
Template<typename t>
void F2 (std::initializer_list<t> param) {return;}
int main ()
{
Auto i = {1, 2, 3, 4};
F1 (i); Good
F1 ({1, 2, 3, 4}); Error
F2 ({1, 2, 3, 4}); Good
return 0;
}
3) in c++14, a function that returns a value of auto must not return a value initialized with {}. The auto type parameter of the lambda function cannot also be called with the {} initial value as a parameter.
Auto F1 () {return 4;}//good
Auto F2 () {Auto i = {1,2,3,4}; return i; }//good
Error Auto F3 () {return {1,2,3,4};}//error
< to Be continued >
[C++11] Effective modern C + + reading notes