1. The basic idea of generics

Generic Programming (Generic
Programming) It's a language mechanism , A standard container library can be implemented through it . Like a class , Generics are also abstract data types , But generics are not object-oriented , It is the supplement and development of object-oriented .
In object oriented programming , When the algorithm is related to the data type , There are some defects in object-oriented abstract description of algorithms .
For example, the description of the stack :
class stack
{

   push( Parameter type )  // Stack algorithm

   pop( Parameter type )   // Stack algorithm

}

If you think of the pseudo code above as an algorithm description , no problem , Because the algorithm has nothing to do with parameter types . But if you write it as compiled source code , You have to specify the type , Otherwise, it cannot be compiled . Overload is used to solve this problem , That's right N Write different parameter types N individual push and pop algorithm , This is very troublesome , Code is not universal .
If the above description is modified as follows :
First specify a generic type T, It is not specified which type it is .
class stack< Parameter template T>
{

   push(T)  // Stack algorithm

   pop(T)   // Stack algorithm

}

Here's the parameter template T Equivalent to a place holder , When we instantiate a class stack Time ,T It will be replaced by a specific data type .
If you define an object S by statc type , In instantiation S If we will T appoint int Type rule :
This time class S Become :
class S
{
    push(int)  // Stack algorithm
    pop(int)   // Stack algorithm
}
Now I can weigh it class stack< Parameter template T> Is a class of classes , It can generate classes with different types of specific parameters .

2. Generics in C++ Application in

Generics in C++ The main implementation of template function and template class .
It is very tedious to use ordinary functions to implement an algorithm related to data type , For example, the addition of two numbers , There are many types to consider :
int add(int a,int b) { return a+b; }
float add(float a,float b) { return  a+b; }
....
Although in the C++ This problem can be solved by overloading functions in , But it is hard to write the same algorithm function repeatedly , More importantly, function overloading is static compilation , Too much memory at runtime .
We can use it here C++ To express the general function , as follows :
template<typename T> // Template declaration
T add(T a,T b) { return a+b; }  // Note the types of formal parameters and return values
At this time C++ The compiler will add Function to generate a corresponding function with specific parameter type and call .
for example :
#include <iostream>
using namespace std;
template <typename T>
T add(T a,T b)  // Pay attention to formal parameters and return types
{   
 return a+b;

void main()
{
    int num1, num2, sum; 
    cin>>num1>>num2;
    sum=add(num1,num2); // use int Match template parameters T, if sum,num1,num2 If the type is inconsistent, it cannot be matched .
    cout<<sum;
}

3. Properties of function templates

1) Function templates are not real functions , It's just C++ Compile to generate a mold of concrete function .
2)  Function templates themselves do not generate functions , The actual generated function is the function that replaces the function template , For example, in the example above add(sum1,sum2),
    This replacement is bound at compile time .
3)  Function templates do not compile only one copy to meet multiple needs , Instead, compile a copy for each function that replaces it .
4) Function templates do not allow automatic type conversion .
5) Function template cannot set default template arguments . such as template <typename T=0> may not .

4.C++ Syntax of template functions

template  <typename  Parameter template list …>
Function return type Function name ( parameter list …)
The two lines above can be merged into one .
for example :
The following expressions are equivalent and class  and typename It's interchangeable .
template  <typename T1, typename T2>
T1 fun(T1, T2, int )
{  //…..}
template  <typename T1,T2>  T1 fun(T1, T2, int )
{  //…..}
template  <class T1, class T2>
 T1 fun(T1, T2, int )
{  //…..}
template  <class T1,T2>  T1 fun(T1, T2, int )
{  //…..}

5.C++ Syntax of template class

template  <class  Template parameter list …>
class Class name
{ // Class body }
Implementation of members …
for example :
// Class declaration section , There are two template parameters T1,T2
template  <class T1, class T2 >  
class A {
   private:
   int a;
  T1 b;  // Member variables can also use template parameters
  public: 
  int fun1(T1 x, int y );
 T2 fun2(T1 x, T2 y);
}
// Class implementation part
template  <class T1, class T2 >
int A<T1>:: fun1(T1 x, int y ){// realization …… }
 template  <class T1, class T2 >
T2 A<T1,T2>:: fun2(T1 x, T2 y) {// realization …… }
 // Use class A
 int main( ) {
 // Defining objects a, Combined use int replace T1, float replace T2
   A<int, float>  a;
   // instantiation a, call a Properties and methods of ……
}
As can be seen from the above example , Class template parameters T1,T2 Valid for both member variables and member functions of a class .
stay C++ Programming , When you want to implement a class, the algorithm of some member functions and member variables depends on the data type , Consider using class templates , And C++ Most of the data structure algorithms are implemented with class templates .

6. Properties of class templates

1)  Class templates are not real classes , It's just C++ The compiler generates a module of a concrete class .
2)  Class templates can set default template arguments .

Technology
©2019-2020 Toolsou All rights reserved,
Python realization switch method vue vue-element-admin Summary of the project Thread.getContextClassLoader And Thread.getClassLoader() difference elementui select obtain valuepython primitive -- lock Lock( Essence )2020 year 7 month 15 day Wechat applet import and include difference Centos7 lower mysql8.0 To install and change the initial password ; Conflict statement (conflicting declaration) solve keras Data generator -- Data enhancement use VS2019 “Windows Desktop applications ” Module creation Win32 window