Templates

Templates are C++'s implementation of generic programming. (Java just calls theirs “generics.”) As with classes, C++ template syntax is similar to Java's generics, but different in important ways. As best I can gather, this is more from each language following the same outside sources than either one copying the other.

In C++ templates are introduced by a template header, which gives the template parameters, and is followed by a function, class, or (rarely) data declaration to which the template parameters apply. The header basically applies to the next thing, and its effect extends to the end of that thing, with no special marker for the end of the template. The parameters have a type and a name, and the type is almost always typename, to indicate that the name will represent a type (as they always do in Java generics), but some other types may be used as well. These will be data constants.

In Java generics, paramters are always the names of a class. In C++ templates, type parameters can represent any type at all, and other kinds of parameters are allowed.

Plain C does not have templates.

Textbook: Chapter 18