CodeToLive

Templates in C++

Templates in C++ allow you to write generic and reusable code. They are used to create functions and classes that work with any data type. Templates are a cornerstone of modern C++ programming, enabling powerful abstractions and compile-time optimizations.

Function Templates

Function templates enable you to define a function that can operate on any data type. The compiler generates the appropriate function for the data type used.


#include <iostream>

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

int main() {
  std::cout << "Sum of integers: " << add(3, 4) << std::endl;
  std::cout << "Sum of doubles: " << add(3.5, 4.5) << std::endl;
  return 0;
}
      

Class Templates

Class templates allow you to define a class that can work with any data type. This is particularly useful for creating generic data structures like containers.


#include <iostream>

template <typename T>
class Box {
private:
  T value;
public:
  void setValue(T v) {
    value = v;
  }
  T getValue() {
    return value;
  }
};

int main() {
  Box<int> intBox;
  intBox.setValue(123);
  std::cout << "Integer value: " << intBox.getValue() << std::endl;

  Box<std::string> stringBox;
  stringBox.setValue("Hello, Templates!");
  std::cout << "String value: " << stringBox.getValue() << std::endl;
  return 0;
}
      

Template Specialization

Template specialization allows you to define a specific implementation of a template for a particular data type. This is useful when you need to handle certain types differently.


#include <iostream>

template <typename T>
void print(T value) {
  std::cout << "Generic template: " << value << std::endl;
}

template <>
void print<int>(int value) {
  std::cout << "Specialized template for int: " << value << std::endl;
}

int main() {
  print("Hello");
  print(123);
  return 0;
}
      

Variadic Templates

Variadic templates allow you to define functions or classes that accept a variable number of arguments. This is useful for creating flexible and reusable code.


#include <iostream>

template <typename T>
void print(T value) {
  std::cout << value << std::endl;
}

template <typename T, typename... Args>
void print(T first, Args... args) {
  std::cout << first << " ";
  print(args...);
}

int main() {
  print(1, 2.5, "Hello", 'A');
  return 0;
}
      

Template Metaprogramming

Template metaprogramming is a technique where templates are used to perform computations at compile time. This can lead to highly optimized code.


#include <iostream>

template <int N>
struct Factorial {
  static const int value = N * Factorial<N - 1>::value;
};

template <>
struct Factorial<0> {
  static const int value = 1;
};

int main() {
  std::cout << "Factorial of 5: " << Factorial<5>::value << std::endl;
  return 0;
}
      

Practical Use Cases

Templates are widely used in C++ for creating generic libraries and algorithms. Some common use cases include:

Advantages of Templates

Templates offer several advantages:

Next: Standard Template Library (STL) in C++