Template Functions in C++

Lesson Introduction

Welcome to the lesson on functions templates in C++! Our goal is to understand how to design functions that work flexibly with different data types, enhancing code reusability and efficiency.

Ever wondered how to write one function to handle different data types? This is where templates are useful. By the end of this lesson, you'll be adept at creating and using templates in C++.

Function Templates

Let's start with the basics: what are template functions? A template function is a blueprint for a function that can work with any data type. This enables you to write one function and use it with different types of variables.

For example, let's find the maximum of two objects. You could write separate functions for integers, floating-point numbers, and strings, but that's inefficient. Instead, a template function can handle all these cases with one piece of code.

Here it is, the findMax function template, which finds the maximum of two values:

C++
#include <iostream>
#include <string>

// A template function to find the maximum of two values
template <typename T>
T findMax(T a, T b) {
    return (a > b) ? a : b;
}

int main() {
    std::cout << "Max of 3 and 7: " << findMax(3, 7) << std::endl; // Max of 3 and 7: 7
    std::cout << "Max of 3.5 and 2.1: " << findMax(3.5, 2.1) << std::endl; // Max of 3.5 and 2.1: 3.5
    std::cout << "Max of 'apple' and 'banana': " << findMax(std::string("apple"), std::string("banana")) << std::endl; // Max of 'apple' and 'banana': banana
    return 0;
}

In this code:

  • We declare a template with template <typename T>.
  • T is a placeholder for the type provided when the function is called.
  • The findMax function compares two values of type T and returns the greater one.

When calling findMax, the compiler deduces the type T based on the arguments provided.

Example: `swapValues` Template Function

Next, let's see swapValues, a template function that swaps two variable values:

C++
#include <iostream>
#include <string>

// A template function to swap two values
template <typename T>
void swapValues(T &a, T &b) {
    T temp = a;
    a = b;
    b = temp;
}

int main() {
    int x = 10, y = 20;
    swapValues(x, y);
    std::cout << "Swapped integers: " << x << " and " << y << std::endl; // Swapped integers: 20 and 10

    double p = 5.5, q = 10.5;
    swapValues(p, q);
    std::cout << "Swapped doubles: " << p << " and " << q << std::endl; // Swapped doubles: 10.5 and 5.5

    std::string s1 = "hello", s2 = "world";
    swapValues(s1, s2);
    std::cout << "Swapped strings: " << s1 << " and " << s2 << std::endl; // Swapped strings: world and hello

    return 0;
}

In this code:

  • We define a template for swapValues with template <typename T>.
  • The swapValues function takes two references of type T and swaps their values using a temporary variable.

Just like findMax, the compiler determines the type T from the arguments.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal