Show: Object Pascal C++
Display Preferences

Function Templates

From Appmethod Topics
Jump to: navigation, search

Go Up to Function Templates Overview Index

Consider a function max(x, y) that returns the larger of its two arguments. x and y can be of any type that has the ability to be ordered. But, since C++ is a strongly typed language, it expects the types of the parameters x and y to be declared at compile time. Without using templates, many overloaded versions of max are required, one for each data type to be supported even though the code for each version is essentially identical. Each version compares the arguments and returns the larger.

One way around this problem is to use a macro:

#define max(x,y) ((x > y) ? x : y)

However, using the #define circumvents the type-checking mechanism that makes C++ such an improvement over C. In fact, this use of macros is almost obsolete in C++. Clearly, the intent of max(x, y) is to compare compatible types. Unfortunately, using the macro allows a comparison between an int and a struct, which are incompatible.

Another problem with the macro approach is that substitution will be performed where you don't want it to be. By using a template instead, you can define a pattern for a family of related overloaded functions by letting the data type itself be a parameter:

template <class T> T max(T x, T y){
   return (x > y) ? x : y;
   };

The data type is represented by the template argument <class T>. When used in an application, the compiler generates the appropriate code for the max function according to the data type actually used in the call:

int i;
Myclass a, b;
int j = max(i,0);       // arguments are integers
Myclass m = max(a,b);   // arguments are type Myclass

Any data type (not just a class) can be used for <class T>. The compiler takes care of calling the appropriate operator>(), so you can use max with arguments of any type for which operator>() is defined.

See Also

Personal tools
In other languages