Modules (C++20) — concept and syntax
5.2.32· Coding › C++ Programming
Modules kyun exist karte hain?
Student-friendly contrast:
#include (purana) |
import (C++20) |
|---|---|
| Textual copy-paste | Compiled binary interface |
| Har baar re-parse hota hai | Ek baar parse hota hai |
| Macros andar leak hote hain | Macros nahi leak hote |
| Include guards chahiye | Koi guards nahi chahiye |
| Order-sensitive | Order-independent |
Module precisely kya hota hai?
Isko kaise likhte hain? (syntax, scratch se)
Hum structure ko piece by piece derive karenge.
Step 1 — module ko naam do aur export karo.
// math.ixx (primary interface unit)
export module math; // declare karta hai ki YEH file module "math" haiYeh step kyun? export module math; kehta hai "yahan compile hua sab kuch math naam ki unit se belong karta hai; importers import math; use karenge." Yeh pehla real declaration hona chahiye.
Step 2 — jo cheezein public chahiye unhe export karo.
export module math;
export int add(int a, int b) { // 'export' => importers ko visible
return a + b;
}
int secret_helper() { return 42; } // koi export nahi => module ke andar privateYeh step kyun? export keyword darwaza hai. add importer ki view mein aata hai; secret_helper andar hi rehta hai. Yeh file level pe encapsulation hai.
Step 3 — ek saath poore group ko export karo.
export { // export block: andar sab kuch export hota hai
int sub(int a, int b) { return a - b; }
int mul(int a, int b) { return a * b; }
}Yeh step kyun? Baar baar export likhne se bachata hai. Sirf convenience hai, meaning bilkul same hai.
Step 4 — doosri file mein use karo.
// main.cpp
import math; // module math ke EXPORTED names le aao
#include <iostream>
int main() {
std::cout << add(2, 3); // kaam karta hai: add export tha
// secret_helper(); // ERROR: export nahi tha, yahan invisible hai
}Yeh step kyun? import math; sirf exported surface laata hai. Kuch aur leak nahi hota — yahi toh poora point hai.
Step 5 — agar purane headers use karne hon, unhe isolate karo.
module; // global module fragment shuru karo
#include <cmath> // legacy header sirf YAHAN allowed hai
export module geometry;
export double hypot2(double a, double b) {
return std::sqrt(a*a + b*b);
}Yeh step kyun? #include export module ...; ke baad appear nahi ho sakta. Global module fragment preprocessor code ke liye quarantine zone hai.

Standard Library ko Import Karna
Worked example: interface aur implementation ko alag karna
Common mistakes (Steel-manned)
Active recall
Recall Quick self-test (expand karne se pehle jawab do)
- Primary interface unit ko kaun si keyword line mark karti hai? →
export module Name; - Module ke andar
#includeke liye ek legal jagah kahan hai? → global module fragment. - Kya
importmacros leak karta hai? → Nahi (header units ko chhod ke). - Ek module mein kitne primary interface units hote hain? → Exactly ek.
export module math; kya declare karta hai?
math ki primary interface unit hai; iske export kiye gaye names importable ho jaate hain.Global module fragment kya hai aur kahan jaata hai?
module; aur export module Name; ke beech ka region; yahi ek jagah hai jahan legacy #include directives allowed hain.export module m; aur module m; mein kya fark hai?
export module m; (single) interface unit hai; module m; ek implementation unit hai jo koi naya public name add nahi karta.Kya kisi module ko import karne se tumhari file mein uske macros aa jaate hain?
Module builds #include se tez kyun hote hain?
Ek single declaration export karne wala keyword kaun sa hai?
export jo declaration se pehle lagaaya jaata hai (ya kai ko export { ... } block mein wrap karke).Ek module mein kitne primary interface units ho sakte hain?
export module m:part; jaisi partitions honi chahiye.Kya ek exported class ka private member importers ko visible hota hai?
export module boundary cross karta hai, lekin private class-member access abhi bhi block karta hai.Recall Feynman: ek 12-saal ke bachche ko samjhao
Socho tumhara recipe share karne ka purana tarika tha ki poori cookbook photocopy karo aur har dost ke bag mein thuns do — slow, aur margins mein likhe notes (macros) bhi copy ho jaate hain. Ek module ki jagah ek sealed lunchbox hai: tum decide karte ho ki exactly kaun se dishes (export) tum lid par rakhoge doston ke liye, aur secret ingredients andar chhupa ke rakhte ho. Dost bas kehte hain "mujhe math lunchbox do" (import math;) aur sirf labelled dishes milti hain — koi margin scribbles nahi, aur box tumne sirf ek baar banaya.
Connections
- Header Files and #include — woh legacy mechanism jo modules replace karte hain.
- The C++ Preprocessor — kyun textual paste aur macro leakage problematic hain.
- Translation Units and Linkage — modules compilation ki unit ko redefine karte hain.
- Namespaces in C++ — dono names manage karte hain, lekin namespaces cross-file visibility control nahi karte.
- Build Systems and Compilation Speed — modules ka main practical faida.
- Module Partitions — ek module ko kai interface files mein split karna.