C++ के Modules (C++20)
In this page:
export module module_name;
export return_type function_name(parameters) { }
// in another file
import module_name;
Modular Design Samajhna
C++20 Modules traditional #include header system का एक modern replacement हैं, compilation तेज़ करने और हर file को यह explicit, isolated control देने के लिए design किए गए कि यह program के बाकी हिस्से को क्या expose करती है।
उदाहरण: Understanding Modular Design
// math.cppm -- a C++20 module file
export module math;
export int add(int a, int b) {
return a + b;
}
// main.cpp would then say: import math; add(2, 3);
Login to try C/C++/Java/PHP code in the editor
Modules Headers से बेहतर क्यों हैं
Traditional headers preprocessor पर depend करते हैं जो literally इनका पूरा content हर file में copy-paste कर देता है जो इन्हें include करती है, जिसका मतलब same declarations एक बड़े codebase में बार-बार re-parse होते हैं।
Modules एक efficient binary interface file में एक बार compile होते हैं जो फिर सीधे load होती है, बिना re-parse हुए।
उदाहरण: Why Modules are Better than Headers
// shapes.cppm
export module shapes;
export class Circle {
public:
double radius;
};
// Unlike a #include'd header, this isn't re-parsed textually by every
// importing file -- the compiler processes it once and reuses the result.
Login to try C/C++/Java/PHP code in the editor
C++20 export और import Syntax
आप एक source file की शुरुआत में export module ModuleName; से एक module define करते हैं, specific declarations को export से mark करते हुए ताकि ये module के public interface का हिस्सा बनें।
जो भी file इसे इस्तेमाल करना चाहे वह एक #include directive की बजाय import ModuleName; लिखती है।
उदाहरण: C++20 export and import syntax
// greet.cppm
export module greet;
export void sayHello() {
// prints a greeting
}
// main.cpp:
// import greet;
// int main() { sayHello(); }
Login to try C/C++/Java/PHP code in the editor
Symbol Collisions Prevent करना
Headers के उलट, एक module के अंदर declared लेकिन explicitly export से mark न की गई कोई भी चीज़ पूरी तरह उस module तक private रहती है -- यह उन files में names में leak या collide नहीं कर सकती जो इसे import करती हैं, जो बड़े header-based projects में common naming conflicts की पूरी category खत्म करता है।
उदाहरण: Preventing Symbol Collisions
// mathutils.cppm
export module mathutils;
int internalHelper(int x) { // not exported: stays private to this module
return x * 2;
}
export int doubleValue(int x) {
return internalHelper(x);
}
Login to try C/C++/Java/PHP code in the editor
Modular Best Practices
Well-designed modules अपना exported interface छोटा और deliberate रखते हैं -- सिर्फ genuinely public functions और types export होने चाहिए, जबकि internal helper functions और implementation details unexported रहें, वह public/private discipline mirror करते हुए जो आप एक class के अंदर apply करते।
उदाहरण: Modular Best Practices
// account.cppm
export module account;
double calculateFee(double amount) { // internal detail, not exported
return amount * 0.02;
}
export double totalWithFee(double amount) { // small, deliberate public interface
return amount + calculateFee(amount);
}
Login to try C/C++/Java/PHP code in the editor
- C++20 modules के लिए एक compiler flag जैसे
-std=c++20के बिनाimportइस्तेमाल करना, इसलिए compiler इसे समझता नहीं। - same content का
#includeऔरimportमिलाना और redefinition errors पाना। exportkeyword भूल जाना, इसलिए module में names importing code से इस्तेमाल नहीं हो सकते।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: