C + + provides us with a function template mechanism. The so-called function template, is actually to establish a general function, its function type and formal parameter type are not specified, and are represented by a virtual type. This general function is called a function template.
Functions with the same function body can be replaced with this template, do not have to define multiple functions, just one in the template can be defined. When a function is called, the system replaces the virtual type in the template based on the type of the argument, thus implementing the functions of the different functions.
Why do you have a function template
Here's an example of why a function template is needed.
Requirement: Write n functions, exchanging values for char type, int type, double type variable.
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 the C + + compiler I'm going to start generics. You don't have to give me any 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);//Automatic data type derivation by float a = 2.0;float B = 3.0;myswap (A, B);//method of automatic data type deduction Myswap<float> (A, b); The display type calls cout<< "Hello ..." <<endl;return;}
As we can see, this can greatly reduce the amount of code that makes our programming easier.
function Template Syntax
function template definition form: template< type form parameter table >
The form parameters of the type are:typename T1, TypeName T2, ..., TypeName Tn or class T1, C Lass T2, ..., class Tn
function template Invocation :
function templates do function arguments
function templates can be used as function parameters, 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; in t 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 << "before sorting" << endl;pirntarray<char> (A, num); Sortarray<char, int> (A, num); Display Type call template function <>cout << "after sorting" << endl;pirntarray<char> (A, num);cout<< "Hello ..." < < Endl;return 0;}
The result of the final output:
function templates encounter function overloading
the difference between a function template and a normal function is that the function template does not allow automatic type conversions, whereas normal functions allow automatic type conversions
when a function template and a normal function are together, the calling rule is as follows :
- Function templates can be overloaded like normal functions
- C + + compilers take precedence over common functions
- If the function template can produce a better match, then select the template
- The syntax of an empty template argument list can be used to qualify the compiler to match only through templates
below I will demonstrate this 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 that must be strictly matched//myswap (CDATA, iData);//myswap (IData, CDATA);cout<< "Hello ..." << Endl;return 0;}
When we run the code, the compiler will errorThus, we can conclude that the function template does not provide implicit type conversions and must be strictly matched . let's move on to 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 normal function conform to the call, 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 a function template produces a better match using a function template Cout<<max (5.0, 6.0, 7.0) <<endl; Heavy Duty Cout<<max (' a ', +) <<endl; Calling a normal function can implicitly type convert return 0;}
The results of the operation are as follows:
C + + compiler template mechanism implementation through the above study, we will have a problem: why function templates can and function overloads can be put together.
How does the C + + compiler provide a mechanism for function templates? we can solve this problem by observing the disassembly, because the assembly code is too long, so it is not posted here, we are interested to try it yourself. by observing the disassembly code, we can conclude that the compiler does not process the function template into a function that can handle any class , and the compiler produces different functions from the function template through the specific type , and the compiler makes the function template Two compilation : Compile the template code itself where it is declared, and compile the replacement code at the point where it is called .
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + function template and its implementation principle