100% found this document useful (1 vote)
12 views

Generic Programming in C

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
12 views

Generic Programming in C

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Generic Programming in C++: Templates

Student’s name: Luong Linh Khoi


Student’s ID: 23127396

Generic Programming
Generic programming is a paradigm in C++ that enables
developers to write code that works with different data types
without rewriting the code for each type.
Generics can be implemented in C++ using Templates. C++ adds
two new keywords to support
templates: ‘template’ and ‘typename’. The second keyword can
always be replaced by the keyword ‘class’.

C++ Templates
Function template
Function templates allow you to define a single function to
operate on different data types.
Example: Implementing a function that returns the larger
of a and b
template<typename T>
const T& max(const T& a, const T& b)
{
return a < b ? b : a;
}
Class template
Class templates allow you to define a single class to operate on
different data types.
Example: Implementing an Array class of any type
template <typename T> class Array {
private:
T* ptr;
int size;
public:
Array(T arr[], int s);
void print();
};
template <typename T> Array<T>::Array(T arr[], int s)
{
ptr = new T[s];
size = s;
for (int i = 0; i < size; i++)
ptr[i] = arr[i];
}
template <typename T> void Array<T>::print()
{
for (int i = 0; i < size; i++)
cout << " " << *(ptr + i);
}
Variadic template (since C++11)
Variadic templates allow functions or classes to accept a variable
number of template arguments.
Example: Implementing a function that adds all of its
arguments together
template<typename T>
T adder(T v) {
return v;
}
template<typename T, typename... Args>
T adder(T first, Args... args) {
return first + adder(args...); // using recursion
}
Template specialization
When a function or class is instantiated from a template,
a specialization of that template is created by the compiler for the
set of arguments used, and the specialization is referred to as
being a generated specialization
Example: Implementing a sort function for any types but
acts differently on char type
template <class T>
void sort(T arr[], int size)
{
// …
}
// Template Specialization: A function
// specialized for char data type
template <>
void sort<char>(char arr[], int size)
{
// …
}
Pros & Cons
Pros
1. Code Reusability
o Templates enable writing a single piece of code that can
operate on different types, reducing redundancy.
2. Type Safety
o Templates are type-safe, meaning the compiler checks
types at compile time and prevents type-related errors.
3. Efficiency
o Template-based code is resolved at compile time,
resulting in no runtime overhead.
4. Flexibility
o Templates can handle complex data structures and
algorithms with ease, providing great flexibility.
Cons
1. Code Bloat
Every instantiation of a template generates a new piece of
code, which can increase binary size.
2. No Information Hiding
All code is exposed in the header file. No one library can
solely contain the code
3. Hard To Debug
Debugging template-heavy code can be challenging due to
its complexity.

References
[1] GeeksForGeeks, Templates in C++ with Examples,
https://www.geeksforgeeks.org/templates-cpp
[2] Wikipedia, Templates( C++),
https://en.wikipedia.org/wiki/Template_(C%2B%2B)

You might also like