Function templates let one function body work with more than one type.

function template A function template uses a type parameter such as `T` so the compiler can build a typed version when the function is called.

Function Templates

example
function_templates.cpp
Replay: real traced execution (multi-file project)
#include <iostream>

template <typename T>
T larger(T left, T right) {
    if (left > right) {
        return left;
    }
    return right;
}

int main() {
    int first = 8;
    int second = 5;

    int best = larger(first, second);

    std::cout << "first=" << first << std::endl;
    std::cout << "second=" << second << std::endl;
    std::cout << "larger=" << best << std::endl;
    return 0;
}
#include <iostream>

template <typename T>
T larger(T left, T right) {
    if (left > right) {
        return left;
    }
    return right;
}

int main() {
    int first = 3;
    int second = 5;

    int best = larger(first, second);

    std::cout << "first=" << first << std::endl;
    std::cout << "second=" << second << std::endl;
    std::cout << "larger=" << best << std::endl;
    return 0;
}
#include <iostream>

template <typename T>
T larger(T left, T right) {
    if (left > right) {
        return left;
    }
    return right;
}

int main() {
    int first = 12;
    int second = 5;

    int best = larger(first, second);

    std::cout << "first=" << first << std::endl;
    std::cout << "second=" << second << std::endl;
    std::cout << "larger=" << best << std::endl;
    return 0;
}
#include <iostream>

template <typename T>
T larger(T left, T right) {
    if (left > right) {
        return left;
    }
    return right;
}

int main() {
    int first = 8;
    int second = 2;

    int best = larger(first, second);

    std::cout << "first=" << first << std::endl;
    std::cout << "second=" << second << std::endl;
    std::cout << "larger=" << best << std::endl;
    return 0;
}
#include <iostream>

template <typename T>
T larger(T left, T right) {
    if (left > right) {
        return left;
    }
    return right;
}

int main() {
    int first = 8;
    int second = 9;

    int best = larger(first, second);

    std::cout << "first=" << first << std::endl;
    std::cout << "second=" << second << std::endl;
    std::cout << "larger=" << best << std::endl;
    return 0;
}
  1. first ← 8, second ← 5, best ← 8

    11int main() {12    int first→ 8 = 8; //@first=3, 1213    int second→ 5 = 5; //@second=9, 21415    int best→ 8 = larger(first8, second5);1617    std::cout << "first=" << first8 << std::endl;18    std::cout << "second=" << second5 << std::endl;19    std::cout << "larger=" << best8 << std::endl;20    return 0;21}
    outputfirst=8
    second=5
    larger=8
  1. first ← 3, second ← 5, best ← 5

    11int main() {12    int first→ 3 = 3;13    int second→ 5 = 5;1415    int best→ 5 = larger(first3, second5);1617    std::cout << "first=" << first3 << std::endl;18    std::cout << "second=" << second5 << std::endl;19    std::cout << "larger=" << best5 << std::endl;20    return 0;21}
    outputfirst=3
    second=5
    larger=5
  1. first ← 12, second ← 5, best ← 12

    11int main() {12    int first→ 12 = 12;13    int second→ 5 = 5;1415    int best→ 12 = larger(first12, second5);1617    std::cout << "first=" << first12 << std::endl;18    std::cout << "second=" << second5 << std::endl;19    std::cout << "larger=" << best12 << std::endl;20    return 0;21}
    outputfirst=12
    second=5
    larger=12
  1. first ← 8, second ← 2, best ← 8

    11int main() {12    int first→ 8 = 8;13    int second→ 2 = 2;1415    int best→ 8 = larger(first8, second2);1617    std::cout << "first=" << first8 << std::endl;18    std::cout << "second=" << second2 << std::endl;19    std::cout << "larger=" << best8 << std::endl;20    return 0;21}
    outputfirst=8
    second=2
    larger=8
  1. first ← 8, second ← 9, best ← 9

    11int main() {12    int first→ 8 = 8;13    int second→ 9 = 9;1415    int best→ 9 = larger(first8, second9);1617    std::cout << "first=" << first8 << std::endl;18    std::cout << "second=" << second9 << std::endl;19    std::cout << "larger=" << best9 << std::endl;20    return 0;21}
    outputfirst=8
    second=9
    larger=9