|
| 1 | + |
| 2 | +# C++20/23 Modules – Quick Reference Cheat Sheet |
| 3 | + |
| 4 | +This cheat sheet summarizes **the correct order of `#include`, `import`, and `export module`** in C++20/23 modules. |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## 1️⃣ Module File Zones |
| 9 | +``` |
| 10 | +[Zone A] Global Module Fragment → before `export module` |
| 11 | +[Zone B] Module Interface → after `export module` |
| 12 | +[Zone C] Module Implementation → optional/private |
| 13 | +``` |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## 2️⃣ Global Module Fragment (`module;`) |
| 18 | +- Must appear **before** `export module` |
| 19 | +- Allowed here: |
| 20 | + - `#include` legacy headers |
| 21 | + - macros |
| 22 | + - conditional compilation |
| 23 | + - rare `import` |
| 24 | +- Example: |
| 25 | +```cpp |
| 26 | +module; |
| 27 | +#include <boost/config.hpp> |
| 28 | +#define INTERNAL_MACRO 1 |
| 29 | +``` |
| 30 | +
|
| 31 | +--- |
| 32 | +
|
| 33 | +## 3️⃣ Module Declaration |
| 34 | +```cpp |
| 35 | +export module my.module; |
| 36 | +``` |
| 37 | +- Marks start of module interface |
| 38 | +- Everything after is part of the module |
| 39 | +- Prefer `import` instead of `#include` inside |
| 40 | + |
| 41 | +--- |
| 42 | + |
| 43 | +## 4️⃣ Import Rules |
| 44 | +- ✅ After module: `import std;`, `import bar;` |
| 45 | +- ✅ Rarely allowed in global fragment |
| 46 | +- ❌ Forbidden before module line: |
| 47 | +```cpp |
| 48 | +import std; |
| 49 | +export module foo; // ❌ |
| 50 | +``` |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +## 5️⃣ Include Rules |
| 55 | +- ✅ Before `export module`: allowed (legacy headers) |
| 56 | +- ✅ After `export module`: content becomes part of module |
| 57 | +- ❌ System headers inside module body: may cause errors |
| 58 | +Use `import std;` instead |
| 59 | + |
| 60 | +--- |
| 61 | + |
| 62 | +## 6️⃣ Recommended Layout |
| 63 | +```cpp |
| 64 | +module; // Global fragment |
| 65 | +#include <legacy.hpp> // OK |
| 66 | +#define HELPER 1 // OK |
| 67 | + |
| 68 | +export module my.module; // Module interface begins |
| 69 | +import std; // Prefer imports |
| 70 | + |
| 71 | +export int foo(); |
| 72 | +#include "public_api.hpp" // Exported |
| 73 | +``` |
| 74 | + |
| 75 | +--- |
| 76 | + |
| 77 | +## 7️⃣ Header-Only Library Example |
| 78 | +```cpp |
| 79 | +module; |
| 80 | +#include <boost/config.hpp> |
| 81 | + |
| 82 | +#ifdef USE_STD_MODULE |
| 83 | +import std; |
| 84 | +#else |
| 85 | +#include <memory> |
| 86 | +#include <type_traits> |
| 87 | +#endif |
| 88 | + |
| 89 | +export module boost.any; |
| 90 | +#include <boost/any.hpp> // exported |
| 91 | +``` |
| 92 | + |
| 93 | +--- |
| 94 | + |
| 95 | +## 8️⃣ Quick Rules Summary |
| 96 | + |
| 97 | +| Phase | Allowed | |
| 98 | +|----------------------------|---------------------------------------| |
| 99 | +| Before `export module` | `#include`, macros, conditional, rare `import` | |
| 100 | +| After `export module` | `import`, includes become part of module interface | |
| 101 | +| Never | `import` before module line, system headers inside module body, macros leaking into exported API | |
| 102 | + |
| 103 | +**Rule of Thumb:** |
| 104 | +1. `module;` → legacy code |
| 105 | +2. `export module X;` → module interface |
| 106 | +3. `import` → after module |
| 107 | +4. Include only when you want it exported |
| 108 | + |
| 109 | +--- |
| 110 | + |
| 111 | +**Tip:** Follow this layout for Beman, Boost, fmt, JSON, or other header-only libraries to wrap them safely as modules. |
0 commit comments