C++ basic templates (Templates)

C++ basic templates (Templates)

Generic Programming

  Generic refers to the meaning that can be operated on multiple data types. Representative works of generic programming: STL is an efficient, generic, and interactive software component. Generic programming was originally born in C++, the purpose is to realize the STL (Standard Template Library) of C++. The language support mechanism is the template. The spirit of templates is actually very simple: parameterized types. In other words, the type information in an algorithm or class originally specific to a certain type is extracted and extracted into a template parameter T.

Function template

  In fact, it is to establish a general function, whose function type and formal parameter type are not specified specifically, but are represented by a virtual type. This general function is called a function template. When calling a function, the system will replace the virtual type in the template according to the type of the actual parameter, thus realizing the functions of different functions.

Function template syntax

Template <class or you can also use typename T>//function (class) template declaration
Return type function name (formal parameter list)//definition/implementation of function template
{
 //Function definition body
}

Template is a keyword for declaring a template. Typename is a keyword for defining formal parameters. It can be replaced by class. There is no difference between typename and class. The parameters in <> are template parameters, which are very similar to function parameters. But the template parameter cannot be empty

Function template call

myswap<float>(a, b);//Display type call myswap(a, b);//Automatic data type deduction

//template <type parameter table>   
template <typename T>
void myswap(T &a,T &b)
{
    T temp = a;
    a = b;
    b = temp;
}
int main()
{
    int x = 1;
    int y = 2;
    myswap<int>(x, y);//The way of automatic data type inference

    cout << x <<endl << y <<endl;
    char m ='a';
    char n ='b';
    myswap<char>(m,n);
    cout << m <<"-----" <<n <<endl;
    return 0;
}

Overloading of function templates

When the template class and overloaded function are used together, the overloaded function will be considered first, followed by the template class, if not, the type conversion will be considered (may be inaccurate). Function template and ordinary function together, calling rules:

  1. Function templates can be overloaded like normal functions
  2. C++ compiler gives priority to ordinary functions
  3. If the function template can produce a better match, then choose the template
  4. The syntax of the empty template argument list can be used to restrict the compiler to only match the template

Class template

Allow users to define a mode for the class, so that some data members in the class, the parameters of default member functions, and the return value of some member functions can take any type (including system predefined and user-defined). If the data type of a data member in a class cannot be determined, or the type of a member function parameter or return value cannot be determined, this class must be declared as a template. Its existence does not represent a specific and actual class. It represents a class.

Grammar of a class template

template <class type parameter name>//declare template class
class concrete type parameter name//define concrete class
{
  //...
}

Compare the size of two integers

class Compare_integer
{
public:
    Compare(int a,int b)
    {
        x = a;
        y = b;
    }
    int max()
    {
        return (x>y)?x:y;
    }
    int min()
    {
        return (x<y)?x:y;
    }
    private:
      int x,y;
};

If we need the double data type at this time, we have to create a new type implementation at this time

 class Compare_double
    {
    public:
       Compare(double a,double b)
       {
          x=a;
          y=b;
       }
       double max()
       {
          return (x>y)?x:y;
       }
       double min()
       {
          return (x<y)?x:y;
       }
    private:
       double x,y;
    };

There are two or more classes whose functions are the same, but the data types are different. For example, the statement above declares that the two classes have too many repetitive codes. Using class templates can reduce these problems. By declaring a common class template.

  template <class T>
 class Compare
 {
 public:
    Compare(T a, T b){
        x = a;
        y = b;
    }
    T max()
    {
        return (x>y)?x:y;
    }
    T min()
     {
        return(x<y)?x:y;
    }

    private:
      T x,y;
 };
int main(int argc, char *argv[])
{
     Compare<int> com(1,3);
    cout<< com.max()<<endl;
    cout<< com.min()<<endl;
    return 0;
}
Reference: https://cloud.tencent.com/developer/article/1437086 C++ Basic Templates (Templates)-Cloud + Community-Tencent Cloud