C++ Tutorial

Modules (C++20)

The modern replacement for header (#include) files

What are C++ Modules?

Introduced in C++20, Modules are a huge update to how C++ code is organized and compiled. Prior to this, C++ relied on the pre-processor and #include headers, which were slow, prone to name collisions, and very repetitive.
Modules solve these problems by allowing you to export specific entities (like functions or classes) from one file and import them into another. This is much more modern and can dramatically speed up compilation times!
cpp
// math.ixx (Module Define!)
export module Math;

export int add(int a, int b) {
    return a + b;
}

Comparisons: Header File (#include) vs. Module (import)

FeatureHeader FileModule
LogicText copy-paste by pre-processorPre-compiled binary representation
SpeedSlow (Same code re-compiled every time!)FAST (Compiled once and reused!)
PrivacyEverything in a header is "public"Only things with export are "public."
OrderInclude order matters!Import order doesn't matter.

How to use Modules

Wait! Most modern C++ projects use a mixture of old headers and new modules while transitioning.
  1. Define: Use the export module keyword at the top of a file.
  2. Export: Put export in front of any function or class you want others to use.
  3. Import: Use import to bring that logic into another file.
cpp
import Math; // Brining in our custom module!

int main() {
    int result = add(5, 10);
    return 0;
}

Why Use Modules?

  1. Speed: Massive improvement in build times for large-scale projects.
  2. Isolation: Prevents "Macro Bleeding" (Where a macro in one file randomly breaks another file!).
  3. Encapsulation: It's much easier to hide internal logic that people shouldn't be using.

Summary

FeatureSyntaxPurpose
Moduleexport moduleDefine the module's identity
Exportexport int f()Make an entity public
Importimport MLoad a pre-compiled module
Best ForCode separation, compilation performance
ModernC++20 required!
BinaryModules are stored in a special binary format
PrivateCode inside a module is hidden unless exported
HeaderModules can also wrap old header files!
LibraryStandard library is now available as std module!
WaitUse import std; in C++23 for everything!
StandardReplacing the fragile #include system
SafetyNo risk of circular dependencies between modules!
CleanNo more manual #ifndef header guards!
ToolingRequires modern compilers and build systems
Speed5x to 10x faster builds in some cases!
ScopeModules provide true code isolation!
PathMost build tools use .ixx or .cppm for modules
LegacyFully compatible with existing #include code
FutureThe primary way all future C++ code will be written
NamespaceModules and Namespaces work perfectly together
PartitionSplit a single module into multiple files (Math:Add)