In computer science, generics are a technique that allows one value to take different datatypes (so-called polymorphism) as long as certain contracts such as subtypes and signature are kept. The programming style emphasizing use of this technique is called generic programming.
For example, if one wanted to create a list using generics, a possible declaration would be to say List<T>, where T represented the type. When instantiated, one could create List<Integer> or List<Animal>. The list then, is treated as a list of whichever type is specified.
Among object-oriented languages, C++, D, BETA, Eiffel, Ada, and later versions of Java, VB.NET and C# provide generic facilities.
It was however templates of C++ that popularized the notion of generics. Templates allow code to be written without consideration of the data type with which it will eventually be used.
Dynamic typing, such featured in Objective-C, and, if necessary, judicious use of protocols circumvent the need for use of generic programming techniques, since there exists a general type to contain any object. Whilst Java does so also, the casting that needs to be done breaks the discipline of static typing, and generics are one way of achieving the benefits of dynamic typing with the advantages of having static typing.
Templates
Templates are of great utility to programmers in C++, especially when combined with multiple inheritance and operator overloading. The C++ Standard Template Library (STL) provides many useful functions within a framework of connected templates.
As the templates in C++ are very expressive they may be used for things other than generic programming. One such use is called template metaprogramming, which is a way of pre-evaluting some of the code at compile-time rather than run-time. Further discussion here only relates to templates as a method of generic programming.
Technical overview
There are two kinds of templates. A function template behaves like a function that can accept arguments of many different types. For example, the C++ Standard Template Library contains the function template max(x, y)
which returns either x or y, whichever is larger. max()
could be defined like this:
template <typename T> T max(T x, T y) { if (x < y) return y; else return x; }
This template can be called just like a function:
cout << max(3, 7); // outputs 7
The compiler determines by examining the arguments that this is a call to max(int, int)
and instantiates a version of the function where the type T
is int
.
This works whether the arguments x
and y
are integers, strings, or any other type for which it makes sense to say "x < y
". If you have defined your own data type, you can use operator overloading to define the meaning of <
for your type, thus allowing you to use the max()
function. While this may seem a minor benefit in this isolated example, in the context of a comprehensive library like the STL it allows the programmer to get extensive functionality for a new data type, just by defining a few operators for it. Merely defining <
allows a type to be used with the standard sort()
, stable_sort()
, and binary_search()
algorithms; data structures such as set
s, heaps, and associative arrays; and more.
As a counterexample, the standard type complex
does not define the <
operator, because there is no strict order on complex numbers. Therefore max(x, y)
will fail with a compile error if x and y are complex
values. Likewise, other templates that rely on <
cannot be applied to complex
data. Unfortunately, compilers historically generate somewhat esoteric and unhelpful error messages for this sort of error. Ensuring that a certain object adheres to a method protocol can alleviate this issue.
A class template extends the same concept to classes. Class templates are often used to make generic containers. For example, the STL has a linked list container. To make a linked list of integers, one writes list<int>
. A list of strings is denoted list<string>
. A list
has a set of standard functions associated with it, which work no matter what you put between the brackets.
Advantages and disadvantages
Some uses of templates, such as the max()
function, were previously filled by function-like preprocessor macros. For example, here is a max()
macro:
#define max(a,b) ((a) < (b) ? (b) : (a))
Both macros and templates are expanded at compile time. Macros are always expanded inline; templates can also be expanded as inline functions when the compiler deems it appropriate. Thus both function-like macros and function templates have no run-time overhead.
However, templates are generally considered an improvement over macros for these purposes. Templates are type-safe. Templates avoid some of the common errors found in code that makes heavy use of function-like macros. Perhaps most importantly, templates were designed to be applicable to much larger problems than macros. The definition of a function-like macro must fit on a single logical line of code.
There are three primary drawbacks to the use of templates. First, many compilers historically have very poor support for templates, so the use of templates can make code somewhat less portable. Second, almost all compilers produce confusing, unhelpful error messages when errors are detected in template code. This can make templates difficult to develop. Third, each use of a template may cause the compiler to generate extra code (an instantiation of the template), so the indiscriminate use of templates can lead to code bloat, resulting in excessively large executables.
Generic programming features in other languages
Templates were left out of some C++-based languages, such as Java and C#, largely due to the problems with templates in C++. These languages have adopted other methods of dealing with the same problems. However, both Java and C# are currently adopting generic programming features comparable to templates.
D supports template programming as advanced as C++'s, and in some ways even more powerful.
Ada's generics predate templates.
External references and links
- David Vandevoorde, C++ Templates: The Complete Guide, 2003 Addison-Wesley. ISBN 0-201-73484-2.
- Introducing Generics in the Microsoft CLR (MSDN Magazine)
- More on Generics in the Microsoft CLR (MSDN Magazine)
See also Partial evaluation