Function Templates

In the Function Overloading lesson, you wrote one version of a function for each type. That works, but often the body is exactly the same in every version. Only the type changes. If you find a bug, you must fix it in every copy.

A function template solves this. You write the function once, with a name like T in place of the type. The compiler then makes a version for each type you use:

#include <iostream>
#include <string>
using namespace std;

template <typename T>
T largest(T a, T b) {
    if (a > b) {
        return a;
    }
    return b;
}

int main() {
    string first = "pear";
    string second = "apple";

    cout << largest(3, 9) << "\n";
    cout << largest(2.5, 1.75) << "\n";
    cout << largest(first, second) << "\n";
    return 0;
}

Output:

9
2.5
pear

template <typename T> goes on the line right above the function. It says that T stands for a type. In largest, both parameters have the type T, and so does the return value.

You do not tell largest what T is. The compiler looks at the arguments and works it out. largest(3, 9) gets two ints, so T is int. largest(2.5, 1.75) gets two doubles, so T is double. For the strings, T is string, and > compares them by their character codes, as in the Strings lesson.

The body must make sense for every type you use. Here it needs >, and ints, doubles and strings all have it.

When the types do not match

Both parameters have the same type T. So what happens with an int and a double?

#include <iostream>
using namespace std;

template <typename T>
T largest(T a, T b) {
    if (a > b) {
        return a;
    }
    return b;
}

int main() {
    cout << largest(3, 4.5) << "\n";
    return 0;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:13:20: error: no matching function for call to ‘largest(int, double)’

Further down, the compiler explains why: deduced conflicting types for parameter ‘T’ (‘int’ and ‘double’). The first argument says T is int, and the second says it is double. The compiler will not guess which one you meant.

You can tell it. Write the type in angle brackets after the function name:

#include <iostream>
using namespace std;

template <typename T>
T largest(T a, T b) {
    if (a > b) {
        return a;
    }
    return b;
}

int main() {
    cout << largest<double>(3, 4.5) << "\n";
    return 0;
}

Output:

4.5

Now T is double, and the int 3 is turned into 3.0 before the call.

T inside other types

T can be part of a bigger type. This template takes a vector<T>, so it prints a vector of any type:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

template <typename T>
void printAll(vector<T> items) {
    cout << "Items:";
    for (int i = 0; i < items.size(); i++) {
        T item = items[i];
        cout << " " << item;
    }
    cout << "\n";
}

int main() {
    vector<int> ages = {31, 7, 58};
    vector<string> pets = {"cat", "dog"};
    printAll(ages);
    printAll(pets);
    return 0;
}

Output:

Items: 31 7 58
Items: cat dog

For ages, T is int. For pets, it is string. Inside the function, T item makes a variable of that type.

In the exercise, finish a function template that returns the largest item of a vector, and use it for ints, doubles and strings.