Geekpedia Programming Tutorials






Basics of function templates

This tutorial covers the basics of using function templates in C++. It shows you how to create them and explains how they work.

On Tuesday, October 26th 2004 at 11:01 AM
By Andrew Pociu (View Profile)
****-   (Rated 3.4 with 13 votes)
Contextual Ads
More C++ Resources
Advertisement

What are function templates used for?


Suppose you have a function that returns the sum of two numbers stored in two variables. The variables are passed as parameters (arguments).

The parameters are of type int, so the function would look like this:





#include "stdafx.h"

// Uncomment the below line if you don't use managed C++

//#include

// Function definition

void Add(int a, int b)

{

   System::Console::WriteLine(a + b);

}


int main()

{

   // Call to the function

   Add(5, 2);

   return 0;

}




But what if I want to add two doubles? The function accepts only int's, what can I do? Well here the function template comes into play.





#include "stdafx.h"

// Uncomment the below line if you don't use managed C++

//#include





// Function template

template <typename AnyType>


// Function definition

void Add(AnyType a, AnyType b)

{

   System::Console::WriteLine(a + b);

}


int main()

{

   // Call to the function

   Add(5.6, 3.3);

   return 0;

}




Probably you already know what this code does and how function templates work. The first thing you noticed is perhaps the following line:





// Function template

template <typename AnyType>



This line creates a - let's say -universal type called AnyType.

Older versions of C++ don't understand the template so instead of typename you have to use the keyword class, like this:





// Function template

template <class AnyType>



Moving on into the code you can see that we replaced the int parameters of the function with AnyType:





// Function definition

void Add(AnyType a, AnyType b)



Now depending on which parameters we will pass to the Add() function, AnyType will change to the type that fits the parameters.

For example if we pass ints, the AnyType keyword will be replaced with int. But if we pass doubles like we did, AnyType will become double:





// Because we passed doubles, AnyType will become double

Add(5.6, 3.3);




So now you know that the actual use of function templates is to pass diferent types of variables to the same function, using function calls, like this:





#include "stdafx.h"

// Uncomment the below line if you don't use managed C++

//#include




// Function template

template <typename AnyType>


// Function definition

void Add(AnyType a, AnyType b)

{

   System::Console::WriteLine(a + b);

}


int main()

{

   // AnyType will be int

   Add(5, 2);


   // AnyType will be double

   Add(5.6, 3.3)




   return 0;

}


Function templates improve performance?


No, using function templates you don't get any improved performance. For example in the code above the compiler will actually create two versions of the function, one with int parameters and one with double. The only gain is the programmer's time.
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by Hemant Kumar on Tuesday, May 10th 2005 at 01:02 AM

this pages is ok. as u hav not given the template use for string. and not deal with objects. only u deal with 2 variable that of same type . but i think in a tutorial you should give some thing more.

by Markandey on Monday, July 9th 2007 at 03:08 AM

Its very basic article, I was searching an implementation of template in C (not C++)


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C++ Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons