9.4. Overloading Template Functions

Overloading template functions is similar to overloading nontemplate functions (see “Function Overloading” on page 262). Template functions with the same name must have unique signatures (different argument types and/or a different number of arguments). Remember that a template function's return type is not part of its signature.

To demonstrate overloading template functions, let's write a version of max() that determines a maximum in a generic array and let's also use our previous version of max() in the same program.

 template <class TYPE> // maximum in generic array TYPE max(const TYPE *array, int size) { TYPE maxv = array[0]; for (int i = 1; i < size; i++) if (array[i] > maxv) maxv = array[i]; return maxv; ...

Get Navigating C++ and Object-Oriented Design now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.