Function Templates and Class Templates in C++
C++ provides powerful tools for creating generic functions and classes, allowing developers to write flexible and reusable code. This is achieved through function templates and class templates. These templates allow the same code to work with different data types without duplication, making code easier to maintain and extend.
Function Templates
A function template is a blueprint for creating functions that can work with any data type. The syntax uses the template
keyword followed by a type parameter. This allows the function to handle multiple data types based on the needs of the program.
Example: Generic Function to Swap Values
#include <iostream> // Function template to swap values template <typename T> void swapValues(T& a, T& b) { T temp = a; a = b; b = temp; } int main() { int x = 10, y = 20; double a = 1.1, b = 2.2; // Swap integer values std::cout << "Before swapping: x = " << x << ", y = " << y << std::endl; swapValues(x, y); std::cout << "After swapping: x = " << x << ", y = " << y << std::endl; // Swap double values std::cout << "Before swapping: a = " << a << ", b = " << b << std::endl; swapValues(a, b); std::cout << "After swapping: a = " << a << ", b = " << b << std::endl; return 0; }
Output:
Before swapping: x = 10, y = 20 After swapping: x = 20, y = 10 Before swapping: a = 1.1, b = 2.2 After swapping: a = 2.2, b = 1.1
In the above example, the function swapValues
is defined as a template, allowing it to swap values of any data type (e.g., int
or double
) without needing separate implementations.
Class Templates
Class templates work similarly to function templates but allow entire classes to be defined with a type parameter. This means that a class can operate on different types of data, making it more versatile and reusable.
Example: Generic Stack Class
#include <iostream> #include <vector> // Class template for Stack template <typename T> class Stack { private: std::vector<T> elements; // Vector to hold stack elements public: void push(const T& element) { elements.push_back(element); } void pop() { if (!elements.empty()) { elements.pop_back(); } else { std::cout << "Stack is empty!" << std::endl; } } T top() const { if (!elements.empty()) { return elements.back(); } else { throw std::runtime_error("Stack is empty!"); } } bool isEmpty() const { return elements.empty(); } }; int main() { // Using Stack with int Stack<int> intStack; intStack.push(10); intStack.push(20); std::cout << "Top of int stack: " << intStack.top() << std::endl; intStack.pop(); std::cout << "Top of int stack after pop: " << intStack.top() << std::endl; // Using Stack with std::string Stack<std::string> stringStack; stringStack.push("Hello"); stringStack.push("World"); std::cout << "Top of string stack: " << stringStack.top() << std::endl; return 0; }
Output:
Top of int stack: 20 Top of int stack after pop: 10 Top of string stack: World
In this example, the Stack
class is defined as a template that can be used with any data type, such as integers and strings. This provides a generic solution to the stack implementation.
Template Specialization
In some cases, you may need to handle a particular data type differently. This is where template specialization comes into play. You can define a specialized version of a template for a specific data type.
Example: Specializing for char
Type
#include <iostream> template <typename T> void print(T value) { std::cout << "Value: " << value << std::endl; } // Template specialization for char template <&typename T> void print(char value) { std::cout << "Character: " << value << std::endl; } int main() { print(10); // General template print('A'); // Specialized template for char return 0; }
Output:
Value: 10 Character: A
In this example, the print
function is specialized for the char
type, while the generic template works for other data types.
Advantages of Templates
Using templates in C++ provides several key benefits:
- Code Reusability: Templates allow you to write generic code that can work with any data type, reducing redundancy.
- Type Safety: Templates provide compile-time type checking, ensuring that the right types are used.
- Flexibility: Templates can be used with any class, struct, or built-in data type, making your code more flexible and adaptable.
Conclusion
Templates in C++ are a powerful feature that promotes generic programming. Function templates and class templates allow for writing flexible, reusable code that can operate with different data types. By using templates, C++ developers can create highly efficient and maintainable programs.