Reasons of Using Templates
Software developers find that many existing codes have to be re-implemented merely because they
were implemented for a particular data type. Take the function swap(...)
as an example.
Basically, it is to swap the value of two variables. In practice, however, a swap function for
int
, implemented in a strong-type programming language, cannot be re-used to correctly swap two
double
variables. The same problem appears for data structure. A linked list basically is a list of
linked item; but at the programming level, the type of items to be linked does matter.
One convenient solution is to duplicate the original code and modify the parameter type
(e.g. replace int
to double
). This works. But it takes time to make these tedious changes.
It is also easy to make mistakes. Later, if the implementation needs to be modified
(e.g. fixing a bug), then you have to modify n versions, where n is the number of "clones".
Another approach is to have a typedef
to specify the data type. The disadvantages of
this approach are: 1) you need to edit the header file for each type change. 2) it is
not possible to have one typedef
to represent two different types simultaneously.
C++ Template is a better solution for the above problems. The swap function can now be
implemented independent of a specific data type. This is same as for pair
. The implementation
is ready for any type, and modification is localized.
Standard Template Library (STL)
STL is a standard set of common data structures and algorithms written in templates. We introduce how to use vector because it is easy to understand and is sometimes more convenient to use than an array.
Reference
Stephen Prata, C++ Primer Plus, Waite Group Press.