- Forward


Templates in C++
An Introduction for Java Programmers


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Introduction
Back SMYC Forward
  • An Observation:
    • All of the classes we have discussed so far have been based on specific elementary data types
  • In Java:
    • All objects extend Object which makes it fairly easy to design generic (though not type-safe) classes
    • Generic classes can be made type-safe using parameterized types
  • In C++:
    • This same kind of functionality can be achieved using templates
Function Templates
Back SMYC Forward
  • Purpose:
    • Allow us to develop generic functions
  • An Example:
    • template<class AnyObject> AnyObject max(AnyObject a, AnyObject b) { if (a > b) return a; return b; }
  • Explained:
    • The function max returns an instance of AnyObject and is passed two instances of AnyObject, one named a and one named b
    • AnyObject is not an actual class, it is a "placeholder"
    • So, this function will be valid for any class or data type that implements the > operator
Class Templates
Back SMYC Forward
  • Purpose:
    • Similar to function templates (though their syntax can be a little bit more confusing)
  • An Example - A Generic FixedSizeCollection:
    • template<class AnyObject> class FixedSizeCollection { public: . . private: AnyObject data[BLOCK_SIZE]; . . };
  • Instantiating a FixedSizeCollection:
    • FixedSizeCollection<int> v;
Organization of Class Templates/Function Templates
Back SMYC Forward
  • Some Mechanics:
    • The code is not compiled until an instance with specific arguments is required
  • An Implication:
    • A class that uses a template class/function can't be compiled without the source code for the template class/function
    • In other words, a class that uses a template class/function must #include the source code
  • Two Options for Organizing your Code:
    • Have distinct header/interface (.h) and implementation (.cpp) files but #include the implementation (.cpp) file (which will #include the .h file as before).
      • This will confuse some IDEs -- they will think that the .cpp file can be compiled and linked.
    • Don't have distinct header/interface (.h) and implementation (.cpp) files for template classes/functions -- put everything in the header file.
      • This makes it difficult to remember that the .h file contains a template so we will use .hpp files for this purpose.
A Complete Example
Back SMYC Forward

Writing an Exception Class - The Specification

cppexamples/vector/IndexOutOfBoundsException.h
 
A Complete Example (cont.)
Back SMYC Forward

Writing an Exception Class - The Implementation

cppexamples/vector/IndexOutOfBoundsException.cpp
 
A Complete Example (cont.)
Back SMYC Forward

A FixedSizeCollection Template

cppexamples/fixedsizevector/FixedSizeCollection.hpp
 
A Complete Example (cont.)
Back SMYC Forward

Use

cppexamples/fixedsizevector/Driver.cpp
 
Friend Functions in Templates
Back SMYC Forward
  • An Observation:
    • The friend function can be templated or not
  • An Implication:
    • The friend function can be 1:1 (i.e., each class has its own friend function) or 1:M (all classes share a single friend function)
Friend Functions in Templates (cont.)
Back SMYC Forward
  • Some Observations:
    • Since a friend function is not a member of the class it must have its own template if it needs one
    • Since the friend function uses a member of the template class the compiler must recognize the template class
  • The Implication:
    • In some situations you will need to include a prototype (a.k.a. a forward specification) of the template class before the specification of the friend function
Friend Functions in Templates (cont.)
Back SMYC Forward

An Example

cppexamples/templates/Table.hpp
 
Specializing Templates
Back SMYC Forward
  • Base Template and Child Template:
    • template <class BT> class Base { }; template <class T> class Child : public Base<T> { };
  • Base Template and Child Class:
    • template <class BT> class Base { }; class Child : public Base<T> { };
  • Base Class and Child Template:
    • class Base { }; template <class CT> class Child : public Base { };
Specializing the Templates Themselves
Back SMYC Forward
  • The Issue:
    • Sometimes a class fixes the value or type of a parameter in a template (e.g., a Container class that fixes the type of element in a template)
    • Sometimes a derived/child template fixes the value or type of a parameter in a base template (a derived Row class that specializes a Table base class)
  • One Example:
    • template <class T> class Container { private: T element }; template <> // Note the empty parameter list class Container<int> { private: int element; };
  • Another Example:
    • template <int ROWS, int COLUMNS> class Table { public: Table<ROWS,COLUMNS>(); }; template <int C> class Row : public Table<1,C> { public: Row<C>(); };
Specializing the Templates Themselves (cont.)
Back SMYC Forward

An Example

cppexamples/templates/Row.hpp
 
Templates for Unit Testing
Back SMYC Forward
  • Background:
    • Unit testing is very important
    • Unit testing can be very tedious
  • Some Observations:
    • A testing harness/framework is very helpfull
    • Templates dramatically simplify testing harnesses/frameworks
Templates for Unit Testing (cont.)
Back SMYC Forward

A Simple Home-Grown Harness/Framework

cppexamples/templates/UnitTest.hpp
 
There's Always More to Learn
Back -