Template Terminology



Item 45. Template Terminology

Precise use of terminology is always important in any technical field, particularly in programming, most particularly in C++ programming, and most definitely particularly in C++ template programming.

Figure illustrates the most important aspects of C++ template terminology.

Template terminology. Precise use of terminology is essential to precise communication of a template design.


Be particularly careful to distinguish between a template parameter, which is used in the declaration of a template, and template argument, which is used in the specialization of a template.

template <typename T> // T is a template parameter
class Heap { ... };
//...
Heap<double> dHeap; // double is a template argument

Also be careful to distinguish between a template-name, which is a simple identifier, and a template-id, which is a template name with an appended template argument list.

Most C++ programmers confuse the terms "instantiation" and "specialization." A specialization of a template is what you get when you supply a template with a set of template arguments. The specialization can be implicit or explicit. For example, when we write Heap<int>, we are explicitly specializing the Heap class template with an int argument. When we write print( 12.3 ), we are implicitly specializing the print function template with a double argument. A specialization of a template may or may not cause a template instantiation. For example, if there is a customized version of Heap for int available, the specialization Heap<int> will refer to that version and no instantiation will take place (see Class Template Explicit Specialization [46, 155]). However, if the primary Heap template is used, or if a partial specialization is used (see Template Partial Specialization [47, 161]), then an instantiation will take place.