Generic Programming Concepts in C++
Generic programming is a programming paradigm that focuses on writing code that works with any data type. The goal of generic programming is to create functions and data structures that can operate on different types of data without being rewritten for each specific type. This is mainly achieved using templates in C++.
In C++, templates are used to define functions, classes, and algorithms that can work with any data type, and they are central to the concept of generic programming.
Templates allow you to define functions or classes that can operate with any data type. They provide the flexibility to work with multiple data types without the need to duplicate code for each type. Templates are the foundation of generic programming in C++.
A function template is a template that defines a generic function. This function can then be used with different data types. The function template is instantiated (created) when you call it with a specific data type.
template <typename T> // or template <class T>
T add(T a, T b) {
return a + b;
}
T is a placeholder for any data type, which will be replaced with an actual type when the function is called.#include <iostream>
using namespace std;
template <typename T>
T add(T a, T b) {
return a + b;
}
int main() {
cout << add(10, 20) << endl; // Works with int
cout << add(3.5, 4.5) << endl; // Works with double
cout << add(1.2f, 2.3f) << endl; // Works with float
return 0;
}
add() function is a template that can take arguments of any type T and return the sum of the two values.int, double, and float when called.A class template allows you to define a generic class. Like function templates, class templates are also parameterized by types, and you can create objects of different types based on the template.
template <typename T> // or template <class T>
class Box {
private:
T value;
public:
Box(T val) : value(val) {}
T getValue() {
return value;
}
};
T is a placeholder for any data type, which is defined when the object is created.#include <iostream>
using namespace std;
template <typename T>
class Box {
private:
T value;
public:
Box(T val) : value(val) {}
T getValue() {
return value;
}
};
int main() {
Box<int> intBox(10); // Box of int
Box<double> doubleBox(3.14); // Box of double
cout << "Integer Box: " << intBox.getValue() << endl;
cout << "Double Box: " << doubleBox.getValue() << endl;
return 0;
}
Box class template is created for a specific type (int, double, etc.) when instantiated.Box<int> is a class that stores an int, and Box<double> stores a double.While templates allow for generic programming, there are cases where you may want to write specific code for a particular data type. Template specialization allows you to define a custom implementation of a template for a specific type.
template <>
class Box<bool> {
private:
bool value;
public:
Box(bool val) : value(val) {}
bool getValue() {
return value;
}
};
#include <iostream>
using namespace std;
template <typename T>
class Box {
private:
T value;
public:
Box(T val) : value(val) {}
T getValue() {
return value;
}
};
// Specialization for bool type
template <>
class Box<bool> {
private:
bool value;
public:
Box(bool val) : value(val) {}
bool getValue() {
return value;
}
};
int main() {
Box<int> intBox(10);
Box<double> doubleBox(3.14);
Box<bool> boolBox(true); // Specialization for bool
cout << "Integer Box: " << intBox.getValue() << endl;
cout << "Double Box: " << doubleBox.getValue() << endl;
cout << "Bool Box: " << boolBox.getValue() << endl;
return 0;
}
Box class template is provided for the bool data type.Box<bool> is instantiated, the specialized version of the Box class is used, while other types use the generic version.C++11 introduced variadic templates, which allow templates to accept a variable number of template arguments. This is particularly useful when you don't know in advance how many arguments you will be dealing with.
template <typename... Args>
void print(Args... args) {
(cout << ... << args) << endl; // Fold expression (C++17)
}
Args... is a parameter pack that can accept any number of arguments.#include <iostream>
using namespace std;
template <typename... Args>
void print(Args... args) {
(cout << ... << args) << endl; // Fold expression to print all arguments
}
int main() {
print(10, 20, 30); // Works with multiple ints
print(1.1, 2.2, 3.3); // Works with multiple doubles
print("Hello", "World"); // Works with multiple strings
return 0;
}
print() function is a variadic template that can accept any number of arguments of different types.(cout << ... << args) simplifies printing all arguments.Code Reusability:
Type Safety:
Efficiency:
Extensibility:
Standard Template Library (STL):
vector, list, and map) and algorithms (like sort, find, and copy).Algorithms:
Overall, generic programming helps you write more flexible, reusable, and type-safe code in C++, promoting efficiency and reducing redundancy.
Open this section to load past papers