Function templates and template Functions

Source: Internet
Author: User

1.1 function template declaration
Function templates can be used to create a common function to support a variety of different form parameters, avoiding repeated design of function bodies for heavy-duty functions. The data type used by the function is used as a parameter.
The Declaration Form of the function template is:
Template <typename data type parameter identifier>
<Return type> <Function Name> (parameter table)
{
Function body
}
Template is the keyword that defines the template function, the angle brackets behind the template cannot be omitted, and typename (or class) is the keyword that declares the data type parameter identifier, it indicates that the identifier following it is a data type identifier. In this way, any variable that you want to determine the Data Type Based on the Data Type of the real parameter can be described using the Data Type parameter identifier in the function that will be defined later, so that this variable can adapt to different data types. For example:
Template <typename T>
T FUC (t x, int y)
{
T x;
//......
}
If the main function has the following statements:
Double D;
Int;
FUC (D, );
The system will replace T in the function template with the data type double of the real parameter d to generate the function:
Double FUC (Double X, int y)
{
Double X;
//......
}
The function template only declares the description of a function, that is, the template. Instead of a function that can be directly executed, the function must replace the type parameter identifier with the Data Type of the real parameter according to the actual situation, to generate real functions.
The keyword typename can also use the keyword class. In this case, the data type parameter identifier can use all the C ++ data types.
1. 2. Generation of template Functions
The data type parameter identifier of the function template is actually a type parameter. When using the function template, You need to instantiate this parameter as a determined data type. Parameters instantiated by type parameters are called template parameters, and functions instantiated by template parameters are called template functions. The template function is generated to instantiate the type parameters of the function template. For example:
Notes:
(1) The function template allows multiple type parameters, but the keyword typename or class must exist before each form parameter in the template definition section, that is:
Template <class data type parameter identifier 1 ,..., Class data type parameter identifier n>
<Return type> <Function Name> (parameter table)
{
Function body
}
(2) No other statements are allowed between the template statement and the function template Definition Statement <return type>. The following statement is incorrect:
Template <class T>
Int I;
T min (t x, t y)
{
Function body
}
(3) The template function is similar to the overload function, but there is a big difference between the two: When the function is overloaded, each function can execute different actions, however, after a function template is instantiated, all template functions must perform the same action.

2. Function template Exception Handling
The template parameters in the function template can be instantiated into various types, but an error may occur when the actual parameters of the instantiated template parameters are inconsistent. For example:
Template <typename T>
Void min (T & x, t & Y)
{Return (x <Y )? X: Y ;}
Void func (int I, char J)
{
Min (I, I );
Min (J, J );
Min (I, j );
Min (J, I );
}
The last two calls in this example are incorrect because the compiler implicitly generates a template function based on the type of the first real parameter, use it to check the consistency of all template functions, such as statements
Min (I, j );
If the real parameter I is an integer, the compiler interprets the template parameter as an integer. The template real parameter J cannot be interpreted as an integer, causing an error. In this case, there is no implicit type conversion function. There are two ways to solve this exception:
(1) Use forced type conversion. For example, rewrite the statement min (I, j) to min (I, INT (j ));
(2) Use non-template functions to overload function templates
There are two methods:
① Borrow the function body of the function Template
In this case, only the prototype of the non-template function is declared. Its function body borrows the function body of the function template. For example, the preceding example is rewritten as follows:
Template <typename T>
Void min (T & x, t & Y)
{Return (x <Y )? X: Y ;}
Int min (INT, INT );
Void func (int I, char J)
{
Min (I, I );
Min (J, J );
Min (I, j );
Min (J, I );
}
If you execute this program, there will be no error because the overload function supports implicit type conversion between data.
② Redefine the function body
Just like a general overload function, you can redefine a complete non-template function, and its parameters can be random. In C ++, when function templates and non-template function with the same name are overloaded, the following call principles should be observed:
• Find a function with exactly matched parameters and call it if it is found. If the parameter matches more than one function, this call is an incorrect call.
• Find a function template and instantiate it to generate a matching template function and call it.
• If both of the above two fail, the function overload method is used to generate Parameter Matching through type conversion. If it is found, it is called.
• If none of the above three failed and no matching functions have been found, this call is an incorrect call.

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.