Deep resolution of default parameters for function templates and functions in C + + _c language

Source: Internet
Author: User
Tags function definition function prototype

C + + function templates
We know that data or values can be passed by function parameters, which are unknown when the function is defined, and can only be determined when a function call occurs. This is the parameterization of the data.

In fact, data types can also be passed by parameters, in the function definition can not specify the specific data type, when a function call, the compiler can automatically determine the data type based on the parameters passed in. This is the data type parameterization.

The so-called function template, in effect, is to establish a common function, whose return value type and parameter type are not specified, instead of a virtual type (in fact, a placeholder with an identifier). This generic function is called a function template (the functions Template). All functions of the same function can be replaced with this template, do not have to define more than one function, only in the template defined once. When the function is called, the system replaces the virtual type in the template with the type of the argument, thus enabling the functions of the different functions.

"Example" Changes the previous section code to be implemented by a function template.

#include <iostream>
using namespace std;
Template<typename t>//template declaration where T is the type parameter
T max (t A, T B, t C)//defines a generic function, using T as the virtual type name
{
  if (b>a) a=b;
  if (c>a) a=c;
  return A;
}
int main ()
{
  //three integer max value
  int i1, i2, i3, I_max;
  CIN >> I1 >> i2 >> i3;
  I_max = max (I1,I2,I3);
  cout << "i_max=" << i_max << Endl;
  To find the maximum value 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 value of three long
  G1, G2, G3, G_max;
  Cin >> G1 >> G2 >> G3;
  G_max = max (G1,G2,G3);
  cout << "g_max=" << g_max << Endl;
}

The running result is the same as the previous section.

The general form of defining a function template is:

  Template < typename t>


Common function definition common function definition
Or

  Template <class t>


Common function definition common function definition

When you create a function template, simply change the first int in the initial function defined in the example 4.5 program to T. The virtual type name T is used instead of the specific data type. When the program is compiled, the 13th call function Max (I1, I2, i3) is encountered, and the compilation system matches the function name Max to the template Max, replacing the type of the argument with the virtual type T in the function template. 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 following two lines (14,15 line) are similar.

There can be more than one type parameter, and you can determine the number as needed. Such as:

  Template <class T1, TypeName t2>


As you can see, using a function template is more convenient than a function overload, and the program is simpler. However, it should be noted that it applies only to functions with the same number of parameters and different types, and the same function body, if the number of parameters is different, you can not use the function template.

Default parameters for C + + functions
In general, when the parameter is taken from the argument from the function call, the number of arguments should be the same as the formal parameter. Sometimes the same function is invoked multiple times with the same arguments, C + + provides a simple way to give formal parameters a default value, so that the formal parameter does not have to be a value. If there is a function declaration:

  Float area (float r=6.5);


Specifies that the default value of R is 6.5, and if you confirm that R has a value of 6.5 when you call this function, you do not have to give the value of the argument, such as:

  Area (); Equivalent to area (6.5);


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

  Area (7.5); The parameter gets a value of 7.5 instead of 6.5.


This method is more flexible and can simplify programming and improve 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 part of the parameter, and the other part does not specify a default value. If there is a function for the volume of a cylinder, the parameter H represents 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); Specify a default value of 12.5 for formal parameter R only


function calls can take the following form:

  Volume (45.6); Equivalent to Volume (45.6,12.5)
  volume (34.2, 10.4);//h has a value of 10.4

The combination of the actual participating formal parameters is done from left to right. Therefore, the parameter that specifies the default value must be placed at the very right end of the formal parameter list column, otherwise an error occurs. For example:

  void F1 (float a, int b=0, int c, char d=′a′); Incorrect
  void F2 (float a, int c, int b=0, char d=′a′);


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

  The values of F2 (3.5, 5, 3,′x′)//parameters are all obtained from the
  F2 (3.5, 5, 3)//The value of the last formal parameter takes the default value ′a′
  F2 (3.5, 5)//The last two parameters are the default values, b=0,d=′a′


As you can see, when you invoke a function that has a default parameter, the number of arguments can be different from the number of formal parameters, and the argument is not given, which is worth the default value from the formal parameter. Using this feature, you can make the function more flexible. For example 4.7, find the maximum number of 2 or 3 digits. You can also use a function with a default parameter instead of overloading the function.

"Example" asks for the maximum number of 2 or 3 positive integers, implemented with a function with default parameters.

#include <iostream>
using namespace std;
int main ()
{
  int max (int A, int b, int c=0);//function declaration, parameter C has default value
  int a,b,c;
  cin>>a>>b>>c;
  cout<< "Max (a,b,c) =" <<max (a,b,c) <<endl;//output the largest of the 3 numbers
  cout<< "max (a,b) =" <<max (A, b) <<endl; Output the maximum of 2 digits return
  0;
}
int max (int a,int b,int c)//function definition
{
  if (b>a) a=b;
  if (c>a) a=c;
  return A;
}

The operating conditions are as follows:

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

There are two points to note when using a function with a default parameter:
If the definition of a function is preceded by a function call, the default value should be given in the function definition. If the definition of a function after a function call requires a function declaration before the function call, the default value must be given in the function declaration, and the default value may not be given when the function is defined (example 4.8).
A function cannot be both an overloaded function and a function with a default argument. Because when the function is called, if less to write a parameter, the system can not determine whether the use of overloaded functions or the use of default parameters of the function, there are two semantic, the system can not be executed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.