C + + templates

Source: Internet
Author: User

C + + programs are types and functions that are programmed to design types and functions and then organize them into C + + program structures. Because of the similarity of things, the types and functions of design sometimes exhibit the same nature. These similar types and functions are grouped together to form a class cluster and function cluster, and programming in a uniform way is template programming. A series of similar types or similar functions can be obtained by the template, and these similar types and similar functions may have different data types, but the processing data has the same representation. These similar types and similar functions are the templates that are generated by the type of data involved, and similar types are called template classes, and similar functions are called template functions.

One • function template (functions Templates)

Examine two swap function swaps, one swap for two int, and one swap for two values of type Double, as follows:

void Swap (int& a,int& b)

{

int temp = A;

A = b;

b = temp;

}

void Swap (double& a,double& b)

{

Double temp = A;

A = b;

b = temp;

}

In fact, swapping objects of any of the two type types has the following function definition form:

void Swap (type& a,type& b)

{

Type temp = A;

A = b;

b = temp;

}

Different type types can write different swap functions, which are overloaded, or that are all functions with the same name. This series of swap overloaded functions, with changes to the type of data being processed, function entities are dispersed in the context of the program that requires the operation, like piecemeal fragments, adding to the workload.

1. Definition of function templates (Define functions template)

The function template is defined in the form

Template <tempname type parameter table >

return type function template name (data parameter table)

{

function template definition Body

}

The "type parameter table" after the template, which is enclosed in angle brackets, describes the template-form parameter (template-parameter) of the function template name. Template parameters are of type form, either basic data types or class types. Each template parameter must be prefixed with class or TypeName (Tempname is more accurate). After the template parameter described by templates is the definition body of the function template, it includes the template return in type, function template name, function template data parameter, and function template definition body. The above swap function family can be written as a function template:

Template<typename t>

void Swap (t& a,t& b)

{

T temp = A;

A = b;

b = temp;

}

Where the function template is named swap, the template parameter is T, the function template's data parameters are A and B, the function template's return type is void, and the function template defines the body as a pair of curly braces in the middle of the content.

The data parameters in parentheses after the function template name are typically used with the template parameter name T, which is followed by the template, which is an object or variable entity with a templated parameter T. In the SWAP function template, the data parameter list is made up of objects A and B with a reference type of T.

A function template is not a function, it is a template that generates a function body with concrete types as arguments. When a function template definition is compiled, no execution code is generated. A function template definition is simply a description of the body of the function that is generated in the future, indicating that it can handle the data types described in the template parameter list each time.

2. Usage of function templates (Using function Templates)

Using a function template is a function call with a function template named function name. The form is:

function template name (data argument table);

The first time a function template is used, it triggers the compiler to produce a function body definition for the corresponding function template. When the compiler discovers a call that has a function template named function name, it verifies that the corresponding data parameter list in the function template is matched against the type of the object or variable in the data argument table, and then generates a function. The definition of the function is the same as the definition of the function template, whereas the type of the data parameter table is based on the type of the data argument table. This function is called a template function. Cases:

1. #include <iostream>

2.using namespace Std;

3.template<typename t>

4.void Swap (t& a,t& b)

5.{

6. T temp = A;a = B;b = temp;

9.3

8.int Main ()

7.5

Ten. Double dx = 3.5,dy = 3.6;

One. int IX = 6,iy = 7,ia = 303,ib = 505;

string S1 = "good", s2= "better";

cout<< "Double dx=" <<dx<< ", dy=" <<dy<< "\ n";

cout<< "int ix =" << ix<< ", iy =" << iy<< "\ n";

cout<< "string S1 =\" <<s1<< "\", S2 =\ "" <<s2<< "\" \ n ";

. Swap (Dx,dy);

. Swap (Ix,iy);

Swap (S1,S2);

. Swap (IA,IB);

cout<< "\after swap:\n";

cout<< "Double dx=" <<dx<< ", dy=" <<dy<< "\ n";

cout<< "int ix=" <<ix<<x, iy= "<<iy<<" \ n ";

cout<< "String s1=\" "<<s1<<" \ ", s2=\" "<<s2<<" \ "\ n";

24.}

When the compiler sees the swap (Dx,dy) on line 16th, because it is the first function call to see the template name swap of the double argument, the generated function is named Swap<double>, which produces a function definition of the following form:

void Swap<double> (double& a,double& B)

{

Double temp= a;a = B;b = temp;

}

Since DX and dy are double type, the double type is re-deduced to get its template argument double. Therefore, the template function is named swap<double>. At the same time, data parameters dx and DY are assigned to data parameters A and B as initial values. Depending on the type of data argument----the type of the matching data parameter---confirming that the template argument----the process of pushing the template parameter is called the deduction of the data argument.

A function call with a function template named function name, with data arguments dx and dy, deduces the function template arguments, and the process of generating the template definition is called the entity or instantiation of the function template. Similarly, the swap<int> template function definition is generated when the swap (Ix,iy) call in line 17th. However, when the function call of swap (IA,IB) is seen again on line 19th, the template function definition for swap<int> is no longer generated because the Swap template function of type int already exists in the system. A template function definition is also a function definition and must conform to the one-time definition rule for C + + functions.

Obviously, a function template can generate many different template functions, such as function template Swap, which generates the template functions swap<double> and swap<int>. Therefore, a function template can be generated by a different name of the template function, template arguments are different, the resulting template function is also different, a function template reflects the function family of different functions, they are different depending on the type arguments.

Two. function template parameters (functions templae Parameters)

1. Harsh type Matching

A template function call is a type parameter match that seeks a function template, and the matching rules of type real participation type parameters are different from the data parameter type matching rules of the function. Type real participation type parameter matching rules are more demanding. Cases:

1.template<typename t>

2.void Swap (t& a,t& b)

3.{

4. T temp = A;a = B;b = temp;

5.}

6.int Add (double a,double b)

9.5

8. Return a+b;

7.3

int main ()

{

int ia = 3;

Double db = 5.0;

Char s1[] = "good", s2[]= "better";

int x = Add (IA,DB); Ok

Swap (IA,DB); NG

Swap (S1,S2); NG

}

C + + templates

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.