[C + +] [Language grammar] function templates and template functions

Source: Internet
Author: User

Reference: http://blog.csdn.net/beyondhaven/article/details/4204345

1. Declaration of function templates and generation of template functions

1.1 Declaration of a function template

Function templates can be used to create a common function that supports a number of different formal parameters and avoids repetitive design of function bodies that overload functions. Its greatest feature is the data type used by the function as a parameter.

The function template is declared in the following form:

Template<typename data type parameter identifiers >

< return type >< function name > (parameter table)

{

function body

}

Where template is the keyword that defines a templated function; the angle brackets behind the templates cannot be omitted; TypeName (or Class) is the keyword that declares the parameter identifier of the data type, indicating that the identifier following it is the data type identifier. Thus, in a later definition of this function, any variable that wishes to determine the data type based on the actual argument type can be described with the data type parameter identifier, so that the variable can be adapted to different data types. For example:

Template<typename t>

T fuc (t x, int y)

{

T x;

......

}

If the following statement is in the keynote function:

Double D;

int A;

FUC (D,a);

The system will use the data type of argument D as a double to replace the T generation function in the function template:

Double fuc (double x,int y)

{

Double X;

......

}

A function template simply declares a description of a function as a template, not a function that can be executed directly, but only if the data type of the argument is used instead of the type parameter identifier in order to produce a real function.

Keyword TypeName can also use the keyword class, at which point the data type parameter identifier can use all C + + data types.

1.2. Generation of template functions

The data type parameter identifier of a function template is actually a type argument that is instantiated as a determined data type when using a function template. A parameter that instantiates a type parameter is called a template argument, and a function instantiated with a template argument is called a template function. The generation of template functions is the process of instantiating the type parameters of a function template. For example:

Some issues to be aware of in use:

The ⑴ function template allows you to use multiple type parameters, but you must have the keyword TypeName or class before each 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

}

⑵ does not allow different statements between the template statement and the function template definition statement < return type >. The following declaration is incorrect:

Template<class t>

int I;

T min (t x,t y)

{

function body

}

⑶ template functions are similar to overloaded functions, but they are quite different: when you overload a function, you can perform different actions within each function body, but template functions that are instantiated by the same function template must perform the same action.

2 exception handling for function templates

Template parameters in a function template can be instantiated into various types, but errors can occur when the template arguments that instantiate the template parameters are not exactly the same, such as:

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 the example are wrong, and the reason for the error is that, at the time of invocation, the compiler implicitly generates a template function by the type of the first encountered argument, and uses it to perform a consistency check on all template functions, such as the statement

Min (i, j);

The first encountered argument I is an integer, and the compiler interprets the template parameter as an integral type, and then the template argument J that appears cannot be interpreted as an integer and produces an error, and there is no implicit type conversion capability. There are two ways to resolve this type of exception:

The ⑴ takes a forced type conversion, such as the statement min (i, j), and the Rewrite to Min (I,int (j));

⑵ using non-template functions to overload function templates

There are two ways of doing this:

The function body of ① borrowing function template

At this point, only the prototype of the non-template function is declared, and its function body borrows the function body of the function template. such as rewriting the above example is 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);

}

Executing the program is not an error, because overloaded functions support implicit type conversions between data.

② Redefining function Body

Just like the general overloaded function, redefine a complete non-template function, with parameters that are arbitrary. In C + +, when a function template is overloaded with a non-template function with the same name, the following invocation principles should be followed:

• Look for a function that exactly matches the parameter and call it if it is found. If the parameter matches more than one function, the call is an incorrect one.

• Look for a function template and, if found, instantiate it to generate a matching template function and invoke it.

• If all two of the above fail, use the function overloading method to generate the parameter match through the type conversion and call it if found.

• If none of the above three fails, and no matching function has been found, the call is an incorrect one.

[C + +] [Language grammar] function templates and template functions

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.