View Single Post
  #1 (permalink)  
Old 03-26-2008, 01:59 AM
Chinmoy's Avatar   
Chinmoy Chinmoy is offline
Programming Professional
 
Join Date: Feb 2008
Location: where heaven meets earth
Posts: 301
Rep Power: 6
Chinmoy has a spectacular aura aboutChinmoy has a spectacular aura about
Default using Template function

Function templates

Introduction ::

Function templates act like general templates and are for functions. They can be used with generic types.

In C++ to create a function template we use the template parameters. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass types to a function as well. These function templates can use these parameters as if they were any other regular type.


Syntax::

The format for declaring function templates with type parameters is:

Code:
template <class identifier> function_declaration;
Code:
template <typename identifier> function_declaration;

The only difference between both prototypes is the use of either the keyword class or the keyword typename. The use of class or typename keywords is indistinct as the ultimate usage is simoilar. Preferably class should be used.


Example::

For example, to create a template function that returns the greater one of two objects we could use:
Code:
template <class gr8>

gr8 max (gr8 a, gr8 b) 
{
      return (a>b?a:b);
}
Here we have created a template function with gr8 as its template parameter. This specifies a prototype for the template gr8 which can be defined and used for future purpose. As we can see, the function template max returns the greater of two argument parameters of this type, but the function is not yet defined.


Calling templates ::

To use this function template we use the following format for the function call:

function_name <type> (parameters);

For example, to call max to compare two integer values of type int we can write:

Code:
int x,y;
max <int> (x,y);
When the compiler encounters this call to a template function, it uses the template to automatically generate a function replacing each appearance of gr8 by the type passed as the actual template parameter (int in this case) and then calls it. This process is automatically performed by the compiler and is invisible to the programmer. Thus it aslo implements data abstraction and hiding.

The complete code would be::
Code:
// function template
#include <iostream.h>

template <class Tel>
Tel max (Tel a, Tel b) 
{
    Tel result;
    result = (a>b)? a : b;
    return (result);
}

int main () 
{
    int i=5, j=6, k;
    long l=10, m=5, n;
    k=max(i,j);
    n=max(l,m);
    cout << k << endl;
    cout << n << endl;
    return 0;
}
In the example above we used the function template max() twice. The first time with arguments of type int and the second one with arguments of type long. With every call a new instance of the function is createda nd then the operation performed without any overlap.

As you can see, the type tel is used within the max() template function even to declare new objects of that type:

Code:
tel result;
Therefore, result will be an object of the same type as the parameters a and b when the function template is instantiated with a specific type.


We can also define function templates that accept more than one type parameter, simply by specifying more template parameters between the angle brackets. For example:

Code:
template <class A, class B>
tel min (A a, B b) 
{
    return (a<b?a:b);
}
In this case, our function template min() accepts two parameters of different types and returns an object of the same type as the first parameter (A) that is passed. For example, after that declaration we could call min() with:

Code:
int i,j;
long l;
i = nim(j,l);
or simply:
Code:
i = min(j,l);

even though j and l have different types, since the compiler can determine the appropriate instantiation anyway.



USE OF TEMPLATES :: Templates and project modulating::

The compiler does not treat templates as normal functions or classes. They are compiled on requirement, meaning that the code of a template function is not compiled until required.

Large projects are handled by a large number of programmers and the job is thus divided. Thus teh implementation and the interface are generally seperated and varies from programmer to programmer.

Thus templates bring about a parity in teh codes as all parts comply eith the template and are easy to link.
Attached Thumbnails
To view attachments your post count must be 1 or greater. Your post count is 0 momentarily.
Attached Files To view attachments your post count must be 1 or greater. Your post count is 0 momentarily.
__________________
God is real... unless declared an integer

Last edited by Chinmoy; 03-26-2008 at 02:06 AM.
Reply With Quote

Sponsored Links