www.easyCPlusPlus.com

Easy C++

www.easyCPlusPlus.com

C++ Tutorial - Lesson 16: Function Templates

Function Templates, Page 1

by John Kopp

Support this site at no cost to you

Welcome to EasyCPlusPlus.com's free tutorial on C++ programming. This lesson covers function templates, which are an advanced topic in C++. This lesson only introduces the basics. Templates, which can be used with classes, as well as with functions, are significant because they are used through out the C++ standard library. A major portion of the C++ libraries was once called the Standard Template Library, STL. The most important reason to study templates is to better utilize the C++ libraries. Being able to write your own templates is secondary to this.

Function templates provide a way to parameterize the arguments or return types of a function. What does this mean? In the definition of the function, rather than specifying an explicit type of some of the arguments or the return value, a placeholder is used. Why would this be of benefit? Let's look at the same example that was used in the lesson on function overloading. Suppose it's needed to have a function that converts a temperature from Fahrenheit to Celsius.

Prototype (Declaration):

void ConvertFToC(float f, float &c);

Definition:
void ConvertFToC(float f, float &c)
{
    c = (f - 32.) * 5./9.;
}

Now suppose that a Convert function is also needed to convert an integer Fahrenheit temperature to Celsius in the same program, or a double Fahrenheit to a double Celsius, etc. In the last lesson, two solutions were presented. The first was to create unique functions with unique names for each combination of data types. We ruled out this technique as an effective solution because it could lead to a large number of functions doing essentially the same task with slightly different names. Keeping track of multiple function names is difficult and can lead to programming errors. The second solution was to use function overloading. Function overloading is a technique that allows the programmer to define multiple functions with the same name differing in the number and/or type of their arguments. The compiler chooses the correct function based on the arguments in the function call. This technique worked well. Function definitions were written for each case, and the sample program correctly resolved the right function to call based on the data types in the arguments of the function invocation. The one weakness in this technique was that it was necessary to write multiple function definitions when each definition differed only in the type of its parameters. Function templates provide a way to write a single function definition where the data type is a parameter.

Prototype (Declaration):
template <class Type>
    void ConvertFToC(Type f, Type &c);

Definition:
template <class Type>
    void ConvertFToC(Type f, Type &c)
{
    c = (f - 32.) * 5./9.;
}

As can be seen in this example, a function template begins with the keyword template followed by a bracketed list of parameters. This is required for both declarations and definitions. The rest of the declaration or definition is exactly as it would normally be written, except that in place of an explicit data type, such as int, a token, in this case "Type", is used instead. As also can be seen, although only a single definition is needed, function templates use complicated syntax relative to function overloading.

Previous Page       Next Page