Generic semantics
Generic (Generic programming), which means that it has the meaning of being operable on a variety of data types. Generic series
Representative works of the process STL is an efficient, generic, interoperable software component.
Generic programming was originally born in C + + to implement STL (Standard Template Library) for C + +. Its language support
The mechanism is the template (Templates).
The spirit of the template is actually very simple: type parameterized, that is, the type is also a parameter,
is also a kind of static polymorphism. In other words, the type information in an algorithm or class that is originally specific to a type is pumped out,
Extract to make template parameters.
function templates
Introduction: Overloaded functions, although to a certain extent achieved the purpose of multi-type adaptation, but not thorough, and there are two of the existence of the semantic.
#include <iostream>using namespacestd;voidMyswap (int& A,int&b)
{intt =A; A=b; b=T;}
void Myswap (Double& A,Double&C)Doublet =A; A=b; b=T;}
int main ()
{LongA =2;Longb =3; Myswap (A, b); //ambiguouscout<<a<<b<<Endl; return 0;}
function templates
Syntax: In a function's parameter table, the return type and the function body are using parameterized types.
template<typename/class type parameter T1, typename/class type parameter T2,... >
Feature: The structure is no different from the normal function, but it is generalized on the incoming parameter and the return value.
return type function template name (function argument list) {function template definition body}
Case: (The concept is more abstract, please observe its characteristics through the case)
As for the type, we will explain in the blog iteration how the situation of several different types of parameters is handled.
The difference between a function template and a normal function is that the function parameters are highly abstracted, so that they have the ability to handle more data types.
Feature summary
1) Strict matching, there is no implicit conversion.
2) First instantiate, then call.
3) Type parameters can be omitted.
4) The ruler is long and the inches are short.
Principle:
The compiler does not treat function templates as functions that can handle any class, and the compiler passes from the function template
The specific type produces different functions; The compiler compiles the function template two times: in the place of the Declaration, the template
The code itself compiles, and the code after the replacement of the parameter is compiled where it is called.
Application of function template--templating of fast sorting algorithm
#include <iostream>#include<typeinfo>using namespacestd;
Template<typename t>voidQuickSort (T * array,intLeftintRight ) {if(left<Right ) {intLow = left;intHigh =Right ; T Pivot=Array[low]; while(low<High ) { while(Array[high] >= pivot && high>Low ) high--; Array[low]=Array[high]; while(Array[low] <= pivot&& high>Low ) Low++; Array[high]=Array[low];
}
Array[low]=pivot; QuickSort (Array,left,low-1); QuickSort (Array,low+1, right); }}
int main () {intarray[Ten] = {1,3,5,7,2,4,6,8,0,9}; QuickSort<int> (Array,0,9); for(Auto I:array) {cout<<i<<Endl; }}
Default parameters for function templates
A function template that, when invoked, is instantiated as a template function before being called. Of course, you can also set the default class
Default value for the type. Because of the powerful automatic derivation of the system, sometimes the default is not too much significance.
int>void quickSort (T * array,intint right)
Template specificity
is to instantiate a special instance version of a particular type of argument when the template is instantiated.
int Compare (t &a, T &B) templateintconstcharconst CharconstChar* &b)
When a template is used for a formal parameter with a special definition, the special version is called , and the template is customized into full and partial
, the function template of the special, can only be full of special;
For example, when we compare the size of two numbers:
#include <iostream># Include <string . H>using Span style= "COLOR: #0000ff" >namespace STD;
Template <typename t> int compare (t &a, t &b) { if (a > B) return Span style= "COLOR: #800080" >1 ; else if (A < b) return -1 ; else return 0 ;}
The actual parameter is twoCharpointer, the size of the pointer is compared,//Instead of pointing to the size of the content, you need to define a specific version of the function template, the special place version of manager : template<>intCompare <Const Char* > (Const Char* &a,Const Char* &b) {returnstrcmp (A, b);}
int main () {intA =3;intb =5; cout<<compare (b) <<Endl; stringSTR1 ="ABC", str2 ="ABC"; cout<<compare (STR1,STR2) <<Endl; Char* P1 ="ABC", *p2="def"; cout<<compare (P1,P2) <<Endl; cout<<compare (P2,P1) <<Endl; return 0;}
On the recognition of template specificity
Reasons for template specificity: the logic or function of the current function template does not meet the requirements of a particular parameter.
Template Special way: the need to special parameters in advance "layout" to the template, pre-booking templates, and then pass the parameters that need to be special can be
A template that has been specially typed by this parameter is called first.
Applicable scenarios
A function template that applies only to functions with the same number of arguments and different types, and with the same function body. If
If the number is different, you cannot use a function template.
function templates for C + + generic programming