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

Presentation 2

The document provides an overview of templates and generic programming in C++, highlighting their purpose, syntax, and advanced features. It covers function templates, class templates, variable templates, non-type template parameters, and template metaprogramming, along with best practices and debugging tips. Real-world applications such as STL containers and smart pointers are also discussed, emphasizing the importance of type safety and code reusability.

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 ODP, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views15 pages

Presentation 2

The document provides an overview of templates and generic programming in C++, highlighting their purpose, syntax, and advanced features. It covers function templates, class templates, variable templates, non-type template parameters, and template metaprogramming, along with best practices and debugging tips. Real-world applications such as STL containers and smart pointers are also discussed, emphasizing the importance of type safety and code reusability.

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 ODP, PDF, TXT or read online on Scribd
You are on page 1/ 15

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



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).
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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy