The usage of function template in C++ is explained in detail

  • 2020-04-02 01:50:30
  • OfStack

define

We know that function overloading can make a function name more than one, using the same or similar function defined by the same name. This simplifies the way functions are called, but the program still needs to define each function separately.

Function templates provided by C++ can simplify this process even more.

The so-called function template is actually to establish a general function, its containing element type parameter type is not specified, with a virtual type to represent, this general function is called function template.

All functions with the same body can be replaced by this template. You don't need to define multiple functions, you only need to define them once in the template. When a function is called, the system replaces the dummy type in the template according to the type of the argument, thus realizing the function's function.

The general form of defining the function template is:


template <typename T>
 General function definition 

or

template <class T>
 General function definition 

Here's a comparison of using normal functions, using function overloads, and using function templates:

This code is used to solve the problem of adding different types of data

Use multiple normal functions
============== sample code 1.1============== =========


#include<iostream>
using namespace std;
int int_add(int a,int b)                              //The function int_add is defined for adding data of type int
{
 int c;
 c=a+b;
 return c;
} 
double dou_add(double a,double b)     //The function dou_add is defined for adding double functions
{
 double c;
 c=a+b;
 return c;
}
int main()
{
 cout<<int_add(5,3)<<endl;              //Call the int_add function
 cout<<dou_add(5.35,5.5)<<endl;  //Call the dou_add function
 return 0;
}

Using constructors
=============== = sample code 1.2=============== =========

#include<iostream>
using namespace std;
int n_add(int a,int b)                              //The function n_add is defined for adding data of type int
{
 int c;
 c=a+b;
 return c;
} 
double n_add(double a,double b)    //The function n_add is defined for adding double functions
{
 double c;
 c=a+b;
 return c;
}
int main()
{
 cout<<n_add(5,3)<<endl;             //Call the n_add function
 cout<<n_add(5.35,5.5)<<endl;   //Call the n_add function
 return 0;
}

Using function templates
============== example code 1.3=================== ========

#include<iostream>
using namespace std;
template<typename T>
T n_add(T a,T b)
{
 T c;
 c=a+b;
 return c;
} 
int main()
{
 cout<<n_add(5,3)<<endl;
 cout<<n_add(5.35,5.5)<<endl;
 return 0;
}

Analysis: Firstly, we analyze the sample code 1.1, which defines two different functions int_add and dou_add respectively according to the addition of different data (int and double). When different types of data are added, we manually set the corresponding function to operate.

Compared with 1.1, sample code 1.2 is simplified in the form of function call, using the function overload technology, for all data, unified use of the function n_add operation, the compilation system will automatically call the corresponding function according to the type of data.

Compared with 1.2, sample code 1.3 is simplified in the function body. If we use the function template, we don't need to write the corresponding function one by one. We just need to construct the corresponding template, and then the system will automatically determine the type of data and replace the corresponding virtual type.

For example, when operating n_add(5.35,5.5), the system automatically determines that the data is doubl, and then replaces the T in the function template with double:

It is equivalent to constructing the function:


int n_add(int a,int b)                             
{
 int c;
 c=a+b;
 return c;
} 


Related articles: