ArticleDirectory
// Reference http://prglab.com/cms/pages/c-tutorial/advanced-concepts/templates.php
Function Template)
Templates allow us to generate common functions that can accept parameters of any data type and return values of any type, function Overloading is not required for all possible data types. To some extent, this achieves the role of a macro. Their prototype can be either of the following two types:
Template <Class Identifier> function_declaration;
Template <typename identifier> function_declaration;
The difference between the two prototype definitions lies in the use of the keyword class or typename. They are actually exactly equivalent, because the two expressions have the same meaning and execution.
For example, to generate a template and return a larger one of the two objects, we can write as follows:
Template <class generictype>
Generictype getmax (generictype A, generictype B) {return (A> B? A: B );}
In the first line Declaration, we have generated a general data type template called generictype. Therefore, in the subsequent functions, generictype becomes a valid data type. It 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 the function getmax is called, we can use any valid data type to call it. This data type will be used as pattern to replace where generictype appears in the function. The method for calling a template with a pattern type is as follows:
Function <type> (parameters );
For example, to call getmax to compare two int-type integers, you can write as follows:
Int X, Y;
Getmax <int> (x, y );
Therefore, the call to getmax is like that all generictypes are replaced by Int.
Here is an example:
// 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 common data type T rather than the generictype, because T is shorter, and it is one of the more common labels of the template, you can use any valid identifier .)
In the above example, we use two parameter types for the same function getmax (): int and long, but only write the implementation of one function, that is to say, we have written a function template and used two different pattern to call it.
As you can see, in our template function getmax (), type T can be used to declare new objects.
T result;
Result is a T-type object, just like a and B, that is, they are all of the same type. This type is the type written in angle brackets when we call the template function <>.
Template <class T> // a class parameter.
Template <class T, Class U> // two class parameters.
Template <class T, int n> // a class and an integer.
Template <class T = char> // has a default value.
Template <int tfunc (INT)> // The parameter is a function.