C + + template - learn more about function templates

catalogue

1. Preface  

2. Why use function templates

  3. Function template syntax

3.1 function template definition form

  4. Function template and function overloading

4.1 function templates cannot convert implicit data types

  4.2 when the function template and ordinary functions are consistent with the call, ordinary functions are preferred

  4.3 if the function template will produce a better match, use the function template

  4.3 function templates and ordinary functions together, and call rules

5. Function template calling mechanism

1. Preface  

C + + provides the concept of template programming. The so-called template is actually to establish a general function or class. The type inside the class and the formal parameter type of the function are not specified specifically, but are represented by a virtual type. This common approach is called template. Templates are the foundation of generic programming, which is writing code in a way independent of any particular type.

2. Why use function templates

To implement the comparison function for int, char and flaot, we can:

int Max(int a, int b)
{
	return a > b ? a : b;
}

char Max(char a, char b)
{
	return a > b ? a : b;
}

float Max(float a, float b)
{
	return a > b ? a : b;
}

  In fact, these three functions can be completed with one function template:

//The template keyword tells the C + + compiler that I want to start generic programming. Please don't report errors at will
//T - parameterized data type
template <typename T>
T Max(T a, T b) {
	return a > b ? a : b;
}

  When the function is used, it can be automatically deduced according to the passed parameters:

void main()
{

	int  x = 1;
	int	 y = 2;
	cout << "max(1, 2) = " << Max(x, y) << endl; //Automatic derivation of parameter types
	cout << "max(1, 2) = " << Max<int>(x, y) << endl;//Display type call

	float a = 2.0;
	float b = 3.0;

	cout << "max(2.0, 3.0) = " << Max(a, b) << endl;

	system("pause");
	return;
}

Operation screenshot:

  3. Function template syntax

  The so-called function template is actually to establish a general function. Its function type and formal parameter type are not specified, but are represented by a virtual type. This generic function is called a function template. All functions with the same function body can be replaced by this template. There is no need to define multiple functions, just define them once in the template. When calling a function, the system will replace the virtual type in the template according to the type of the argument, so as to realize the functions of different functions.

3.1 function template definition form

It consists of the following three parts:   Template description  + Function definition + function template call

  1. Template description   

template    < Type form parameter table  >  

Form of type parameter:

typename T1 ,  typename T2 , ...... , typename Tn 

Or class T1,   class   T2 , …… , class   Tn

(Note: typename and class have the same effect)

2. Function definition

  Type   Function name   (form parameter table){

}

Note: the generic parameter of the template description must appear once in the function definition

       Generic type parameters or general type parameters can be used in the function parameter table

3. Function template call

max<int>(a, b);  // Explicit type call

max(a, b);   // Automatic data type derivation

  4. Function template and function overloading

4.1 function templates cannot convert implicit data types

When common function overloading and function template exist at the same time:

template <typename T>
void Swap(T& a, T& b) {
	T t;
	t = a;
	a = b;
	b = t;
	cout << "Swap The template function was called" << endl;
}


void Swap(char &a, int &b){
	int  t;
	t = a;
	a = b;
	b = t;
	cout<<"Swap Ordinary functions are called"<<endl;
}

    Execution: char cNum = 'c'; int iNum = 65;
               Swap(cNum, iNum);

    Operation screenshot:

  Screenshot of calling when there is only function template:

 

 

 

The error prompt is: there is no function template "Swap" instance matching the parameter list. The parameter type is (char, int)

  Thus:

In the first case, the template function and ordinary function coexist, and the parameter type matches the ordinary overloaded function better, so the ordinary function is called.

In the second case, there is no ordinary function, and the function template will not perform implicit data type conversion.

                        Conclusion: implicit data type conversion is not provided and must be strictly matched.

Difference between function template and ordinary function conclusion:  

Both are allowed to coexist;

Function templates do not allow automatic type conversion.

  4.2 when the function template and ordinary functions are consistent with the call, ordinary functions are preferred

  Now change the function template parameters and overloaded functions:

int Max(int a, int b)
{
	cout << "call int Max(int a, int b)" << endl;
	return a > b ? a : b;
}

template<typename T1, typename T2>
T1 Max(T1 a, T2 b)
{
	cout << "call T Max1(T1 a, T2 b)" << endl;
	return a > b ? a : b;
}

When both the function template and the ordinary function are consistent with the call, the ordinary function is preferred

int a = 1;
int b = 2;
cout<<"Max(a, b)"<<Max(a, b)<<endl;

  Operation screenshot:

 

  4.3 if the function template will produce a better match, use the function template

int a = 1;
int b = 2;

char c = 'a';
//If the function template will produce a better match, use the function template
Max(c, a);
Max(1.0, 2.0);
//Or explicitly use the function template and use the < > type list
Max<>(a, b);

  Operation screenshot:

 

  4.3 function templates and ordinary functions together, and call rules

1 function templates can be overloaded like ordinary functions

2 C + + compiler gives priority to ordinary functions

3 if the function template can produce a better match, select the template

4. You can restrict the compiler to match only through the template through the syntax of the empty template argument list

5. Function template calling mechanism

  Observed by disassembly language:

1. The compiler does not process the function template into any type of function

2. The compiler generates different functions from the function template through specific types

 

 


 

 

Tags: C++ Back-end

Posted on Wed, 27 Oct 2021 08:43:38 -0400 by Sphynx