1
Function Template 1.1 Function Template
Function template Definition
Templates <comma-separated-list-of-parameters>
....
L
The template function performs real-time parameter deduction. Therefore, when using the function template, you do not need to specify the parameter type.
L
in terms of compilation principles, the template is not compiled into a single entity that can process any type, instead, for each type of instantiated template parameters, a different entity is generated from the template.
L
if you try to instantiate a template based on the types of operations that do not support the template, a compilation error occurs, we can conclude that the template has been compiled twice:
n
before instantiation, check the template Code to check whether the syntax is correct.
n
During instantiation, check the template code to see if all templates are valid.
L
As the template mentioned above is to be instantiated, the template definition needs to be known during compilation. This is different from the differences between compilation and linking in common functions, because for common functions, only the declaration of the function can be compiled, but the template function cannot. Therefore, in general, template functions use inline functions.
1.2 Template parameters
Here we need to understand two concepts:
L
Template parameter, in angle brackets;
L
The called parameter is in parentheses;
Template <typename T1, typename T2>
InlineT1Max (T1
Const & A, T2 const &)
{
Return
A <B? B:;
}
Note that the return value of the above function is T1 Instead T1
Const & , Which must be T1 Because T2 const & Transform T1 A local variable is generated during the process, but the local variable cannot be returned as a reference.
If there are multiple template parameters, You can explicitly specify the first real parameter and let the deduction process export the remaining parameters. Generally, you must specify all the real parameters of the last template that cannot be interpreted before the real parameters.
For example:
Template <typename RT, typename T1, typename T2>
Inline RT max (T1 const & A, T2 const & B)
{
Return a <B?
B:;
}
1.3 Overload template Functions
First, reload is a very difficult topic. Here, there are some details that the average person (including me) does not understand. For more information, see the appendix of this book.
Let's look at an example:
// (1)
Inline int
Const & MAX (INT const & A, int const & B)
{Return a <
B? B: ;}
// (2)
Template <typename
T>
Inline t
Const & MAX (T const & A, T const & B)
{Return a <
B? B: ;}
// (3)
Template <typename
T>
Inline t
Const & MAX (T const & A, T const & B, T const & C)
{Return
: Max (a, B), c );}
Int main ()
{
: Max <> (7, 42 );//Call2
: Max ('A', 42.7 );//Call1
}
L
when other functions are the same, the reload parsing process usually calls non-template functions without generating an instance from the template. This is also easy to understand from the compiler perspective. If you instantiate another template, the code will become larger and duplicated.
L
the template does not allow automatic type conversion, therefore, the second call above will call (1) MSO-Hansi-font-family: "Times New Roman" ">, instead of ( 2 "Times New Roman"> ).
L
generally, when reloading a function template, it is best to change only the content that needs to be changed. That is to say, you should restrict your changes to the following two situations: changing the number of parameters, or explicitly specify template parameters. Otherwise, unexpected behaviors may occur, for more information, see p18.
.
L
the declaration of all overloaded versions of the function should be prior to the position where the function is called.