Introduction to C++ | Templates
C++ Templates
C++ Templates are a powerful feature that allows you to write generic functions and classes. Templates enable the creation of functions, classes, and structures that can operate on any data type. This makes your code reusable and type-independent.
What are Templates?
A template is a blueprint or formula for creating a generic class or function. It allows you to define the data type of the object or function when it is used.
- Function Templates: Used to create functions that can operate on different data types.
- Class Templates: Used to create classes that can work with different data types.
Syntax of Function Templates
Function Template Syntax
template
T add(T a, T b) {
return a + b;
}
Example of Function Template
Here’s an example where a function template is used to add two numbers of any type:
Code Example
#include <iostream>
using namespace std;
// Function template to add two numbers of any type
template
T add(T a, T b) {
return a + b;
}
int main() {
cout << "Sum of integers: " << add(5, 10) << endl;
cout << "Sum of doubles: " << add(3.14, 2.72) << endl;
return 0;
}
Output
Sum of doubles: 5.86
Syntax of Class Templates
Class Template Syntax
template
class Box {
private:
T value;
public:
Box(T val) : value(val) {}
T getValue() {
return value;
}
};
Example of Class Template
Here’s an example where a class template is used to create a box that stores any type of value:
Code Example
#include <iostream>
using namespace std;
// Class template to store a value of any type
template
class Box {
private:
T value;
public:
Box(T val) : value(val) {}
T getValue() {
return value;
}
};
int main() {
Box<int> intBox(10);
Box<double> doubleBox(5.75);
cout << "Integer Value: " << intBox.getValue() << endl;
cout << "Double Value: " << doubleBox.getValue() << endl;
return 0;
}
Output
Double Value: 5.75
Key Points About Templates
- Templates allow you to write code that works with any data type.
- They enable code reusability and reduce redundancy.
- The typename keyword is used to define a generic type in templates.
- Function and class templates are instantiated with specific types when they are called or used.
- Templates can also be specialized for specific types if needed.
Template Specialization
Template specialization allows you to provide a specific implementation for a template when it is used with a particular type.
Code Example: Template Specialization
#include <iostream>
using namespace std;
// General template
template
T multiply(T a, T b) {
return a * b;
}
// Template specialization for type 'string'
template <>
string multiply(string a, string b) {
return a + b; // Concatenate strings instead of multiplying
}
int main() {
cout << "Multiplying integers: " << multiply(3, 4) << endl;
cout << "Multiplying strings: " << multiply(string("Hello "), string("World!")) << endl;
return 0;
}
Output
Multiplying strings: Hello World!
Advantages of Using Templates
- Code Reusability: Write a single function or class that can work with any data type.
- Type Safety: Templates ensure that the correct data type is used, reducing errors at compile time.
- Efficiency: Templates are resolved at compile time, which leads to more efficient code.
Pro Tip:
💡 Pro Tip
Template specialization can be used to customize the behavior of templates for certain data types, allowing you to provide more specific implementations where necessary.