| Templates--known in other languages as generic types ---permit you to write C++ classes which represent parameterized data types. A common application for class templates is container classes. That is, classes which implement data structures that can contain data of any type. For instance, a well-implemented binary tree is not interested in the type of data in its nodes. Templates have undergone a number of changes since their initial inclusion in the ARM. They are a particularly troublesome C++ language element in that it is difficult to implement templates well in a C++ compiler. Here is a fictitious and overly simplistic C++ class template that implements a fixed- sized stack. It provides a pair of methods for setting (and getting) the element at the bottom of the stack. It uses the modern C++ template syntax, including the new
C++ permits this class to be instantiated for any type you like, using calling code that looks something like this:
An old trick for fashioning class templates is to use the C preprocessor. Here is our limited
There is a couple of subtleties being used here that should be highlighted. This generic class declaration uses the C preprocessor operator `##' to generate a type name which is unique amongst stacks of any type. The
The syntax for instantiating a |