COME ON CODE ON

A blog about programming and more programming.

C++ Templates

leave a comment »

Templates are very useful when implementing generic constructs like vectors, stacks, lists, queues which can be used with any arbitrary type. C++ templates provide a way to re-use source code as opposed to inheritance and composition which provide a way to re-use object code. Templates in C++ are of two types : function templates and class templates.

Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type.

In C++ this can be achieved using 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 also types to a function. These function templates can use these parameters as if they were any other regular type.

A classic example can be:

template <class myType>
myType GetMax (myType a, myType b)
{
       return (a>b?a:b);
}

int main ()
{
    long long a=10183273822013LL,b=10000011010021LL;
    printf("%d\n",GetMax<int>(10,12));
    printf("%lld\n",GetMax<long long>(a,b));
    return 0;
}

Other better example can be:

template< class T >
T gcd( T a, T b )
{
       return( b == 0 ? a : gcd( b, a % b ));
}

int main ()
{
    long long a=10183273823013LL,b=10000011010121LL;
    printf("%d\n",gcd<int>(10,12));
    printf("%lld\n",gcd<long long>(a,b));
    return 0;
}

Class templates can have members that use template parameters as types.For example:

template <class T>
class mypair
{
      T a, b;
      public:
             mypair (T first, T second)
             {
                  a=first; b=second;
             }
             T getmax ()
             {
                     return a>b? a : b;
             }
};

int main ()
{
    mypair<long long> p(1018333013LL,1000100121LL);
    printf("%lld\n",p.getmax());
    return 0;
}

The Standard Template Library generic algorithms have been implemented as function templates, and the containers have been implemented as class templates. Some of the basic algorithms which can be used are :

int main ()
{
    long long a=10183278223013LL,b=10000011000121LL;
    printf("Max :%lld\n",max(a,b));
    printf("Min :%lld\n",min(a,b));
    printf("a=%lld b=%lld\n",a,b);
    swap(a,b);
    printf("a=%lld b=%lld\n",a,b);
    return 0;
}

I will cover them in details in another post. Templates are very useful and a good programmer should always have this in his kitty.

-fR0D

Advertisements

Written by fR0DDY

April 16, 2009 at 5:26 PM

Posted in Programming

Tagged with , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: