Only select operations benefit by being implemented as template functions. Similarly, only select classes benefit from a template implementation. Data structures, called containers in C++ programs, are the prime example. "A data structure is a specialized format for organizing, processing, retrieving and storing data. There are several basic and advanced types of data structures, all designed to arrange data to suit a specific purpose." Initially, C++ formed containers with multiple inheritance, inheriting "knowledge" of the container from one superclass and "knowledge" of the data from a second superclass. Today, it implements containers as classes with one or more template variables specifying the contained data's type.
Some data structures or containers are simple enough for compilers to implement them directly in computer programming languages. For example, most languages recognize arrays, data files, structures, and classes without accessing an external library. Other data structures, for example, stacks, lists, trees, and hash tables, are too complicated to be language primitives. Nevertheless, these complex data structures are too useful to ignore, so programming languages typically provide them as classes organized into language libraries. Although the list of helpful data structures is long and varied, they all utilize the same basic template syntax.
Making Template Classes
A stack is a classic data structure and a prime candidate for implementation as a template-based container. We first studied stacks in detail in the arrays chapter. Since then, we have used them in other examples, most recently as a stack class demonstrating the this pointer. Basing a stack on templates is a simple upgrade, allowing them to hold or contain any program-specified data type. The following figure illustrates the fundamental syntax and features of a template class.
Note
Historically, C++ only recognized the "class" keyword when creating templates. While the current ANSI standard allows either "class" or "typename," the original keyword often appears in data structure documentation and seems appropriate when creating containers storing objects.
#include <iostream>
#include "Person.h"
#include "stack.h"
using namespace std;
int main()
{
stack<Person> p;
Person x("Alice");
Person y("Dilbert");
Person z("Wally");
p.push(x);
p.push(y);
p.push(z);
p.pop().display();
p.pop().display();
p.pop().display();
return 0;
}
(a)
(b)
Using a template class.
When a C++ program creates an object from a template class, it must provide a concrete data type as part of the instantiation process. The provided data type replaces the template variable throughout the template class and member functions. The compiler compiles the code after it replaces the template variable with the concrete, program-supplied data type.
Creating a stack container storing integers.
Creating a stack container storing Person objects.
Constant Value Expressions
The Chapter 9 version of the stack class included a class constant, Size, specifying the maximum number of elements the container can hold. This implementation wastes space when only a small stack is needed and fails when a program calls for a large one. Chapter 6 demonstrated that programmers can specify default values for function arguments and later accept or override the defaults. Templates allow programmers the same flexibility when creating template classes. This approach requires modifying the template application code that creates the container.
Template Code
template <class T, int SIZE>
Application Code
stack<int, 10> s;
Template Code
template <class T, int SIZE = 100>
Application Code
stack<int, 10> s;
stack<int> s;
(a)
(b)
Creating and using template constants.
A template class that requires programmers to specify a data type and a stack size.
A template class with a default size. Programmers must specify a data type but may override or accept the default size.