0% found this document useful (0 votes)
7 views15 pages

Presentation 2

The document introduces templates in programming, explaining their purpose as blueprints for generating type-agnostic code to avoid duplication and enhance type safety. It covers function and class templates, advanced features like default arguments and non-type parameters, and highlights their applications in real-world scenarios such as STL containers and smart pointers. Best practices and debugging tips are also provided to help avoid common pitfalls in template programming.

Uploaded by

mtahir97345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views15 pages

Presentation 2

The document introduces templates in programming, explaining their purpose as blueprints for generating type-agnostic code to avoid duplication and enhance type safety. It covers function and class templates, advanced features like default arguments and non-type parameters, and highlights their applications in real-world scenarios such as STL containers and smart pointers. Best practices and debugging tips are also provided to help avoid common pitfalls in template programming.

Uploaded by

mtahir97345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Introduction to Templates:

• What?:

• Blueprints for generating type-agnostic code.

Why?:
• Avoid code duplication (write once, use for any type).

• Type safety (better than macros/void pointers).


Analogy:
• Like a cookie cutter shaping dough (template) into cookies (type-specific code).
Templates and generic programing
• Group Members:
• Muhammad Tahir : university roll number 1301-241019

• Musadiq Ijaz : university roll number 1301- 241030

• Hassan Aslam :university roll number 1303-24102

• Muhammad Jawad : university roll number 1303-241035

• Date: 10 May 2025


Function Templates:
Syntax:

template <typename T> // 'typename' or 'class' keyword

T add(T a, T b) { return a + b;

Key concepts;
1-Template Instantiation:

cout << add<int>(3, 5); // Explicit: T = int

cout << add(3.5, 2.1); // Implicit: T = double


• Multiple Types:

template <typename T1, typename T2>

void printPair(T1 a, T2 b) { cout << a << ", " << b; }

• Template Specialization

template <>

const char* add(const char* a, const char* b) {

return strcat(a, b); // Custom behavior for strings

}
• Basic Syntax:
Class Templates:
• template <class T>
• class Box {

• private:

• T content;
• public:

• void set(T item) { content = item; }


• T get() { return content; }
• };
• Advanced Features:

• Default Template Arguments:

• template <class T = int> // Default to 'int'

• class Container { /*...*/ };

• Member Function Templates:

• template <class U>

• void compare(U other) { /* compare content with 'other' */ }


Inheritance with templates:
• template <class T>
• class PrettyBox : public Box<T> { /*...*/ };
• STL Example:

• vector<int> v; // 'vector' is a class template


• stack<string> s; // 'stack' is another


Variable Templates :
• Core Idea:
• Typed constants or compile-time values.
• Examples:

• 1-Mathematical Constants:
• template <typename T>
• constexpr T pi = T(3.141592653589793);
• cout << pi<double>; // High precision
• 2-Type-Dependent Values:
• template <class T>

• constexpr T max_limit = numeric_limits<T>::max();


• Why Useful?:

• Replace #define macros with type-safe alternatives.


Non-Type Template Parameters:
What?:
Templates can accept values (not just types).
Example:

template <int N>

class Array {

private:

int data[N]; // Fixed-size array

public:

int size() { return N; }

Array<10> arr; // N = 10

Use Cases:

Compile-time computations (e.g., factorial).


Fixed-size containers (like std::array).
Template Metaprogramming:
• Compile-Time Power:

• Templates are Turing-complete (can run logic at compile time).

• Example: Factorial Calculation:

• template <int N>

• struct Factorial {

• static const int value = N * Factorial<N-1>::value;

• };

• template <>

• struct Factorial<0> { // Base case

• static const int value = 1;


• };

• cout << Factorial<5>::value; // 120 (calculated at compile time)


• Real-World Use:

• Optimizations in libraries (e.g., Eigen for linear algebra).


Best Practices & Pitfalls:
• Dos:

• Use typename for types, class for template template parameters.

• Prefer constexpr variable templates over macros.

• Don’ts:

• Avoid overly complex template hierarchies.

• Watch for code bloat (each instantiation generates new code).


Debugging Tips:

• Use static_assert for type constraints:

• template <class T>

• void print(T val) {

• static_assert(is_integral<T>::value, "Must be
integer");
• cout << val;
•}
Real-World Applications:
• 1-STL Containers: vector<T>, map<K,V>.

• 2-Smart Pointers: unique_ptr<T>, shared_ptr<T>.

• 3-Math Libraries: Matrix operations with Matrix<double>.


Summary & Q&A!
• Key Takeaways:

• Function Templates: Generic algorithms.

• Class Templates: Reusable data structures.

• Variable Templates: Type-safe constants.


Q&A :
• "What happens if you try to
instantiate Stack<Employee> without defining operator>?"

• Anything you wanna ask or anything you wanna


add...???
***
• Thank YoU!!!

You might also like