Template

Source: Internet
Author: User
Tags exception handling prototype definition

Reference http://prglab.com/cms/pages/c-tutorial/advanced-concepts/templates.php

Functions template (function Templates)

A template is a common language feature, and a template is called a parameterized type (parametrized types). The use of template mechanism can significantly reduce redundant information, can greatly save program code, and further improve the reusability and maintenance of object-oriented programs

Templates allow us to generate generic functions that can accept parameters of any data type and return any type of value without the need for functional overloading of all possible data types. This realizes the function of the macro (macro) to some extent. Their prototype definition can be any of the following two types:

Template <class identifier> function_declaration;

Template <typename identifier> function_declaration;

The difference between the two prototypes defined above is the use of the keyword class or typename. They are actually completely equivalent, because both expressions have exactly the same meaning as execution.

For example, to generate a template that returns the larger of two objects, we can write this:

Template <class generictype>

GenericType Getmax (GenericType A, GenericType b) {return (A>B?A:B);}

In the first line of the statement, we have generated a generic data type template called GenericType. So in the function that follows it, GenericType becomes a valid data type that is used to define two parameters A and B and is used as the return value type of the function Getmax.

GenericType still does not represent any specific data type; When a function getmax is invoked, we can invoke it with any valid data type. This data type will be used as pattern in place of the generictype appearing in the function. The way to invoke a template with a type pattern is as follows:

function <type> (parameters);

For example, to call Getmax to compare integers of two int types can be written like this:

int x,y;

Getmax <int> (x,y);

As a result, the Getmax call is as if all generictype appear in place of int instead.

Here are 2 examples:

1 template <class Any>//class can also be replaced by TypeName, the latter is new.

void swap (any &a,any &b)

{

......

}

2//function template

#include <iostream.h>

Template <class t> t Getmax (t A, T b) {

T result;

result = (a>b)? A:B;

return (result);

}

int main () {

int i=5, j=6, K;

Long l=10, m=5, N;

K=getmax (I,J);

N=getmax (L,M);

cout << k << Endl;

cout << n << Endl;

return 0;

}

6

10

(In this example, we name the generic data type T instead of GenericType, because T is shorter, and it is one of the more generic indications of the template, although it is possible to use any valid identifier.) )

In the example above, we used two parameter types for the same function Getmax (): int and long, but only one implementation of a function, that is, we wrote a template for a function that we called with two different patterns.

As you see, in our template function Getmax (), type T can be used to declare the new object T result;

Result is an object of type T, like A and B, that is, they are all of the same type, which is the type that is written in the angle bracket <> when we call the template function.

Template <class t>//most commonly used: a class parameter.

Template <class T, class u>//two class parameters.

Template <class T, int n>//One class and an integer.

Template <class T = char>//has a default value.

Template <int tfunc (int) >//parameter is a function.

Multiple type parameters are allowed in a function template. However, it should be noted that the template must have a keyword before each type parameter of the definition section. Exception handling for function templates. Function templates have a feature, although the de type parameter T of the function template can be instantiated into various types, but each parameter with type parameter T must be instantiated to the exact same type. The template type does not have an implicit type conversion. If you do not pay attention to this, you may have an error.

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.