+ Reply to Thread
Results 1 to 5 of 5

Thread: using Template function

  1. #1
    Programming Expert Chinmoy has a spectacular aura about Chinmoy has a spectacular aura about Chinmoy's Avatar
    Join Date
    Feb 2008
    Location
    where heaven meets earth
    Posts
    404

    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 using Template function-temp.jpg  
    Attached Files
    • File Type: c TEMP.C (364 Bytes, 23 views)
    Last edited by Chinmoy; 03-26-2008 at 03:06 AM.
    God is real... unless declared an integer

  2. #2
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,877
    Blog Entries
    25

    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.

  3. #3
    Programming Expert Chinmoy has a spectacular aura about Chinmoy has a spectacular aura about Chinmoy's Avatar
    Join Date
    Feb 2008
    Location
    where heaven meets earth
    Posts
    404

    Re: using Template function

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

  4. #4
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,877
    Blog Entries
    25

    Re: using Template function

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

  5. #5
    Programming Expert Chinmoy has a spectacular aura about Chinmoy has a spectacular aura about Chinmoy's Avatar
    Join Date
    Feb 2008
    Location
    where heaven meets earth
    Posts
    404

    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. Inline function
    By Chinmoy in forum C Tutorials
    Replies: 1
    Last Post: 04-07-2008, 04:13 PM
  2. function pointer
    By Chinmoy in forum C Tutorials
    Replies: 0
    Last Post: 03-19-2008, 01:52 AM
  3. SecurityAudit
    By vinay in forum Visual Basic Programming
    Replies: 27
    Last Post: 01-07-2008, 01:14 PM
  4. multi-pass preprocessing
    By kenna in forum C and C++
    Replies: 11
    Last Post: 08-14-2007, 11:45 AM

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