+ Reply to Thread
Results 1 to 5 of 5

Thread: using Template function

  1. #1
    Chinmoy's Avatar
    Chinmoy is offline Programming Expert
    Join Date
    Feb 2008
    Location
    where heaven meets earth
    Posts
    410
    Rep Power
    18

    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 Attached Thumbnails using Template function-temp.jpg  
    Attached Files Attached Files
    Last edited by Chinmoy; 03-26-2008 at 12:06 AM.
    God is real... unless declared an integer

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: using Template function

    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.

  4. #3
    Chinmoy's Avatar
    Chinmoy is offline Programming Expert
    Join Date
    Feb 2008
    Location
    where heaven meets earth
    Posts
    410
    Rep Power
    18

    Re: using Template function

    Yes, that should work pretty well! Is there any error message?
    God is real... unless declared an integer

  5. #4
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: using Template function

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

  6. #5
    Chinmoy's Avatar
    Chinmoy is offline Programming Expert
    Join Date
    Feb 2008
    Location
    where heaven meets earth
    Posts
    410
    Rep Power
    18

    Re: using Template function

    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.
    God is real... unless declared an integer

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 4
    Last Post: 10-27-2011, 08:10 PM
  2. function template
    By wilk_u in forum C and C++
    Replies: 1
    Last Post: 04-20-2011, 05:14 PM
  3. Replies: 4
    Last Post: 04-18-2011, 07:13 PM
  4. Replies: 1
    Last Post: 03-20-2011, 06:16 AM
  5. Replies: 3
    Last Post: 11-12-2010, 05:47 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts