Template (programming)

related topics
{math, number, function}
{system, computer, user}

Templates are a feature of the C++ programming language that allow functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one.

Templates are of great utility to programmers in C++, especially when combined with multiple inheritance and operator overloading. The C++ Standard Library provides many useful functions within a framework of connected templates.

Contents

Technical overview

There are two kinds of templates: function templates and class templates.

Function templates

A function template behaves like a function except that the template can have arguments of many different types (see example). In other words, a function template represents a family of functions. For example, the C++ Standard Library contains the function template max(x, y) which returns either x or y, whichever is larger. max() could be defined like this, using the following template:

#include <iostream>
 
template <typename T>
const T& max(const T& x, const T& y)
{
  if(y < x)
    return x;
  return y;
}
 
int main()
{
  // This will call max <int> (by argument deduction)
  std::cout << max(3, 7) << std::endl;
  // This will call max<double> (by argument deduction)
  std::cout << max(3.0, 7.0) << std::endl;
  // This type is ambiguous; explicitly instantiate max<double>
  std::cout << max<double>(3, 7.0) << std::endl;
  return 0;
}

In the first two cases, the template argument T is automatically deduced by the compiler to be int and double, respectively. In the third case deduction fails because the type of the parameters must in general exactly match the template arguments. This function template can be instantiated with any copy-constructible type for which the expression (y < x) is valid. For user-defined types, this implies that the less-than operator must be overloaded.

[edit] Class templates

A class template provides a specification for generating classes based on parameters. Class templates are commonly used to implement containers. A class template is instantiated by passing a given set of types to it as template arguments.[1] The C++ Standard Library contains many class templates, in particular the containers adapted from the Standard Template Library, such as vector.

[edit] Explicit template specialization

When a function or class is instantiated from a template, a specialization of that template is created by the compiler for the set of arguments used (and the specialization is referred to as being a generated specialization). However, the programmer may decide to implement a special version of a function (or class) for a given set of template arguments which is called an explicit specialization. If a class template is specialized by a subset of its parameters it is called partial template specialization. If all of the parameters are specialized it is a full specialization. Function templates cannot be partially specialized.

Explicit specialization is used when the behavior of a function or class for particular choices of the template parameters must deviate from the generic behavior: that is, from the code generated by the main template, or templates.

[edit] Advantages and disadvantages

Some uses of templates, such as the maximum() function, were previously fulfilled by function-like preprocessor macros. For example, the following is a C++ maximum() macro:

  #define maximum(a,b) ((a) < (b) ? (b) : (a))

Both macros and templates are expanded at compile-time. Macros are always expanded inline, whereas templates are only expanded inline when the compiler deems it appropriate. When expanded inline, macro functions and template functions have no extraneous run-time overhead. Template functions with many lines of code will incur run-time overhead when they are not expanded inline, but the reduction in code size may help the code to load from disk more quickly and/or fit within RAM caches.

Templates are considered "type-safe", that is, they require type-checking at compile-time. Hence, the compiler can determine at compile-time whether or not the type associated with a template definition can perform all of the functions required by that template definition.

By design, templates can be utilized in very complex problem spaces, whereas macros are substantially more limited.

There are fundamental drawbacks to the use of templates:

  1. Historically, some compilers exhibited poor support for templates. So, the use of templates could decrease code portability.
  2. Many compilers lack clear instructions when they detect a template definition error. This can increase the effort of developing templates, and has prompted the development of Concepts for possible inclusion in a future C++ standard.
  3. Since the compiler generates additional code for each template type, indiscriminate use of templates can lead to code bloat, resulting in larger executables.
  4. Because a template by its nature exposes its implementation, injudicious use in large systems can lead to longer build times.
  5. It can be difficult to debug code that is developed using templates. Since the compiler replaces the templates, it becomes difficult for the debugger to locate the code at runtime.
  6. Templates of Templates (nesting) are not supported by all compilers, or might have a max nesting level.
  7. Templates are in the headers, which require a complete rebuild of all project pieces when changes are made.
  8. No information hiding. All code is exposed in the header file. No one library can solely contain the code.
  9. Virtual functions can cause excessive execution time bloat, and consume hundreds of kilobytes during run time space. (V Function Table).

Additionally, the use of the "less-than" and "greater-than" signs as delimiters is problematic for tools (such as text editors) which analyse source code syntactically. It is difficult, or maybe impossible, for such tools to determine whether a use of these tokens is as comparison operators or template delimiters. For example, this line of code:

  foo (a < b, c > d) ;

may be a function call with two parameters, each the result of a comparison expression. Alternatively, it could be a declaration of a constructor for class foo taking one parameter, "d", whose type is the parametrised "a < b, c >".

Full article ▸

related documents
Extended Euclidean algorithm
Taylor's theorem
Cholesky decomposition
Integer
Square root
Icon (programming language)
Kernel (matrix)
Hausdorff dimension
Tail recursion
Metric space
Cantor's diagonal argument
Equivalence relation
Dirac delta function
Set (mathematics)
Standard ML
Supremum
Vigenère cipher
Operator
Complete metric space
Interpolation
Exponential function
Monoid
L'Hôpital's rule
Insertion sort
PL/SQL
Semidirect product
Riemannian manifold
Communication complexity
Category theory
Abstraction (computer science)