Templates in C++
Templates are a powerful feature in C++ that allow functions and classes to operate with generic types. This means you can write a function or class to work with any data type, without having to rewrite it for each type.
1. What are Templates?​
Templates in C++ allow you to create generic functions and generic classes. You can define a function or class once and use it with different data types without rewriting it for each one.
Types of Templates:​
- Function Templates
- Class Templates
2. Function Templates​
A function template works with any data type. You can write a function template that works for int, float, double, or any other type.
Syntax of a Function Template:​
template <typename T>
T function_name(T arg1, T arg2) {
// Code here
}
T
is the placeholder for the data type.
Example of a Function Template:​
#include <iostream>
using namespace std;
template <typename T>
T add(T a, T b) {
return a + b;
}
int main() {
cout << "Sum of integers: " << add(5, 10) << endl;
cout << "Sum of floats: " << add(5.5, 10.1) << endl;
return 0;
}
Output:​
Sum of integers: 15
Sum of floats: 15.6
3. Class Templates​
A class template allows you to create a class that can work with any data type. This is useful for creating data structures like stacks, queues, or linked lists that can handle any type of data.
Syntax of a Class Template:​
template <typename T>
class ClassName {
T variable;
public:
ClassName(T var) : variable(var) {}
void display() {
cout << "Variable: " << variable << endl;
}
};
Example of a Class Template:​
#include <iostream>
using namespace std;
template <typename T>
class Container {
T value;
public:
Container(T val) : value(val) {}
void display() {
cout << "Value: " << value << endl;
}
};
int main() {
Container<int> intContainer(42);
Container<string> stringContainer("Hello");
intContainer.display();
stringContainer.display();
return 0;
}
Output:​
Value: 42
Value: Hello
4. Template Specialization​
Sometimes, you might want to provide a specific implementation of a template for a particular data type. This is called template specialization.
Example of Template Specialization:​
#include <iostream>
using namespace std;
template <typename T>
class Calculator {
public:
T add(T a, T b) {
return a + b;
}
};
// Specialization for string data type
template <>
class Calculator<string> {
public:
string add(string a, string b) {
return a + " " + b;
}
};
int main() {
Calculator<int> intCalc;
Calculator<string> stringCalc;
cout << "Sum of integers: " << intCalc.add(5, 10) << endl;
cout << "Concatenated strings: " << stringCalc.add("Hello", "World") << endl;
return 0;
}
Output:​
Sum of integers: 15
Concatenated strings: Hello World
5. Advantages of Templates​
- Code Reusability: You write a generic function or class once, and use it with any data type.
- Type Safety: Templates are type-safe and checked during compilation.
- Flexibility: Templates allow you to create more flexible, reusable, and maintainable code.
6. Final Thoughts​
Templates in C++ offer a powerful way to create functions and classes that can work with any data type. They help in writing generic, reusable code and are a key feature in modern C++ programming.
Happy coding!