The Motivation — Genericity / Type-Parameterized Entities
Many groups of algorithms and classes have the same implementation with the sole exception of the type they operate on. For example the code used for
searching through an array:
for (int i = 0; i < n; i++)
if (searchVal == a[i] return true;
return false;
is the same regardles of whether the elements are integers, doubles, or strings. Similarly, one could define a Simple class that holds double's
that is essentially identical to our Simple class of int values, with the exception of some type declarations. Such code is quite common.
It's therefore highly desireable to be able to write code for which certain types are left unspecified and which can be filled in as needed. As another example, we'd like to be
able to write a sort without specifying the element type of the array, and then as we need it, specify in one place that we want the element type to be int,
in another that we want an array of strings be sorted and so on.
This style of coding is called generic programming and it's provided by a language feature of C++ called templates
Generic Programming
a style of programming in which alorithms are reduced to their most abstract form
involves heavy use of type paramters which are then instantiated.
Function and Class Templates
generic programming facility of C++
template parameters (usually types)
may be defined for functions (function template or classes (class template)
Templates are NOT functions or classes; they're patterns for the creation of such.
Supplying a value to a template parameter instantiates the template and creates a
corresponding function/class.
This is a compile-time operation performed at the point of instantion
Template Overview
patterns or 'cookie cutters' of classes/functions
not actual class/function
resulting class/function obtained via instantiation
class template (not template class)
function template (not template function)
syntax takes getting used to, and can get quite hairy
Function templates
For functions that essentially differ only in the type of their parms
template <typename Parm>
parameterized function definition
Class templates
For classes that essentially differ only in the type of one (or more) data members
(usually some form of element type)
template <typename Parm>
parameterized class declaraction
function implementations are similar to free-standing function templates
Where do template definitions go?
Template has to be instantiated by user of template
Template parm needs to be supplied
Templates therefore need to be seen when compiling the application
I.e., templates are a compile-time entity, not link-time
When specifying a template one or more type parameters are specified and subsequently used in the definition of the template.
A function template is not a function, it is a template; i.e., a pattern suitable for creating a set of functions that differ by types alone.
An actual function is created by 'calling' the name of the template with values of (having actual types).
It is the types of the values that determines the types in the function generated from the template
The process of creating an actual function from a twmplate (by supplying an actual type) is called instantiation.
A class template is not a class, it is a template; i.e., a pattern suitable for creating a set of classes that differ by types alone.
An actual class is created by specifying the actual type(s) to be passed to the type parameter(s).
A Beginner's Tip for Working with Templates
When I first began working with templates, I often found it a bit easier to code a corresponding clas with a simple type first and
then transform it into a template:
Code a regular class, using the simplest type (usually int) in the place of the eventual type parameter
This way you can get rid of all the usual syntax and basic semantic/type error in the absence of lengthy, confusing template errors
If you know how to use typedef, use it to introduce a type that will eventually become the template parameter. If you
don't either learn it, or keep track of all places where you use the type that will eventually be replaced by the template
parameter.
Once you coded (and possibly tested) the class, replace your simple type with a type parameter, and add the template
header.
Generics
C++
Java
Basic mechanism
Templates
Generic constraints enforced by compiler
Code bloat
Each new type instantiation
Single class
Typing I
Static (compile-time) duck typing
Compiler enforces generics, standard super/subclass rules