C++ function templates

C++ function templates

like class template we can also define function template that could be used to create a family of functions with different argument types. The general format of a function template is

template< class T >
returntype function_name(arguments of type T)
{
//……….
// body of function
// with type T
// whenever appropriate
//…..
}

The function template syntax is similar to that of the class template except that we are defining functions instead of classes we must use the template T as and when necessary in the function body and in its argument list

The following example declares a swap() function template that will swap two values of a given type of data

template< class T >
void swap(Y&x,T&y)
{
T temp=x;
x=y;
y=temp;
}

this essentially declare a set of overloaded function. one for each type of data. We can invoke the function swap() like any general function for example in the below we can apply the swap() function as follows

void f(int m,int n,float a,float b)
{
swap(m,n); // swap two integer values
swap(a,b); // swap two float values
//….
}

This will generate a swap() function from the function template for each set of argument types shows how a template function is defined and implemented

,

FUNCTION TEMPLATE AN EXAMPLE
#include <iostream>
using namespace std;
template <class T>
void swap(T &x, T &y)
{
T temp=x;
x=y;
y=temp;
}
void fun(int m,int n,float a,float b)
{
std::cout << "m and n before swap:"<< m << "" << n << "\n";
swap(m,n);
std::cout << "m and n after swap:"<< m <<" " << n << "\n";
std::cout << "a and b before swap:" << a << " "<< b << "\n";
swap(a,b);
std::cout << "a and b after swap:" << a << " " << b << "\n";
}
int main()
{
fun(100,200,11.22,33.44);
return 0;
}

the output:
m and n before swap:100 200
m and n after swap: 200 100
a and b before swap: 11.22 33.439999
a and b after swap: 33.439999 11.22

another function often used is sort() for sorting arrays of various types such as int and double. The following example shows a function template for bubble sort

template < class T >
bubble(T a[], int n)
{
for(int i=0;i < n-1;i++)
{
for(int j=n-1;i < j;j–)
{
if(a[j] < a[j-1])
{
T temp=v[j];
v[j]=v[j-1];
v[j-1]=temp;
}
}
}
}
note that the swapping statements
T temp=v[j];
v[j]=v[j-1];
v[j-1]=temp;
may be replaced by the statement
swap(v[j],v[j-1]);

where swap() has been defined as a function template

here is another example where a function returns a value

template < class T >
T max(T x, T y)
{
return x>y?x:y;
}

a function generated from a function template is called a template function in the below program demonstrates the use of two template functions in nested form for implementing the bubble sort algorithm. shows another example of application of template functions

BUBBLE SORT USING TEMPLATE FUNCTIONS
#include <iostream>
using namespace std;
template<class T>
void bubble(T a[], int n)
{
for (int i=0;i < n-1;i++)
{
for(int j=n-1;i < j;j--)
{
if(a[j] < a[j-1])
{
swap(a[j],a[j-1]); // calls the template function
}
}
}
}
template<class X>
void swap(X &a,X &b)
{
X temp=a;
a=b;
b=temp;
}
int main()
{
int x[5]={10,50,30,40,20};
float y[5]={1.1,5.5,3.3,4.4,2.2};
bubble(x,5); // calls template function for int values
bubble(y,5); // calls template function for float values
std::cout <<"sorted x-array";
for(int i=0;i < 5;i++)
std::cout << x[i] << " ";
std::cout << "sorted y-array";
for(int j=0;j < 5;j++)
std::cout << y[j] << " ";
std::cout << "\n";
}

output:
Sorted x-array: 10 20 30 0 50
sorted y-array: 1.1 2.2 3.3 4.4 5.5

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise