Introduction to C + + Classic-Example 9.1-function templates, function templates, using arrays as template parameters

Source: Internet
Author: User

1: The function template is not a real function, so the compiler cannot generate executable code for it. Defining a function template is just a description of the function's functional framework, which, when executed, determines its functionality based on the actual parameters passed.

2: The general form of the function template definition is as follows:

template < type form parameters > return type function name (formal parameter table)

{

...//function implementation

}

Where template is the keyword, which means defining a template, the angle brackets "<>" are template parameters, there are two main parameters, one is the template type parameter, the other is the template non-type parameter. The template defined in the preceding code uses the template type parameter, and the template type parameter starts with the keyword class or typedef, followed by a user-defined legal identifier. The template non-type parameter is the same as the normal parameter definition, usually a constant.

You can divide the declaration function template into the section of template and function name. For example:

Template <class t>

void Fun (T t)

{

...//function implementation

}

For example, the following code defines a summed function template:

Template <class type>//defining a profile type

Type Sum (Type Xvar,type yvar)

{

return Xvar+yvar;

}

After you have defined the function template, you need to call the function template in the program. The following code shows the invocation of the SUM function template.

int iret=sum (10,20);//Implement the addition of two integers

Double Dret=sum (10,20.5);//Implement the addition of two real numbers

If you call the SUM function template in the following form, an error will occur.

int iret=sum (10.5,20);//Wrong call

Double Dret=sum (10,20.5);//Wrong call

In the code above, two different types of parameters are passed for the function template. Causes the compiler to create ambiguity. If the function template is called when the presentation template type is displayed, no error occurs. For example:

int iret=sum<int> (10.5,20);//correct call to function template

Double dret=sum<double> (10,20.5);//correct call to function template

Using a function template to generate an actual executable function is also called a template function. function templates and template functions are not a concept. Essentially, a function template is a "framework", which is not a program that can actually compile generated code, whereas a template function is a function that is generated after instantiating a type parameter in a function template, which is essentially the same as a normal function and can generate executable code.

3: Role of function templates

Getting Started with C + + Classic-Example 9.1-function templates, function templates, using arrays as template parameters

Related Article

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.