Chapter 22 - Templates


Chapter Goals



Chapter 22 - Templates


22.1 Template Functions


22.1 Template Functions


22.1 Template Functions (cont.)


22.1 Template Functions (cont.)

template<typename T>
void print(ostream& out, T data[], int count)
{
   out << "[";
   for (int i = 0; i < count; i++)
   {
      if (i > 0)
         out << ",";
      out << data[i];
   }
   out << "]";
}

22.1 Template Functions (cont.)

Syntax 22.1: Template Function Definition
template<typename type_var1, ..., typename type_varn>
return_type function_name(parameters)
{
   statements
}
Purpose: Define a function that can be reused for different types of arguments

22.1 Template Functions (cont.)

Syntax 22.1: Template Function Definition (cont.)
Example:
template<typename T>
void print(ostream& out, T data[], int count)
{
   out << "[";
   for (int i = 0; i < count; i++)
   {
      out << data[i];
      if (i + 1 < count)
         out << ",";
   }
   out << "]";
}

22.1 Template Functions (cont.)


Common Error 22.1


A type argument is characterized implicitly, by its use in the function.


Common Error 22.1 (cont.)



Common Error 22.1 (cont.)


Coercion doesn't happen. You can't mix types.


22.2 Compile-Time Polymorphism


22.3 Template Classes


22.3 Template Classes (cont.)

Example: non-template class:

class Pair
{
   public:
      Pair(int a, int b);
      int get_first() const;
      int get_second() const;
   private:
      int first;
      int second;
};

22.3 Template Classes (cont.)

inline Pair::Pair(int a, int b)
{
   first = a;
   second = b;
}

inline int Pair::get_first() const
{
   return first;
}

inline int Pair::get_second() const
{
   return second;
}

22.3 Template Classes (cont.)


22.3 Template Classes (cont.)


22.3 Template Classes (cont.)

template<typename F, typename S>
inline Pair<F, S>::Pair(const F& a, const S& b)
{
   first = a;
   second = b;
}

22.3 Template Classes (cont.)

template<typename F, typename S>
inline F Pair<F, S>::get_first() const
{
   return first;
}

template<typename F, typename S>
inline S Pair<F, S>::get_second() const
{
   return second;
}

22.3 Template Classes (cont.)


22.3 Template Classes (cont.)

Syntax 22.2 Template Class Definition
template<typename type_var1, ..., typename type_varn>
class ClassName
{
   features
};
Purpose: Define a class template with a type parameter.

22.3 Template Classes (cont.)

Syntax 22.2 Template Class Definition (cont.)
Example:
template<typename F, typename S>
class Pair
{
public:
   Pair(const F& a, const S& b);
   F get_first() const;
   S get_second() const;
private:
   F first;
   S second;
};

22.3 Template Classes (cont.)

Syntax 22.3: Template Member Function Definition
template<typename type_variable>
{
   statements
}
Example:
template<typename T>
inline T Pair<T>::get_first() const
{
   return first;
}
Purpose: Supply the implementation of a member function for a class template.

22.4 Template Linked List


22.4 Template Linked List (cont.)


22.4 Template Linked List (cont.)


Quality Tip 22.2


Use typedef to Form an Alias for a Long Name


Quality Tip 22.2 (cont.)


Syntax typedef Statement
typedef declaration;
Example:
typedef pair<int, string> ElementType;
Purpose: Create an alias for a complicated type name.

22.5 Nontype Template Arguments


22.5 Nontype Template Arguments (cont.)


22.5 Nontype Template Arguments (cont.)


22.5 Nontype Template Arguments (cont.)


22.6 Setting Behavior Using Template Arguments


22.6 Setting Behavior Using Template Arguments


22.6 Setting Behavior Using Template Arguments (cont.)


22.6 Setting Behavior Using Template Arguments (cont.)

class CompareByName
{
public:
   bool operator()(const Employee& a,
      const Employee& b) const;
};

bool CompareByName::operator()(const Employee& a,
   const Employee& b) const
{
   return a.get_name() < b.get_name();
}

22.6 Setting Behavior Using Template Arguments (cont.)


22.6 Setting Behavior Using Template Arguments (cont.)


22.6 Setting Behavior Using Template Arguments (cont.)


22.6 Setting Behavior Using Template Arguments (cont.)


22.6 Setting Behavior Using Template Arguments (cont.)


22.6 Setting Behavior Using Template Arguments (cont.)


22.6 Setting Behavior Using Template Arguments (cont.)


22.7 Case Study: Matrices, Cont.


22.7 Case Study: Matrices (matrix5.h)


22.7 Case Study: Matrices (matrix5.cpp)


22.7 Case Study: Matrices (matrixtest5.cpp)


Chapter Summary


  1. A template allows a function or class to be parameterized by type
  2. The template abstracts the actions to be performed
  3. Template arguments are inferred from the values in a function invocation
  4. Template functions can have the same name as nontemplate functions. The nontemplates are searched first for a match
  5. Template classes allow the creation of containers that work with many different types of values
  6. Template classes must be explicitly instantiated in order to declare new variables
  7. Template arguments can be type names or they can be constants
  8. A typedef statement can be used to form an alias for a type name
  9. Template arguments can be used to set behavior, for example, setting the comparison algorithm for sorted containers