Skip to content

mdbentaleb/42_CPP_Module_07

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧠 C++ Module 07

🧩 Templates, Generic Programming & Type Abstraction

πŸ“˜ Purpose of the Module

C++ Module 07 is where C++ stops being β€œtyped” and becomes parametric.

You are no longer writing code for int, float, or std::string. You are writing code for any type that behaves correctly.

This module teaches you:

  • How C++ generates code from templates
  • How types become parameters
  • How generic algorithms are built
  • How containers are implemented
  • How compile-time polymorphism works

This is not runtime polymorphism (Module 04). This is compile-time polymorphism.

Templates are how the STL exists. Now you are building that power yourself.


🧩 Exercise Overview

πŸ” Exercise 00 β€” Function Templates

You implement:

  • swap<T>()
  • min<T>()
  • max<T>()

These functions must:

  • Work for any type
  • Use operator overloading
  • Be generated at compile time

This teaches:

Templates don’t run β€” they are instantiated.

You write one function. The compiler generates many.


πŸ”„ Exercise 01 β€” Iter on Any Type

You implement:

iter(array, size, function);

This function:

  • Works on any array type
  • Works with const and non-const data
  • Works with any callable

This teaches:

  • Template parameter deduction
  • Const-correctness in templates
  • Function templates as arguments
  • How algorithms are decoupled from types

This is how std::for_each is born.


πŸ“¦ Exercise 02 β€” Template Array Class

You build:

Array<T>

A full generic container with:

  • Deep copy
  • Bounds checking
  • Dynamic allocation
  • Type safety
  • Exception safety

This teaches:

  • How STL containers are implemented
  • How templates own memory
  • How exceptions interact with generics
  • Why templates must live in headers

You are now building your own std::vector.


🧠 Core Skills Developed

After Module 07, you understand:

  • Compile-time polymorphism
  • Generic programming
  • Template instantiation
  • Type abstraction
  • Writing algorithms that work for any type
  • How STL containers are designed
  • How C++ avoids runtime overhead with templates

You are no longer writing C++ code. You are writing C++ generators.


βš™οΈ Key Takeaway

Module 07 is about power through abstraction.

If you don’t understand templates:

  • You can’t read STL
  • You can’t write reusable code
  • You can’t write high-performance libraries

If you do:

  • You can write containers
  • Algorithms
  • Libraries
  • Frameworks

Templates are the real engine of modern C++.

About

🧠 C++ Module 07 β€” Master C++ templates 🧩, generic algorithms πŸ”, and compile-time polymorphism. Learn how C++ builds reusable, type-safe code without runtime overhead.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors