← Back to list

Templates , Type Hinting, Generics !

A Takeaway from CPP Module 07 : Learning Templates

Mohammad Abir Abbas · 2023-09-28 18:46 · 0 claps · 2.5 min read
#42-network #42-wolfsburg #cpp #python #template
Open on Medium ↗
Wiki topics: EDU · Education & Learning

Templates , Type Hinting, Generics !

A Takeaway from CPP Module 07

C++ Templates (Static Molds):

  • C++ templates involves compile-time code generation where each instantiation of a template with a different type generates a new set of machine code.
  • Handles multiple data types with a single function or class definition

— — — — — — — — — — — — — —Analogy — — — — — — — — — — — — — — C++ templates are like static molds in a workshop. Each mold represents a blueprint that can be filled with specific details to create a unique object.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Code Sample and Output :

Template Code sample

Template Code sample

Output

Output

  1. Python Type hinting(Defined Lego Blocks):

— — — — — — — — — — — — — — Analogy — — — — — — — — — — — — — —

Defined shapes on LEGO blocks, ensuring that the pieces fit together correctly and hinting at how they can be combined.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Features:

  • Is dynamically typed.
  • The type hints are not enforced at runtime without additional libraries.

Code Sample and Output:

Python Type hinting

Python Type hinting

Output from Compiler

Output from Compiler

  1. Rust Generics (Flexible Blueprints):

— — — — — — — — — — — — — — Analogy — — — — — — — — — — — — — —

Versatile blueprints adapting to different objects.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Features:

  • Rust introduces generics similar to templates in c++, which are like flexible blueprints that can be used to create a wide range of objects.
  • With generics, you can define functions, structs, and enums that can work with different types, providing code reusability and type safety.
  • Uses monomorphization to generate code at compile time, similar to C++, but with a more disciplined approach that can lead to less code bloat.

Rusty Generics

Rusty Generics

Output

Output

If you find any mistakes or have a feedback feel free to write to me in slack mabbas / through email : mabbas@students.42wolfsburg.de


메타데이터
post_id
218549281400
slug
visual-content-understanding-programming-paradigms-218549281400
url
https://medium.com/@md.abir1203/visual-content-understanding-programming-paradigms-218549281400
canonical_url
https://medium.com/@md.abir1203/visual-content-understanding-programming-paradigms-218549281400
author_url
https://medium.com/@md.abir1203
status
ok
fetched_at
2026-07-31 16:21:18