Templates in C++ are a powerful feature that allows you to write generic, reusable code. Templates enable functions and classes to operate with any data type without rewriting the same code for each type. There are two main types of templates in C++: function templates and class templates.
Function templates allow you to write a single function that works with different data types. The template is defined with a placeholder type, which is replaced with the actual data type when the function is called.
#include <iostream>
// Define a function template that adds two values
template<typename T>
T add(T a, T b) {
return a + b;
}
int main() {
int x = 5, y = 10;
double a = 2.5, b = 3.7;
// Call the function template with different types
std::cout << "Sum of integers: " << add(x, y) << std::endl;
std::cout << "Sum of doubles: " << add(a, b) << std::endl;
return 0;
}
You can define a function template that works with multiple data types by specifying more than one template parameter.
#include <iostream>
// Define a function template that compares two values of different types
template<typename T1, typename T2>
bool areEqual(T1 a, T2 b) {
return a == b;
}
int main() {
int x = 5;
double y = 5.0;
// Call the function template with different types
std::cout << "Are they equal? " << (areEqual(x, y) ? "Yes" : "No") << std::endl;
return 0;
}
Class templates allow you to create classes that work with any data type. The class is defined with one or more placeholder types, which are specified when an object of the class is created.
#include <iostream>
// Define a class template
template<typename T>
class Box {
private:
T value;
public:
Box(T val) : value(val) {}
T getValue() const {
return value;
}
};
int main() {
Box<int> intBox(123);
Box<double> doubleBox(45.67);
// Access the value stored in the boxes
std::cout << "Integer Box: " << intBox.getValue() << std::endl;
std::cout << "Double Box: " << doubleBox.getValue() << std::endl;
return 0;
}
Class templates can also work with multiple data types by specifying more than one template parameter.
Copy code
#include <iostream>
// Define a class template with two types
template<typename T1, typename T2>
class Pair {
private:
T1 first;
T2 second;
public:
Pair(T1 a, T2 b) : first(a), second(b) {}
T1 getFirst() const {
return first;
}
T2 getSecond() const {
return second;
}
};
int main() {
Pair<int, double> p(42, 3.14);
std::cout << "First: " << p.getFirst() << std::endl;
std::cout << "Second: " << p.getSecond() << std::endl;
return 0;
}
Copy code
#include <iostream>
// Generic template
template<typename T>
T getMax(T a, T b) {
return (a > b) ? a : b;
}
// Specialized template for const char*
template<>
const char* getMax(const char* a, const char* b) {
return (std::strcmp(a, b) > 0) ? a : b;
}
int main() {
std::cout << "Max of 10 and 20: " << getMax(10, 20) << std::endl;
std::cout << "Max of 'Apple' and 'Banana': " << getMax("Apple", "Banana") << std::endl;
return 0;
}
You can also partially specialize a class template for certain types.
#include <iostream>
// Generic template
template<typename T, typename U>
class MyClass {
public:
void show() {
std::cout << "Generic MyClass" << std::endl;
}
};
// Partial specialization for T = int
template<typename U>
class MyClass<int, U> {
public:
void show() {
std::cout << "Specialized MyClass for int" << std::endl;
}
};
int main() {
MyClass<double, double> obj1;
obj1.show(); // Outputs: Generic MyClass
MyClass<int, double> obj2;
obj2.show(); // Outputs: Specialized MyClass for int
return 0;
}
Templates in C++ can also be used for template metaprogramming, a technique where templates are used to perform computations at compile time. This is an advanced usage of templates that can lead to highly efficient and optimized code.
Templates are a fundamental feature of C++, enabling generic programming and allowing for highly reusable and efficient code. Understanding templates is essential for mastering C++ and writing flexible, type-safe code.
Previous Page | Course Schedule | Course Content