Generic Programming with C++

Generic programming allows programmer to write & use one code for several different data types. For example this function:

template <class T>
inline T min (T a, T b)
   return (a < b ? a : b);

will work with any type that supports "<" operator.

You can create versions of this function for specific types, like for int in the following example:

inline int min<int>(int x, int y)
   return x < y ? x : y;

More advanced methods for generic programming are often called Template Metaprogramming and are related to calculations performed in compilation time.


See also