1.1 TEMPLATES int by alllona

VIEWS: 10 PAGES: 3

									1.1     TEMPLATES

1.1.1   FUNCTION TEMPLATES
Templates are mechanisms for generalizing application code for functions and classes. A function
template is not really a function, because it does not actually cause program code to be placed in
memory. Instead it is a pattern, or blueprint, for making functions. Therefore, we distinguish a function
template (the definition or blueprint - from a template function – the instantiation, the newly created
function that is instantiated on the basis of a concept.
A function template allows you to parameterize the function parameters, variables, and return type of a
function.
By parameterizing or abstracting the data types from the body of a function or class, templates act like
macros. However, macros neither perform type checking on the arguments, nor on the return type.
A function template consists of a template tag followed by a function definition. This template tag
includes the template keyword followed by a list of one or more template parameters enclosed in angle
brackets. So the function template begins with the line

template <class Type>

This is to signal to the compiler that what follows is a template and that Type is a type parameter or
template argument. This parameter can be replaced by any type, whether the type is a class or not.
The word Type is arbitrary: <class T> or <class anyType> would suffice too.

Suppose we need a swap() function and we want it to be used with different type parameters. We then
would have to rewrite the following swap() for integers:

void swap (int& iNum1, int& iNum2)
{
 int tmp = iNum1;
 iNum1 = iNum2;
 iNum2 = tmp;
}

The function template and the prototype would look like this:

template <class Type>
void swap (Type &iNum1, Type &iNum2) ;

The meaning of this template is apparent: where the previous swap() dealt with integers, this
parametrized swap() will deal with any Type. Throughout the definition of the function, whenever a
specific data type such as int would ordinarily be written, the template argument, Type will be
substituted.

The definiton for this function template might be something like:

template <class Type>
void swap (Type &iNum1, Type &iNum2)
{
       Type tmp = iNum1;
       iNum1 = iNum2;
       iNum2 = tmp;
}

When the compiler sees a template definition it generates no code until it reaches the point in an
application where the template is used. At that time, it generates the proper code on the basis of the
template, which then becomes part of the application. The code for the new function will be created
from the template based on the parameters used in that particular call.
Therefore, the following statements will result in a template function – the instantiation – dealing with
integers:
void main()
{
       int a, b;
       a = 0;
       b = 1;
       swap(a,b);
}

If you again set up a call to the function template in the same application, but this time change the type
of the template parameter, the compiler creates a second function and inserts it into the application.
This would happen in the following case in which a similar function call is made, but now with doubles:

void main()
{
       […]
       double c,d;
       c = 1.1;
       d = 2.2;
       swap(c,d);
}

Additional calls to the function with the same type of template parameter will not produce any new
code. That is, the compiler will recognize that a function has already been created and set up a call to
the existing template function.

It is possible for function templates to have more than one parameter, as in the following function
template that returns the biggest of two values:

template<class Type1, class Type2>
Type1 max(Type1 a, Type2 b)
{
       return (a > b) ? a : b;
}

responding to this situation in main():

void main()
{
       int a;
       double c;
       a = 0;
       c = 1.1;
       cout << max(a,c);
}
1.1.2   OVERRIDING A TEMPLATE FUNCTION
The previous example is called a function template (or generic function, if you like). A specific
instantiation of a function template is called a template function. Template function instantiation occurs
when you take the function address, or when you call the function with defined (non-generic) data
types. You can override the generation of a template function for a specific type with a non-template
function :

#include <string.h>

char *max(char *x, char *y)
{
       return(strcmp(x,y)>0) ?x:y;
}

If you call the function with string arguments, it's executed in place of the automatic template function.
In this case, calling the function avoided a meaningless comparison between two pointers.Only trivial
argument conversions are performed with compiler-generated template functions.The argument
type(s) of a template function must use all of the template formal arguments. If it doesn't, there is no
way of deducing the actual values for the unused template arguments when the function is called.

								
To top