5.2.32 · D3 · HinglishC++ Programming

Worked examplesModules (C++20) — concept and syntax

4,319 words20 min read↑ Read in English

5.2.32 · D3 · Coding › C++ Programming › Modules (C++20) — concept and syntax

Prerequisite ideas jo hum yahan use karte hain: The C++ Preprocessor (#include asal mein kya karta hai), Translation Units and Linkage (ek "compiled unit" kya hota hai), Namespaces in C++ (visibility ka ek alag axis), Module Partitions (ek module ko multiple files mein split karna), aur Build Systems and Compilation Speed (yeh sab kyun matter karta hai).


Pehle: ek module file ki anatomy

Do phrases neeche har example mein milte hain — global module fragment aur named-module region — toh chalo inhe ek picture mein pin karte hain pehle hum inhe use karein. Woh line jo fragment kholti hai woh bare word module; hai — ek token, ek semicolon, kuch nahi — toh use pehle define karte hain.

Figure — Modules (C++20) — concept and syntax

Figure mein plum-shaded band (fragment / airlock, module; se khula) aur dividing line ke neeche teal region (named module) follow karo. Baad ke har example mein bas yahi variation hai ki ek line us dividing line ke relative kahan padti hai.


Scenario matrix

Yeh pura space hai un cases ka jo module topic aap par throw kar sakta hai. Neeche har cell mein kam se kam ek worked example hai — example numbers brackets mein hain.

Axis Case class Covered by
Export position single export on one declaration E1
export { ... } block (group) E1
no export → private name E1, E2
#include placement legal: inside global module fragment E3
illegal: after export module E3
degenerate: no fragment needed at all E1
File roles one file does everything (interface = implementation) E1
split: interface unit + implementation unit E4
Boundary confusion export vs class public/private E5
Multiplicity do files same export module claim karte hain (error) E6
correct fix: partitions E6
Macros macro leakage: kya import use carry karta hai? (no) E7
header unit import "h.h" does carry it E7
Standard library import std; / header units vs #include E7
Real-world word problem build-speed estimate: parse-once payoff E8
Exam twist trace karo kya compiles / kya kahan visible hai E2, E5, E9

E1 — Baseline: single, group, aur private (cell: export position + degenerate no-fragment)

Figure — Modules (C++20) — concept and syntax

E2 — Forecast trap: private name ko touch karna (cell: no-export → error)


Figure — Modules (C++20) — concept and syntax

E4 — Split interface / implementation (cell: two file roles)

Figure — Modules (C++20) — concept and syntax

E5 — export vs public collision (cell: boundary confusion / exam twist)


E6 — Duplicate interface vs partitions (cell: multiplicity)


E7 — Macros, header units, aur standard library (cell: macro leakage + import std)


E8 — Real-world word problem: build-speed payoff (cell: word problem)


E9 — Exam twist: full visibility trace (cell: exam-style)


Active recall

Recall Expand karne se pehle jawab do

Bare module; line (bina name ke) kis kaam aati hai, aur ise omit karne par kya toot jaata hai? ::: Yeh global module fragment kholti hai; ise omit karo aur #include likhne ki koi legal jagah nahi rehti (E1, E3). Module file mein #include ka SINGLE legal spot kahan hai? ::: Global module fragment mein, module; aur export module Name; ke beech (E3). Ek exported class ka private member — importers ko visible? ::: Nahi; class door (public) use block karta hai chahe module door (export) open ho (E5). Kya import lib; lib ke macros laata hai? ::: Nahi — named-module import koi macros export nahi karta; agar genuinely zaroorat ho toh header unit import "h.h"; use karo (E7). Header unit vs named-module import — kaun macros rakhta hai? ::: Header unit (import "h.h";) macros rakhta hai; named-module import (import util;) nahi rakhta (E7). Do files dono export module shape; likhte hain — legal? ::: Nahi; ek module mein exactly ek primary interface unit hoti hai. Partitions use karo export module shape:part; (E6). Ek header 0.5 s parse, 200 units mein include — #include vs module parse totals? ::: s vs s, saving (E8).