Syntax
Creating templates involves the use of a new C++ keyword: template
. This keyword lets the compiler know that this class or function is intended to be used as a template, and that instances of a template parameter in a template definition should be replaced with the actual data type provided by a template instantiation:
template <typename T> template <class T>
In the preceding examples, T
is the template parameter. Anywhere that type T
is used within a template class or function, it will be replaced by the actual type. This will become clearer with some examples. T
is a very common name for a template parameter, but the name can be anything you wish.
Template Classes
An example of a very simple template class is provided here:
template<typename T> class Position { public: Position(T x, T y) { m_x = x; ...