{"id":50338697,"url":"https://github.com/tigran-sargsyan-w/cpp-module-07","last_synced_at":"2026-05-29T15:30:21.779Z","repository":{"id":340228656,"uuid":"1165082256","full_name":"tigran-sargsyan-w/cpp-module-07","owner":"tigran-sargsyan-w","description":"This module is designed to help you understand templates in C++.","archived":false,"fork":false,"pushed_at":"2026-04-26T14:38:08.000Z","size":37,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-05-06T11:49:04.074Z","etag":null,"topics":["42","42-common-core","42-cursus","42-school","array-template","class-templates","cpp","cpp-module-07","cpp-modules","cpp07","cpp98","function-templates","generic-programming","generics","iter","memory-management","operator-overloading","orthodox-canonical-form","templates"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tigran-sargsyan-w.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-02-23T20:03:06.000Z","updated_at":"2026-04-26T14:42:05.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/tigran-sargsyan-w/cpp-module-07","commit_stats":null,"previous_names":["tigran-sargsyan-w/cpp-module-07"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/tigran-sargsyan-w/cpp-module-07","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tigran-sargsyan-w%2Fcpp-module-07","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tigran-sargsyan-w%2Fcpp-module-07/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tigran-sargsyan-w%2Fcpp-module-07/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tigran-sargsyan-w%2Fcpp-module-07/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tigran-sargsyan-w","download_url":"https://codeload.github.com/tigran-sargsyan-w/cpp-module-07/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tigran-sargsyan-w%2Fcpp-module-07/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33659872,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-29T02:00:06.066Z","response_time":107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["42","42-common-core","42-cursus","42-school","array-template","class-templates","cpp","cpp-module-07","cpp-modules","cpp07","cpp98","function-templates","generic-programming","generics","iter","memory-management","operator-overloading","orthodox-canonical-form","templates"],"created_at":"2026-05-29T15:30:20.846Z","updated_at":"2026-05-29T15:30:21.771Z","avatar_url":"https://github.com/tigran-sargsyan-w.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# C++ Module 07 – Templates 🧩📦\n\n✅ **Status**: Completed – all exercises  \n🏫 **School**: 42 – C++ Modules (Module 07)  \n🏅 **Score**: 100/100  \n\n\u003e *Function templates, template parameters, generic utilities (`swap/min/max`), applying a function to array elements (`iter`), and building a safe dynamic array class template (`Array\u003cT\u003e`).*\n\n---\n\n## 📚 Table of Contents\n\n* [Description](#-description)\n* [Goals of the Module](#-goals-of-the-module)\n* [Templates Notes \u0026 Nuances](#-c-templates--important-notes--nuances-c98)\n* [Exercises Overview](#-exercises-overview)\n\n  * [ex00 – Start with a few functions](#ex00--start-with-a-few-functions)\n  * [ex01 – Iter](#ex01--iter)\n  * [ex02 – Array](#ex02--array)\n* [Requirements](#-requirements)\n* [Build \u0026 Run](#-build--run)\n* [Repository Layout](#-repository-layout)\n* [Testing Tips](#-testing-tips)\n* [42 Notes](#-42-notes)\n\n---\n\n## 📝 Description\n\nThis repository contains my solutions to **42’s C++ Module 07 (C++98)**.\n\nModule 07 is dedicated to **C++ templates** — writing code that works with **any type** as long as the type supports the required operations (comparisons, assignments, etc.).\nIt introduces both **function templates** and **class templates**, and focuses on building safe and reusable generic components.\n\nAll exercises are written in **C++98** and compiled with strict flags (`-Wall -Wextra -Werror -std=c++98`).\n\n---\n\n## 🎯 Goals of the Module\n\nConcepts covered:\n\n* **Function templates** and type deduction\n* Writing generic utilities (`swap`, `min`, `max`)\n* Template functions that accept **function pointers**\n* Supporting both **const** and **non-const** contexts\n* **Class template** design and memory ownership\n* **Deep copy** (copy constructor + assignment operator)\n* Implementing `operator[]` with **bounds checking + exceptions**\n* Avoiding memory leaks with correct allocation/deallocation (`new[]` / `delete[]`)\n\n---\n\n## 🧩 C++ Templates — Important Notes \u0026 Nuances (C++98)\n\nTemplates in C++ are **compile-time blueprints**.\nThey become real code only when the compiler sees a **concrete type** (`int`, `std::string`, your own class, etc.).\n\n---\n\n### 🧠 Core Rule: “Template code must be visible”\n\n#### ✅ Why templates live in `.hpp`\n\nThe compiler must see the **full implementation** at the place where it instantiates the template.\n\nIf you put template definitions only in `.cpp`, you often get linker errors like:\n\n* `undefined reference to swap\u003cint\u003e(...)`\n* `undefined reference to Array\u003cstd::string\u003e::operator[](...)`\n\n✅ Typical solutions:\n\n* Put everything in `.hpp`\n* Or split into `.hpp` + `.tpp/.ipp` and `#include` the `.tpp` from the `.hpp`\n\n---\n\n### 🔧 Function Templates (swap / min / max)\n\nFunction templates generate functions for the used types.\n\n**Key nuance (Module 07 ex00):**\n\n* `min/max` should return **by const reference** (`T const \u0026`)\n* If values are equal, the subject requires returning the **second argument**\n\n  * Use `\u003c=` / `\u003e=` to guarantee that behavior\n\n**Hidden requirement:**\n\n* `min/max` require a valid comparison operator (`\u003c`, `\u003c=`, etc.)\n* `swap` requires copy/assignment to exist for the type\n\n---\n\n### 🧱 Class Templates (Array\u003cT\u003e)\n\nClass templates generate **different types**:\n\n* `Array\u003cint\u003e` and `Array\u003cstd::string\u003e` are two different classes.\n\n**Where methods are implemented:**\n\n* Usually in `.hpp` (or `.tpp` included from `.hpp`) — because the implementation must be visible at instantiation time.\n\n**Common pitfall:**\n\n* putting methods in `.cpp` without explicit instantiation → linker errors\n\n---\n\n### 🧰 Member Function Templates\n\nA class can have a method that is itself templated.\n\n✅ Useful when:\n\n* the class type is “fixed”, but one method must accept many different types.\n\n---\n\n### 🚫 Variable Templates (Not in C++98)\n\nVariable templates exist only since **C++14**.\n\nIn C++98, the classic workaround is:\n\n* `static const` inside a templated struct/class\n\n---\n\n### ⚠️ Template Compilation “Gotchas”\n\n* Templates are generated **only for types actually used**\n* Error messages can look scary because they show “instantiation traces”\n* Template code often requires the type to support:\n\n  * copy constructor / assignment\n  * comparison operators (`\u003c`, `\u003c=`, `\u003e`, `\u003e=`)\n  * stream output (`operator\u003c\u003c`) if you print the type\n\n---\n\n## 📦 Exercises Overview\n\n### ex00 – Start with a few functions\n\n\u003e Implement classic generic helpers as function templates.\n\n**Goal:**\nImplement these **function templates**:\n\n* `swap(T \u0026a, T \u0026b)` — swaps values (no return)\n* `min(const T \u0026a, const T \u0026b)` — returns the **smaller** one\n\n  * If equal → returns the second argument\n* `max(const T \u0026a, const T \u0026b)` — returns the **greater** one\n\n  * If equal → returns the second argument\n\n**Example usage (from subject):**\n\n```bash\n$ ./whatever\na = 3, b = 2\nmin( a, b ) = 2\nmax( a, b ) = 3\nc = chaine2, d = chaine1\nmin( c, d ) = chaine1\nmax( c, d ) = chaine2\n```\n\n**Concepts practiced:**\n\n* Template implementation must be visible (header-only)\n* Type deduction and generic behavior\n* Correct equality rule (equal → return the second)\n\n---\n\n### ex01 – Iter\n\n\u003e Apply a function to each element of an array.\n\n**Goal:**\nImplement a function template:\n\n```cpp\niter(arrayPtr, length, function);\n```\n\n* 1st param: **address of an array**\n* 2nd param: **length** (passed as const)\n* 3rd param: **function** called on every element\n\n**Key requirement:**\nYour `iter` must support both:\n\n* arrays of **non-const** elements (function takes `T\u0026`)\n* arrays of **const** elements (function takes `const T\u0026`)\n\n**Concepts practiced:**\n\n* Function pointer as template parameter\n* Const-correctness in generic code\n* Reusable iteration over raw arrays\n\n---\n\n### ex02 – Array\n\n\u003e Build a minimal safe dynamic array class template.\n\n**Goal:**\nImplement a class template `Array\u003cT\u003e` with:\n\n* Default constructor → **empty array**\n* Constructor `(unsigned int n)` → array of **n default-initialized elements**\n* Copy constructor + assignment operator (**deep copy**)\n* `operator[]` for element access\n\n  * If index is out of bounds → throw `std::exception`\n* `size()` → returns number of elements, does not modify the instance\n\n**Constraints:**\n\n* Must allocate using **`new[]`**\n* No “preventive allocation” (no extra capacity)\n* Must never access non-allocated memory\n\n**Concepts practiced:**\n\n* Class templates + optional `.tpp`\n* Rule of Three and ownership safety\n* Exception-safe bounds checking\n\n---\n\n## 🛠 Requirements\n\nFrom the official rules:\n\n* **Compiler**: `c++`\n* **Flags**: `-Wall -Wextra -Werror` and compatible with `-std=c++98`\n* **No external libraries**, no C++11/Boost\n* Template implementations must be available in headers (or included `.tpp`)\n\n---\n\n## ▶️ Build \u0026 Run\n\nClone the repository and build each exercise separately.\n\n```bash\ngit clone \u003cthis-repo-url\u003e\ncd cpp-module-07\n```\n\n### ex00\n\n```bash\ncd ex00\nmake\n./whatever\n```\n\n### ex01\n\n```bash\ncd ex01\nmake\n./iter\n```\n\n### ex02\n\n```bash\ncd ex02\nmake\n./array\n```\n\n\u003e Executable names can differ depending on implementation.\n\n---\n\n## 📂 Repository Layout\n\n```text\ncpp-module-07/\n├── ex00/\n│   ├── Makefile\n│   ├── main.cpp\n│   └── whatever.hpp\n│\n├── ex01/\n│   ├── Makefile\n│   ├── main.cpp\n│   └── iter.hpp\n│\n└── ex02/\n    ├── Makefile\n    ├── main.cpp\n    ├── Array.hpp\n    └── Array.tpp      # optional (template implementation)\n```\n\n---\n\n## 🔍 Testing Tips\n\n* **ex00**\n\n  * Test with `int`, `std::string`, and custom types (with comparison operators)\n  * Verify the **equal → return second** rule for `min/max`\n\n* **ex01**\n\n  * Test with `int[]`, `std::string[]`\n  * Test both:\n\n    * function that takes `T\u0026` (can modify elements)\n    * function that takes `const T\u0026` (read-only)\n  * Also test with a `const` array to confirm const correctness\n\n* **ex02**\n\n  * Check deep copy:\n\n    * copy an array, modify original, verify copy does not change\n  * Check bounds:\n\n    * `arr[size]` must throw\n  * Check default initialization:\n\n    * `Array\u003cint\u003e(n)` contains default values (typically 0)\n\n---\n\n## 🧾 42 Notes\n\n* C++ modules do not use Norminette, but clean and readable code is still expected during evaluation.\n* Template rule: implementations must be visible at compile time (header or included `.tpp`).\n* Module 07 is meant to practice the “core” template mechanics without relying on STL containers.\n\n---\n\nIf you’re a 42 student working on the same module, feel free to explore the code, get inspired, but **write your own implementation** — that’s where the real learning happens. 🚀\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftigran-sargsyan-w%2Fcpp-module-07","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftigran-sargsyan-w%2Fcpp-module-07","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftigran-sargsyan-w%2Fcpp-module-07/lists"}