5.2.32 · D1 · HinglishC++ Programming

FoundationsModules (C++20) — concept and syntax

1,634 words7 min read↑ Read in English

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

Pehle aap parent note ki ek bhi line padh sako, aapke paas pehle se thodi ideas ka dhera hona chahiye: source file kya hoti hai, compiling karta kya hai, macro kya hota hai, "ek naam visible hai" ka matlab kya hai. Parent note inhe quietly assume karta hai. Yeh page har ek ko ground up se banata hai, us order mein jisme yeh ek doosre pe depend karte hain.


1. Source file — text ka ek page

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

Figure dekho. Baayein ek text file hai. Compiler text run nahi kar sakta — pehle usse translate karna padta hai machine instructions mein. Woh translation hi agla idea hai.


2. Compiling — text ko ek machine part mein badalna


3. Translation unit — jo compiler actually chhabta hai

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

Figure mein, main.cpp file apne aap mein chhoti hai, lekin #include ke header text paste karne ke baad, translation unit phool jaati hai. Woh phoolna — same header text hundreds of TUs mein copy hona — problem #1 hai jo parent note list karta hai ("slow builds"). Yeh idea itna central hai ki iska apna vault note hai: Translation Units and Linkage.


4. Preprocessor aur #include — copy-paste machine

Kyunki #include paste hai, do files jo same header include karti hain unhe har declaration ki do copies milti hain — isliye purani duniya mein include guards chahiye the (doosra paste skip karne ki #ifndef trick). Modules is poori pareshani ko delete kar dete hain; yahi parent ki "No guards needed" row hai.


5. #define aur macros — leaky hissa

Modules macros ko import ke paar nahi le jaate. Woh ek akela fact hai isliye parent "Macros do not leak" pe jashn manata hai. Aapko pata hona chahiye ki macro kya hai tab dekhoge ki na-leakna ek jeet kyun hai.


6. Names, visibility, aur scope

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

Figure teen nested visibility rings dikhata hai. Yeh ring picture exactly wahi hai jiske baare mein parent ka sabse bada mistake callout hai: export aur public alag rings hain.

  • public / private — ek inner ring: kaun se class members doosra code touch kar sakta hai.
  • export — ek outer ring: kaun se file-level names module boundary cross karke importers tak jaate hain.

Ek export ki gayi class ka private member exported-as-a-class hai lekin phir bhi privately sealed hai — do independent rings, dono khulni chahiye aapko woh reach karne ke liye.


7. export aur import — naye keywords

export module Name; ke baad sab kuch named-module region hai, jahan #include forbidden hai — iske pehle quarantine zone ka wahi reason hai.


8. Yeh sab kyun — build-speed ka payoff


Prerequisite map

Source file = text

Compiling = text to machine code

Translation unit = one lump

Preprocessor and include = paste

Macros define = blind replace

Names and visibility

export and import

Build speed pain

Modules C++20

Har arrow kehta hai "daayein idea samajhne se pehle baayein idea chahiye." Saare raaste parent ko feed karte hain: Modules (C++20).


Equipment checklist

Khud test karo — jawab zor se bolo, phir reveal karo.

A source file is
C++ instructions ki ek plain text file, kuch magical nahi.
Compiling means
compiler ek source file padhta hai aur machine code (ek object file) produce karta hai.
Parsing is
compiling ke andar reading/understanding step — slow part jise modules ek baar karte hain.
A translation unit is
ek source file plus jo kuch bhi textually usmein paste hua, ek lump ki tarah compile hota hai.
#include <x> does
file x ka poora text wahan paste karta hai, compiling se pehle.
A macro (#define) is
ek blind text-replacement rule jo preprocessor har jagah apply karta hai, scope ignore karke.
Why macros are dangerous
woh har us file mein leak ho jaate hain jo header include kare aur kisi boundary ki izzat nahi karte.
:: means
scope resolution — "yeh name kis box (namespace ya class) mein rehta hai."
public/private control
kaun se class members bahar ka code touch kar sakta hai (inner ring).
export controls
kaun se file-level names module boundary cross karke importers tak jaate hain (outer ring).
import math; does
module math ki compiled interface padhta hai aur uske exported names laata hai — link, paste nahi.
The : in export module m:p; marks
ek partition, module m ka ek sub-piece.
Why modules build faster
interface ek baar parse/compile hokar binary mein save ho jaati hai aur reuse hoti hai, har jagah re-parse nahi hoti.