Function Templates in C++ programming



Function templates definition

A function template is a “generic” function that can work with different data types. The programmer writes the specifications of the function, but substitutes parameters for data types.

When the compiler encounters a call to the function, it generates code to handle the specific data type(s) used in the call.

Overloaded functions make programming convenient because only one function name must be remembered for a set of functions that perform similar operations. Each of the functions, however, must still be written individually.

For example, consider the following overloaded square functions:


   int square(int number)
    {
    return number * number;
    }
    double square(double number)
    {
    return number * number;
    }

The only differences between these two functions are the data types of their return values and their parameters. In situations like this, it is more convenient to write a function template than an overloaded function.

Function templates allow you to write a single function definition that works with many different data types, instead of having to write a separate function for each data type used.

This program demonstrates how this function template is used:


    double divide(double numerator, double denominator)
    {
    if (denominator == 0)
    throw "ERROR: Cannot divide by zero.\n";
    else
    return numerator / denominator;
    }   

The following statement causes the exception to be thrown.


    #include <iostream>
    #include <iomanip>
    using namespace std;
    // Template definition for square function
    template <class T>
    T square(T number){
    return number * number;
    }
    int main() {
    cout << setprecision(5);
    // Get an integer and compute its square
    cout << "Enter an integer: ";
    int iValue;
    cin >> iValue;
    // The compiler creates int square(int) at the first
    // occurrence of a call to square with an int argument
    cout << "The square is " << square(iValue);
    // Get a double and compute its square
    cout << "\nEnter a double: ";
    double dValue;
    cin >> dValue;
    // The compiler creates double square(double)at the first
    // occurrence of a call to square with a double argument
    cout << "The square is " << square(dValue) << endl;
    return 0;

    Program Output with Example Input Shown in Bold:
    Enter an integer: 3
    The square is 9
    Enter a double: 8.3
    The square is 68.89

Using Operators in Functions Templates

The square function template shown earlier in this section applies the operator * to its parameter. The square template will work correctly as long as the type of the parameter passed to it supports the * operator.

For example, it works for numeric types such as int, long, and double, because all these types have a multiplication operator *. In addition, the square template will work with any user-defined class type that overloads the operator *. Errors will result if square is used with types that do not support the operator *.

For example, because the string class overloads all the relational operators, it can be used with template functions that compute the minimum of an array of items.

The program below illustrates this.


    #include <iostream>
    #include <string<
    using namespace std;
    // Template for minimum of an array
    template <class T<
    T minimum(T arr[ ], int size){
    T smallest = arr[0];
    for (int k = 1; k < size; k++) {
    if (arr[k] < smallest)
    smallest = arr[k];
    }
    return smallest;
    }
    int main() {
    // The compiler creates int minimum(int [], int)
    // when you pass an array of int
    int arr1[] = {40, 20, 35};
    cout << "The minimum number is " << minimum(arr1, 3)
         << endl;
    string arr2[] = {"Zoe", "Snoopy", "Bob", "Waldorf"};
    cout << "The minimum string is " << minimum(arr2, 4)
         << endl;
    return 0;
    }

    Program Output:
    The minimum number is 20
    The minimum string is Bob 

Always remember that templates will only work with types that support the operations used by the template.

For example, a class can only be used with a template that applies the relational operators such as <, <=, and != to its type parameter if the class overloads those operators.


PROMOTIONS