6.18. Function Templates

Overloaded functions are normally used to perform similar operations that involve different program logic on different data types. If the program logic and operations are identical for each data type, overloading may be performed more compactly and conveniently by using function templates. You write a single function template definition. Given the argument types provided in calls to this function, C++ automatically generates separate function template specializations to handle each type of call appropriately. Thus, defining a single function template essentially defines a whole family of overloaded functions.

Figure 6.24 defines a maximum function template (lines 3–17) that determines the largest of three values. All function template definitions begin with the template keyword (line 3) followed by a template parameter list to the function template enclosed in angle brackets (< and >). Every parameter in the template parameter list (often referred to as a formal type parameter) is preceded by keyword typename or keyword class (they are synonyms in this context). The formal type parameters are placeholders for fundamental types or user-defined types. These placeholders, in this case, T, are used to specify the types of the function’s parameters (line 4), to specify the function’s return type (line 4) and to declare variables within the body of the function definition (line 6). A function template is defined like any other function, but uses the formal type parameters as placeholders for actual data types.


 1   // Fig. 6.24: maximum.h
 2   // Function template maximum header.
 3   template < typename T > // or template< class T >          
 4   T maximum( T value1, T value2, T value3 )                  
 5   {                                                          
 6      T maximumValue = value1; // assume value1 is maximum    
 7                                                              
 8      // determine whether value2 is greater than maximumValue
 9      if ( value2 > maximumValue )                            
10         maximumValue = value2;                               
11                                                              
12      // determine whether value3 is greater than maximumValue
13      if ( value3 > maximumValue )                            
14         maximumValue = value3;                               
15                                                              
16      return maximumValue;                                    
17   } // end function template maximum                         


Fig. 6.24. Function template maximum header.

The function template declares a single formal type parameter T (line 3) as a placeholder for the type of the data to be tested by function maximum. The name of a type parameter must be unique in the template parameter list for a particular template definition. When the compiler detects a maximum invocation in the program source code, the type of the data passed to maximum is substituted for T throughout the template definition, and C++ creates a complete function for determining the maximum of three values of the specified data type—all three must have the same type, since we use only one type parameter in this example. Then the newly created function is compiled—templates are a means of code generation.

Figure 6.25 uses the maximum function template to determine the largest of three int values, three double values and three char values, respectively (lines 17, 27 and 37). Separate functions are created as a result of the calls in lines 17, 27 and 37—expecting three int values, three double values and three char values, respectively.


 1   // Fig. 6.25: fig06_25.cpp
 2   // Function template maximum test program.
 3   #include <iostream>
 4   #include "maximum.h" // include definition of function template maximum
 5   using namespace std;
 6
 7   int main()
 8   {
 9      // demonstrate maximum with int values
10      int int1, int2, int3;
11
12      cout << "Input three integer values: ";
13      cin >> int1 >> int2 >> int3;
14
15      // invoke int version of maximum
16      cout << "The maximum integer value is: "
17         << maximum( int1, int2, int3 );
18
19      // demonstrate maximum with double values
20      double double1, double2, double3;
21
22      cout << " Input three double values: ";
23      cin >> double1 >> double2 >> double3;
24
25      // invoke double version of maximum
26      cout << "The maximum double value is: "
27         << maximum( double1, double2, double3 );
28
29      // demonstrate maximum with char values
30      char char1, char2, char3;
31
32      cout << " Input three characters: ";
33      cin >> char1 >> char2 >> char3;
34
35      // invoke char version of maximum
36      cout << "The maximum character value is: "
37         << maximum( char1, char2, char3 ) << endl;
38   } // end main


Input three integer values: 1 2 3
The maximum integer value is: 3

Input three double values: 3.3 2.2 1.1
The maximum double value is: 3.3

Input three characters: A C B
The maximum character value is: C


Fig. 6.25. Function template maximum test program.

The function template specialization created for type int replaces each occurrence of T with int as follows:

int maximum( int value1, int value2, int value3 )
{
   int maximumValue = value1; // assume value1 is maximum
   // determine whether value2 is greater than maximumValue
   if ( value2 > maximumValue )
      maximumValue = value2;
   // determine whether value3 is greater than maximumValue
   if ( value3 > maximumValue )
      maximumValue = value3;
   return maximumValue;
} // end function template maximum

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.253.161