Function templates

Basics of Function Templates

Function templates allow you to create functions that can operate on any data type. This is particularly useful for algorithms that are type-agnostic, like sorting or searching algorithms.

Syntax and Examples:

Here’s an example of a simple function template for adding two values:

template <typename T>
T add(T a, T b) {
    return a + b;
}

int main() {
    cout << add(5, 3);          // Output: 8
    cout << add(2.5, 3.7);      // Output: 6.2
}

In this example:

  • template <typename T> declares the template.
  • T add(T a, T b) is the template function that adds two values of type T.

Overloading Function Templates

Function templates can be overloaded, just like regular functions. This allows you to define multiple versions of a template function with different parameter lists.

Overloading Basics:

template <typename T>
T add(T a, T b) {
    return a + b;
}

template <typename T>
T add(T a, T b, T c) {
    return a + b + c;
}

int main() {
    cout << add(5, 3);          // Output: 8
    cout << add(1, 2, 3);       // Output: 6
}

In this example:

  • The first add function template takes two parameters.
  • The second add function template takes three parameters.

Template Specialization

Template specialization allows you to create a specialized version of a template for a specific type. This is useful when you need to handle particular types differently.

Syntax and Examples:

Here’s an example of template specialization for a max function:

template <typename T>
T max(T a, T b) {
    return (a > b) ? a : b;
}

// Specialization for char*
template <>
const char* max<const char*>(const char* a, const char* b) {
    return (strcmp(a, b) > 0) ? a : b;
}

int main() {
    cout << max(5, 10);                 // Output: 10
    cout << max("apple", "orange");     // Output: orange
}

In this example:

  • The generic max template compares any two values.
  • The specialized max template for const char* compares C-style strings using strcmp.

Code Example: Add 2 values

This is a simple example of a function template in C++. The function template add takes two arguments of type T and returns their sum of type T. The main function demonstrates how the add function can be used with different types of data.

The template keyword is used to declare the function as a template function. The class T or typename T is the placeholder for the type parameter T. The type parameter T is used in the function declaration and implementation to specify the type of the function arguments and return value.

Inside the function, a and b are added using the + operator, which works for different types of data such as integers, floating-point numbers, and doubles. The type of the return value is also specified as T.

In the main function, the add function is called with different types of data, including integers, floats, and doubles. The function is able to work with all these different data types because the add function is a template function.

When the function is called, the type parameter T is inferred from the data types of the function arguments. In the first call to add, T is inferred to be int, in the second call T is inferred to be float, and in the third call T is inferred to be double. This allows the add function to work with different types of data using the same code.

#include <iostream>
using namespace std;

template <class T>;
T add(T a, T b) {
    return a + b;
}

int main() {
    int a = 10, b = 5;
    float c = 6.7f, d = 3.45f;
    double e = 4.34, f = 6.75;
    cout << a << " + " << b << " = " << add(a, b) << endl;
    cout << c << " + " << d << " = " << add(c, d) << endl;
    cout << e << " + " << f << " = " << add(e, f) << endl;
    return 0;
}
Output
10 + 5 = 15
6.7 + 3.45 = 10.15
4.34 + 6.75 = 11.09