Templates in C++
An Introduction for Java Programmers
|
Prof. David Bernstein
James Madison University
|
|
Computer Science Department
|
bernstdh@jmu.edu
|
Introduction
- 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
- Purpose:
- Allow us to develop generic functions
- An Example:
- 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
- Purpose:
- Similar to function templates (though their syntax can be
a little bit more confusing)
- An Example - A Generic
FixedSizeCollection
:
- Instantiating a
FixedSizeCollection
:
Organization of Class Templates/Function Templates
- 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
Writing an Exception Class - The Specification
cppexamples/vector/IndexOutOfBoundsException.h
A Complete Example (cont.)
Writing an Exception Class - The Implementation
cppexamples/vector/IndexOutOfBoundsException.cpp
A Complete Example (cont.)
A FixedSizeCollection
Template
cppexamples/fixedsizevector/FixedSizeCollection.hpp
A Complete Example (cont.)
Use
cppexamples/fixedsizevector/Driver.cpp
Friend Functions in Templates
- 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.)
- 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.)
An Example
cppexamples/templates/Table.hpp
Specializing Templates
- Base Template and Child Template:
- Base Template and Child Class:
- Base Class and Child Template:
Specializing the Templates Themselves
- 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:
- 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.)
An Example
cppexamples/templates/Row.hpp
Templates for Unit Testing
- 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.)
A Simple Home-Grown Harness/Framework
cppexamples/templates/UnitTest.hpp
There's Always More to Learn