6.1) Templates in C++
Templates in C++ are a powerful feature that allow you to write generic code that can work with different data types. Templates enable you to create functions and classes that can operate on a variety of data types without the need to duplicate code for each data type. This concept is known as template metaprogramming and is widely used in C++ to create reusable and flexible code.
Here’s a detailed explanation of templates in C++:
Table of Contents
1. Function Templates
A function template is a blueprint for creating functions that can work with multiple data types. The template specifies a generic parameter type, and this parameter type can be used in the function’s parameters, return type, and implementation.
template <typename T>
T add(T a, T b) {
return a + b;
}
int main() {
int sum1 = add(5, 3); // Calls add<int>(5, 3)
double sum2 = add(2.5, 1.5); // Calls add<double>(2.5, 1.5)
return 0;
}
2. Class Templates
A class template is a blueprint for creating classes that can work with multiple data types. Like function templates, class templates allow you to define a generic parameter type that can be used throughout the class definition.
template <typename T>
class Container {
public:
Container(T value) : data(value) {}
T getValue() const { return data; }
private:
T data;
};
int main() {
Container<int> intContainer(5);
Container<double> doubleContainer(3.14);
int intValue = intContainer.getValue();
double doubleValue = doubleContainer.getValue();
return 0;
}
3. Template Specialization
You can provide specialized implementations for specific data types within a template. This is known as template specialization.
template <>
class Container<char> {
public:
Container(char value) : data(value) {}
char getValue() const { return data; }
private:
char data;
};
4. Non-Type Template Parameters
Templates can also have non-type parameters, which are constants or values that are not data types. These parameters are evaluated at compile time.
template <int N>
int power(int base) {
int result = 1;
for (int i = 0; i < N; ++i) {
result *= base;
}
return result;
}
int main() {
int result = power<3>(2); // Computes 2^3 = 8
return 0;
}
5. Template Arguments Deduction
In C++17 and later, you can use template argument deduction to simplify template function calls.
template <typename T>
T add(T a, T b) {
return a + b;
}
int main() {
auto sum = add(5, 3); // Deduces int as the template argument
return 0;
}
Benefits of Templates
- Code Reusability: Templates allow you to write generic code that can be used with multiple data types.
- Flexibility: Templates enable you to create functions and classes that adapt to different data types.
- Performance: Templates generate specialized code for each data type, potentially leading to optimized code.
- Standard Library: The C++ Standard Library extensively uses templates to provide generic data structures and algorithms.
Templates in C++ are a powerful tool for creating versatile and efficient code that can work with various data types while reducing code duplication. They are widely used in C++ programming to create generic libraries, algorithms, and container classes.