In many ways, templates work like preprocessor macros, replacing the templated variable with the given type. However, there are many differences between a macro like this:
#define min(i, j) (((i) < (j)) ? (i) : (j))
and a template:
template<class T> T min (T i, T j) { return ((i < j) ? i : j) }
Here are some problems with the macro:
i
and j
parameters are evaluated twice. For example, if either parameter has a postincremented variable, the increment is performed two times.