Advertisement
Advanced C++, Compilation & Linking

What are C++20 modules and how do they improve on headers?

Short answer

Modules (C++20) are a new way to organize code that replaces textual #include with a compiled, importable unit (import mymod;).

Save C++ mock interview
Advertisement
AModel answer
Modules (C++20) are a new way to organize code that replaces textual #include with a compiled, importable unit (import mymod;). They avoid repeatedly re-parsing headers (faster builds), don't leak macros or private details, remove include-order fragility, and give better encapsulation. Adoption is gradual as tooling matures, but modules address longstanding header/preprocessor problems.
export module math;
export int add(int a, int b);
// user: import math;
Real-world example
Migrating heavy headers to modules cuts compile times by avoiding repeated header parsing.
Likely follow-ups
  • How do modules speed up builds? | What header problems do they solve?
Related topics