Jump to content


Happy Holidays from Codecall!



Recent Topics

Recent Status Updates

  • Photo
      25 Dec
    MaheshKumar

    JER HR Consulting LLC, originally established in 1990 as James E. Rocco Associates, Inc., was created to provide quality compensation consulting services to non-profit organizations by offering a comprehensive suite of services that support an effective Total Rewards Strategy. Our team of specialists provides expert assistance that is tailored to the individual needs and challenges of each valued client. We work closely with Human Resources Departments, Chief Executive Officers and other sen...

View All Updates

Developed by TechBiz Xccelerator
Photo
- - - - -

using Template function

form

  • Please log in to reply
4 replies to this topic

#1 Chinmoy

Chinmoy

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 365 posts

Posted 25 March 2008 - 10:59 PM

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:

template <class identifier> function_declaration;

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:

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:

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::

// 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:

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:

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:

int i,j;
long l;
i = nim(j,l);

or simply:
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

  • temp.JPG

Attached Files

  • Attached File  TEMP.C   364bytes   210 downloads

Edited by Chinmoy, 25 March 2008 - 11:06 PM.

  • 0

God is real... unless declared an integer

my blog :: http://techarraz.com/


#2 John

John

    CC Mentor

  • Moderator
  • 4,450 posts
  • Location:New York, NY

Posted 31 March 2008 - 09:42 PM

Say I want to make a comparable template, would I use template<typename Comparable> or template<class Comparable>? Since I want to use anything that is comparable including primitive data types, would template<class Comparable> work? I don't consider primitive data types a class.
  • 0

#3 Chinmoy

Chinmoy

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 365 posts

Posted 01 April 2008 - 10:08 PM

Yes, that should work pretty well! Is there any error message?
  • 0

God is real... unless declared an integer

my blog :: http://techarraz.com/


#4 John

John

    CC Mentor

  • Moderator
  • 4,450 posts
  • Location:New York, NY

Posted 01 April 2008 - 11:50 PM

I'm not actually trying to compile it - it was just a question in theory.
  • 0

#5 Chinmoy

Chinmoy

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 365 posts

Posted 03 April 2008 - 01:16 AM

Oh yes of course. You can see it in the tutorial. I have used classes all along. And why should class be aa primitive data type! It is an user defined aggregate data type.
  • 0

God is real... unless declared an integer

my blog :: http://techarraz.com/






Also tagged with one or more of these keywords: form

Powered by binpress