← Back to C++ Course | Chapter 16: Modern C++ | Lesson 8 of 9

C++ के Modules (C++20)

Modules code को टुकड़ों में split और share करने का एक नया तरीका है, अलग labeled chapters की तरह जिन्हें आप import कर सकते हैं। ये पुराने header files से cleaner हैं।
Syntax
cpp
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

cpp
// 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);

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

cpp
// 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.

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

cpp
// greet.cppm
export module greet;

export void sayHello() {
	// prints a greeting
}

// main.cpp:
// import greet;
// int main() { sayHello(); }

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

cpp
// 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);
}

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

cpp
// 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);
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. C++20 modules के लिए एक compiler flag जैसे -std=c++20 के बिना import इस्तेमाल करना, इसलिए compiler इसे समझता नहीं।
  2. same content का #include और import मिलाना और redefinition errors पाना।
  3. export keyword भूल जाना, इसलिए module में names importing code से इस्तेमाल नहीं हो सकते।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.