function templates for generic programming

Source: Internet
Author: User

"Template Introduction, why use a template?"

There is often a phenomenon in programming that the function bodies of two or more functions are identical, except that they differ in their parameter types, and they need to define different versions of different data types separately.

A better way to solve these problems is to use templates. A template is a tool for implementing the code reuse mechanism, which enables the implementation of type parameterization, which means that the type is defined as a parameter for code reuse.

"Template Category:

Templates are divided into function templates and class templates. They allow users to construct template functions and template classes, respectively.

"Function Template:

Establishes a general function whose function return type and parameter type are indeterminate, represented by a virtual type, which is called a function template.

function template declaration format:

Template <typename Type parameters >

return type function name (template parameter list)

{function body

   }

Where template is a keyword that indicates the declaration of one of the templates;

TypeName can also be used as a class function to indicate that a virtual type name is followed;

The type parameter is actually a type name;

Cases:

Template <typename t>t max (t x, t y) {return (x > Y) x:y;}


"How to use function templates

The max () function above represents a class of functions, and to use this template function you need to instantiate the T as a deterministic data type, which is called a template argument.

Function name (template argument table)

Cases:

Template <typename t>t max (t x, t y) {return (x > Y) x:y;}  int main () {int i1 = 19,i2=23;double D1 = 50.344, d2 = 4656.346;char C1 = ' k ', c2 = ' n '; cout << "The max of I1,i2 is= << Max (I1, I2) << endl;cout << "The max of D1,d2 is=" << Max (D1, D2) << Endl;cout <&L T "The max of C1,c2 is=" << Max (c1, C2) << Endl;return 0;}

Results:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7F/2F/wKioL1cWEYDjQNfHAAA1s4xQ_08858.png "title=" Qq20160419190340.png "alt=" Wkiol1cweydjqnfhaaa1s4xq_08858.png "/>


As seen from the example above, the function template provides an abstraction of a class of functions that takes the type parameter T as the virtual type of the parameter and function return value. The concrete functions generated by the instantiation of a function template are called template functions .


"A function template related to pointers

Template <typename at>at sum (at* array, int size = 0) {at total = 0;for (int i = 0; i < size; i++) {Total + = array[ I];} return total;};  int int_array[] = {One, one, one, 55.5, 44.4, 66.6, 1010};d ouble double_array[] = {11.1,22.2,33.3,  77.7, 88.8, 99.9, 100.10};int main () {int itotal = SUM (Int_array, ten);d ouble dtotal = SUM (Double_array, ten); cout << "The sum of the elements of this integer array is:" << itotal << endl;cout << "The sum of the elements of this double array is:" << dtotal << endl;return 0;}

Results:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7F/31/wKiom1cWFMjiTzuvAAA038AzZYs566.png "title=" Qq20160419190340.png "alt=" Wkiom1cwfmjitzuvaaa038azzys566.png "/>

Two template functions are generated in the program, and SUM (Int_array) and sum (Double_array) instantiate the type arguments as int and double respectively.


The same function template allows you to use multiple type parameters, but you must have a keyword typename before each type parameter in the template definition section.

Template <typename para1,typename para2>void Two_para (para1 x, Para2 y) {cout << x << ' << y < < Endl;} int main () {Two_para ("Zhang"), Two_para (123.45, 888L); return 0;}

Results:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7F/31/wKiom1cWGK_AfRcBAAAuCPgs-tY543.png "title=" Qq20160419193806.png "alt=" Wkiom1cwgk_afrcbaaaucpgs-ty543.png "/>

1. In this program, two template functions are generated, "Two_para (" Zhang ")", using template argument int,char* to instantiate the type parameters para1 and Para2, "Two_para (123.45,888l)", Instantiate the type parameters para1 and Para2, respectively, with the template argument Double,long,

2. Inserting other statements is not allowed before the template statement and function templates define statements.

3. As with general functions, function templates can also be overloaded.


Template <typename type>type min (type x, type y) {return  (x <  y)  ? x : y;} Template <typename type>type min (type x, type y, type z) {Type  t;t =  (x < y)  ? x : y;return  (t < z)  ?  t: z;} Int main () {int m = 10, n = 20, min2;double a = 10.1,  b = 20.2, c = 30.3, min3;min2 = min (m, n);min3 =  Min (a, b, c);cout <<  "Min" (" << m << ", " < < n <<  ") ="  << min2 << endl;cout <<  " Min (" << a << ", " << b << ", " <<  c <<  ") ="  << min3 << endl; return 0;} 

Results:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7F/2F/wKioL1cWHTDBDVCjAAAtTV-e4uQ476.png "title=" Qq20160419195404.png "alt=" Wkiol1cwhtdbdvcjaaattv-e4uq476.png "/>


"Function templates and non-function templates can be overloaded, the invocation order is: First look for a parameter exactly match the non-function template, if found to call it, if not to look for a function template to instantiate it, resulting in a matching template function.















This article is from "Sherry Wang" blog, please be sure to keep this source http://940814wang.blog.51cto.com/10910665/1765507

function templates for generic programming

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.