MC logo
[^] CSc 220 Home
[Introduction] [Boolean and Control] [Functions] [Arrays and Pointers] [Dynamic Arrays] [Array Errors] [Command Array] [Standard Containers] [Structs and Classes] [Automatic Pointer] [Multi-File Programs] [Copying Objects] [Templates] [Inheritance] [Plain C I/O] [Type Odds And Ends] [Plain C Strings] [File I/O]
[Template Functions] [Template Generalized Stack] [Template Generalized Stack Driver] [Array Template Stack] [Array Template Test Driver] [Generalized Safe Array] [Safe Array Demo] [Vector Iterator Demo]
Readings: Ch 24, 25

Templates allow the creation of functions and classes with one or more types to be specified later. For instance, you can create a stack of items of type T, then instantiate it as a stack of integer, or strings, or whatever you want, as often as you want.

Java generics were taken from C++ templates, and use a similar syntax. (With, as usual, several differences.) They are also less general, since Java's type parameters may represent only non-primitive types, while C++'s may be of any type.

After templates were added to C++, a large number of data structure template classes to the standard libraries. These were derived from something called the Standard Template Library developed at Hewlett-Packard. This has freed a lot of C++ programmers from needing to reinvent the tree.