C + + provides a function template mechanism for us. The so-called function template, in fact, is to establish a common function, whose function type and parameter types are not specified, with a virtual type to represent. This generic function is called a function template.
All functions of the same function can be replaced with this template, do not have to define more than one function, only in the template defined once. When the function is called, the system replaces the virtual type in the template according to the type of the argument, thus enabling the functions of the different functions.
Why should I have a function template
below, let's use an example to illustrate why a function template is needed.
Requirements: Writes n functions, exchanging the values of char type, int type, and double type variables.
If the function template is not applicable, our code needs to write this:
void swap (int &a, int &b)
{
int t = A;
A = b;
b = t;
}
void Swap (char &a, char &b)
{
char t = A;
A = b;
b = t;
}
Such code is always cumbersome, and almost the same code is repeated many times, because we have a function template mechanism. With the function template, our code can write this:
#include <iostream>
using namespace std;
Template keyword tells C + + compiler I'm going to start generics. You don't have to be random. Error
//data type T parameterized data type
template <typename t>
void Myswap (t &a, t &b)
{
T t;
t = A;
A = b;
b = t;
}
void Main ()
{
int x = 1;
int y = 2;
Myswap (x, y); The way of the automatic data type derivation
float a = 2.0;
Float b = 3.0;
Myswap (A, b); The way of automatic data type derivation
myswap<float> (A, B);//Display type call
cout<< "Hello ..." <<endl;
return;
}
We can see that this can greatly reduce the amount of code, let us programming more convenient.
function Template Syntax
function template definition form:template< type form parameter table >
Type form parameters are:typename T1, TypeName T2, ..., TypeName Tn or class T1, class 4> T2, ..., class Tn
function template Call :
Myswap (x, y); How automatic data type is deduced
function template to do function parameters
function template can be used as a function parameter, we can write a simple sort of code to verify.
#include <iostream>
using namespace std;
Template<typename T, TypeName t2>
void Sortarray (t *a, T2 num)
{
T tmp;
int I, J;
For (i=0 i<num; i++)
{for
(j=i+1; j<num; J + +) {
if (A[i] < a[j])
{
tmp = a[i];
a[i] = a[j];
A[J] = tmp;
}}} Template<class t>
void Pirntarray (T *a, int num)
{
int i = 0;
For (i=0 i<num; i++)
{
cout<<a[i]<< "";
}
cout << Endl;
}
int main ()
{
int num = 0;
Char a[] = "DDADEEETTTTT";
num = strlen (a);
cout << "sort before" << Endl;
Pirntarray<char> (A, num);
Sortarray<char, int> (A, num); Display Type call template function <>
cout << "sort after" << Endl;
Pirntarray<char> (A, num);
cout<< "Hello ..." << Endl;
return 0;
}
Final output results:
function template encounters a function overload
The difference between a function template and a normal function: The function template does not allow automatic type conversions, whereas normal functions allow automatic type conversions
When a function template is together with a normal function, the calling rule is as follows:
Function templates can be overloaded like normal functions the C + + compiler takes precedence over ordinary functions if a function template can produce a better match, the selection template can be defined by the syntax of the empty template argument list, qualifying the compiler only through template matching I'll demonstrate the process through code:
#include <iostream>
using namespace std;
Template <typename t>
void Myswap (t &a, T &b)
{
T t;
t = A;
A = b;
b = t;
cout<< "Myswap template function Do" <<endl;
}
void Myswap (char &a, int &b)
{
int t;
t = A;
A = b;
b = t;
cout<< "Myswap normal function do" <<endl;
}
int main ()
{
char cData = ' a ';
int idata = 2;
Myswap<int> (CData, idata); Conclusion function templates do not provide implicit data type conversions must be strictly matched
//myswap (CData, idata);
Myswap (Idata, cData);
cout<< "Hello ..." <<endl;
return 0;
}
When we run the code like this, the compiler complains. Thus, we can conclude that function templates do not provide implicit type conversions and must be strictly matched. Next, let's look at another piece of code:
#include <iostream>
using namespace std;
int max (int a, int b)
{
cout<< "int max (int a, int b)" <<endl;
Return a > B? a:b;
}
Template<typename t>
t Max (t A, T B)
{
cout<< "T Max (t A, T b)" <<endl;
Return a > B? a:b;
}
Template<typename t>
t Max (t A, T B, t C)
{
cout<< "T Max (t A, T B, t C)" <<endl;
Return Max (Max (A, B), c);
}
int main ()
{
int a = 1;
int b = 2;
Cout<<max (A, b) <<endl; When the function template and the normal function all meet the call, select the normal function
cout<<max<> (A, B) <<endl;//If the function template is displayed, use the <> type list
Cout<<max (3.0, 4.0) <<endl; If the function template produces a better match using the function template
Cout<<max (5.0, 6.0, 7.0) <<endl;//Overload
Cout<<max (' A ', MB) << Endl; Calling normal functions can be implicitly typed return
0;
The results of the operation are as follows:
implementation of C + + compiler template mechanism
Through the above learning, we will have a problem: why function templates can and function overload can be placed together. How the C + + compiler provides a function template mechanism. We can solve this problem by observing the disassembly, because the assembly code is too long, so there is no paste, we are interested to try their own. By observing the disassembly code, we can conclude that the compiler does not treat function templates as functions that can handle any class; The compiler produces different functions from a function template by a specific type; The compiler compiles a function template two times: the template code itself is compiled where it is declared, Compile the code after the parameter is replaced at the place where it was invoked.