1 type conversions and template arguments
1) Conversions only: const conversion, array and function-to-pointer conversions
Note: Different sizes of the same element types are different types
2) The same argument type must be the same for the same template parameter name
3) The argument types corresponding to different template parameter names can be different, but must be compatible
2 return value issues for function templates
The function template infers only the type of the function argument list and does not infer the return value
Workaround:
1) Display template parameters
Note: The display specifies that the template type parameter is the same as the normal function parameter on the type conversion
Template <typename T1, TypeName T2, TypeName t3>
T1 alternative_sum (T2, T3)
------------------------------------------------------------------------------
Auto V = alternative_sum<int> (i, j);
V is int
2) Tail return type
Template <typename it>
Auto FCN (it first, it larst), Decltype (*first)
{
return *first;
}
Note: The decltype operator of an iterator can only return a reference to an object, not a value.
Can be Remove_reference<decltype (*first) >::type
3 function pointers and argument inference
The program context must satisfy: the type or value can be uniquely determined for each template parameter
4 References and template argument inference
1) lvalue Reference
t&---> Arguments must be an lvalue
F1 (i) I (int) T int
F1 (CI) CI (const int) T const INT
F1 (5) error, not lvalue
Const t&---> Arguments can be objects (const or non-const), temporary objects, or literal constants
Note: When a function argument is const, T is not a const type
F2 (i) I (int) T int
F2 (CI) CI (const int) T INT
F2 (5) error, not lvalue T int
Note: The const parameter can be bound to a right value
2) rvalue Reference
t&&
------Access Argument T
Lvalue lvalue Reference
Right- value Object type
Lvalue references lvalue reference
Rvalue reference rvalue reference
Reference folding can only be used to indirectly create references to references.
Common uses for the right direction: 1) template forwarding, template overloading
5 Std::move Principle
Template <typename t>
TypeName remove_reference<t>::type&& Move (t&& T) {
Reurn static_cast<typename remove_reference<t>::type&&> (T);
}
Note: Turning an lvalue static_cast to an rvalue reference is allowed.
6 Forwarding Std::forward
forward<t> return type is t&&
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + template argument inference