In depth analysis of function templates and function default parameters in C++

  • 2020-05-05 11:32:09
  • OfStack

C++ function template
We know that data or values can be passed through function arguments, that they are not known at the time of function definition, and that their values can only be determined when a function is called. This is the parameterization of the data.

In fact, the data type can also be passed through parameters, in the function definition is can not specify the specific data type, when the function calls, the compiler can automatically determine the data type according to the parameters passed in. This is called data type parameterization.

A function template is actually a generic function that does not specify the return value type and parameter type, but instead USES a dummy type (actually an identifier to hold the place). This generic function is called the function template (Function Template). All functions with the same body can be replaced by this template. Instead of defining 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 with the type of an argument, thus enabling different functions.

Change the previous section of code to use the function template instead.


#include <iostream>
using namespace std;
template<typename T> // Template declaration, where T Is type parameter 
T max(T a, T b, T c) // Define a generic function with T Make a virtual type name 
{
  if(b>a) a=b;
  if(c>a) a=c;
  return a;
}
int main( )
{
  // Find the maximum of three integers 
  int i1, i2, i3, i_max;
  cin >> i1 >> i2 >> i3;
  i_max = max(i1,i2,i3);
  cout << "i_max=" << i_max << endl;
  // Find the maximum of three floating point Numbers 
  double d1, d2, d3, d_max;
  cin >> d1 >> d2 >> d3;
  d_max = max(d1,d2,d3);
  cout << "d_max=" << d_max << endl;
  // Find the maximum of three long integers 
  long g1, g2, g3, g_max;
  cin >> g1 >> g2 >> g3;
  g_max = max(g1,g2,g3);
  cout << "g_max=" << g_max << endl;
}

The result is the same as in the previous section.

The general form of defining a function template is


  template < typename T>


      general function definition   general function definition
Or


  template <class T>


      general function definition   general function definition

When creating a function template, simply change the int at the beginning of the first function defined in the example 4.5 program to T. Replace the concrete data type with the virtual type name T. When the program is compiled, the function max(i1, i2, i3) is called on line 13. The compilation system matches the function name max with the template max, and replaces the virtual type T in the function template with the type of the argument. This is equivalent to having a function defined:


int max(int a, int b, int c)
{
  if(b>a) a=b;
  if(c>a) a=c;
  return a;
}


Then call it. The last two lines (14,15) are similar.

Type parameters can be more than one, you can determine the number of required. Such as:


  template <class T1, typename T2>


As you can see, using a function template is more convenient and simpler than overloading functions. However, it should be noted that it only applies to the case that the number of parameters of the function is the same but the type is different, and the function body is the same. If the number of parameters is different, the function template cannot be used.

The default parameter of the C++ function is
Normally, when a function is called, the arguments get their value from the arguments, so the number of arguments should be the same as the arguments. Sometimes the same arguments are used for multiple calls to the same function. C++ provides an easy way to do this by giving the parameter a default value so that it doesn't have to be taken from the arguments. If there is a function declaration:


  float area(float r=6.5);


Specify that r has a default value of 6.5, and if you confirm that r has a value of 6.5 when you call this function, you don't have to give a value for an argument, such as


  area( ); // The equivalent of area(6.5);


If you do not want the parameter to take this default value, the argument is given separately. Such as:


  area(7.5); // The value of the parameter is 7.5 Rather than 6.5


This method is more flexible, can simplify the programming, improve the operation efficiency.

If you have more than one parameter, you can have a default value for each parameter, or you can specify a default value for only one parameter and no default value for another parameter. If there is a function to find the volume of the cylinder, h is the height of the cylinder, and r is the radius of the cylinder. The function prototype is as follows:


  float volume(float h, float r=12.5); // Only the parameter r Specify default values 12.5


Function calls can take the following form:


  volume(45.6); // The equivalent of volume(45.6 . 12.5)
  volume(34.2, 10.4); //h The value of 34.2 . r The value of 10.4

The combination of real parameters is from left to right. Therefore, the parameter that specifies the default value must be placed at the rightmost end of the parameter table column, otherwise an error occurs. For example:


  template < typename T>
0


If you call the f2 function above, you can take the following form:


  template < typename T>
1


As you can see, when a function with default arguments is called, the number of arguments can be different from the number of arguments, from the default value to the value of arguments that are not given. Taking advantage of this feature makes the use of functions more flexible. For example, example 4.7 find the largest number of two or three Numbers. You can also use a function with default parameters instead of an overloaded function.

Find the largest number of two or three positive integers, using a function with default parameters.


  template < typename T>
2

Here's how it works :


14 -56 135 � 
max(a,b,c)=135
max(a,b)=14

There are two things to note when using functions with default parameters:
If the function is defined before the function is called, the default value should be given in the function definition. If the function is defined after the function is called, then a function declaration is required before the function is called. The default value must be given in the function declaration, and the default value may not be given when the function is defined (for example, 4.8).
A function cannot be both an overloaded function and a function with default arguments. Because if you write one parameter less when calling a function, the system cannot decide whether to use the overloaded function or the function that USES the default parameter, the ambiguity occurs and the system cannot execute.


Related articles: