Templates (C++)

Independent concepts should be independently represented and should be combined only when needed. Where this principle is violated, you either bundle unrelated concepts toghether or create unnecessary dependencies. Either way, you get a less flexible set of components out of which to compose systems. Templates provide a simple way to represent a wide range of general concepts and simple ways to combine them. The resulting classes and functions can match hand-written, more-specialized code in run-time and space efficiency.

Templates is a powerful C++ software reuse feature. **Function templates** and **class templates** enable you to specify, with a single code segment, an entire range of related (overloaded) functions or an entire range of related classes. This technique is called generic programming.

We might write a single function template for an array-sort function, then have C++ generate separate function-template specializations that will sort int arrays, float arrays, string arrays, and so on. Also, we might write a single class template for a stack class, then have C++ generate separate class-template specializations, such as a stack-of-int class, a stack-of-float class, a stack-of-string class, and so on.

Most C++ compilers require the complete definition of a template to appear in the client source-code file that uses the template. For this reason and for reusability, templates are often defined in headers, which are then #included in the appropriate client source-code files. For class templates, this means that the member functions are also defined in the header.

Function Templates
Overloaded functions normally perform similar or identical operations on different types of data. If the operations are identical for each type, they can be expressed more compactly and conveniently using function templates. Initially, you write a single function-template definition.

All function-template definitions begin with keyword template followed by a list of template parameters:

Example: template printArray:

If a template is invoked with a user-defined type, and if that template uses functions or operators (e.g., ==, +, <=) with objects of that class type, then those functions and operators must be overloaded for the user-defined type. Forgetting to overload such operators causes compilation error.

Class Templates
To instantiate a stack, for example, a data type must be specified. This creates a wonderful opportunity for software reusability. We need the means for describing the notion of a stack generically and instantiating classes that are type-specific versions of this generic stack class. C++ provides this capability through class templates.

Class templates are called parameterized types, because they require one or more type parameters to specify how to customize a "generic class" template to form a class-template definition. When an additional specialization is needed, you use a concise, simple notation, and the compiler writes the source code for that specialization. One Stack class template, for example, could thus become the basis for creating many Stack classes (such as Stack of double, Stack of int, Stack of char, Stack of Employee, etc.) used in a program. For example: