Templates Generic Programming
Templates Generic Programming
Programming
Generic Programming
code in a way that is abstracted from any specific type. This allows for
templates.
Generic Programming
1.Code Reusability
cases.
Templates in C++
• Templates are the foundation of generic programming, which involves writing code in
a) Function Template
b) Class Template
Function Overloading
// or
int main()
{
int x = 5, y = 2;
float a = 4.5, b = 1.3;
cout << num(x, y) << "\n";
cout << num(a, b);
}
Macros vs Templates
Macros and templates are both mechanisms in C++ for achieving generic
Macros
programming. Templates
Macros operate purely on text and offer no type safety Templates provide strong type safety because they are type-
guarantees. aware and checked by the compiler.
Macros, being text substitutions, can obscure debugging and Templates produce more readable and maintainable code
make code harder to understand. because they are part of the C++ language syntax and benefit
from compiler error messages.
Macros are global and can potentially pollute the global Templates respect C++ scoping rules and can be confined
namespace. within namespaces and classes, enhancing encapsulation and
reducing name clashes.
Macros are typically used for simple text substitutions or Templates are suitable for most generic programming needs
when compatibility with older codebases or languages (like C) in modern C++, providing better safety and flexibility.
is necessary.
Macros vs Templates
#define Square(x) ((x) * (x)) template <typename T>
T Square(T x)
int main() {
{ return x * x;
int a = 5; }
int result = Square(a++); // int main()
Expands to: ((a++) * (a++)), leads to {
undefined behavior int a = 5;
} int result = Square(a); // Calls
Square<int>(5)
}
Macros vs Templates
#define Max(x,y) (x>y?x:y)
int main()
{
int a=3,b=7;
cout<<Max(a,b)<<endl;
cout<<Max(a++,b++)<<endl;
cout<<Max(a,b);
}
Macros vs Templates
template <typename T>
T Max(T x,T y)
{
return(x>y?x:y);
}
int main()
{
int a=3,b=7;
cout<<Max(a,b)<<endl;
cout<<Max(a++,b++)<<endl;
cout<<Max(a,b);
}
Function with two generic types
// Generic function
template <typename T>
void print(T value)
{ int main()
cout << value << endl;
} {
template<>
void print(int value)
print(42);
{ print(3.14);
cout << "Integer: " << value << endl;
} print("Hello");
template<> }
void print(double value)
{
cout << "Double: " << value << endl;
}
Using standard parameters with template functions
template<typename T>
T fun(T r, double pi=3.14) int main()
{ {
double area=pi*r*r; fun(4,5);
cout<<"Area of circle is fun(10);
"<<area; }
}
Using standard parameters with template functions
template<class X>
void tab(X data, int tab=1)
{
int main()
for(; tab; tab--) {
for(int i=0; i<8; i++) tabo("This is a test");
{ tabo(100, 1);
cout << ' ‘; tabo('X', 2);
} tabo(10/3, 3);
cout << data << "\n";
}
}
Generic function restrictions in C++
1. Not all operations are generic:
Template functions cannot have virtual functions. Virtual functions are used for
polymorphism, which relies on runtime type information. Since templates are
resolved at compile time, virtual functions wouldn't work in this context.
Generic function restrictions in C++
3. Limited friend functions with templates:
Friend functions can be declared with templates, but they lose the ability to
access private members of the templated class. This is to prevent unintended
access and maintain encapsulation.
4. Performance considerations:
While generics offer code reuse, in some cases, compiling a generic function for
different types might lead to slightly less efficient code compared to a
specifically written function. This is because the compiler might need to
generate multiple versions of the function for different types.