Learn C++
#include "advanced/modules.hpp"

Modules (C++20)

//concept

Modules replace the textual #include model: export module math; declares a module whose exported names import cleanly (import math;) without re-parsing headers, leaking macros, or depending on include order. Builds get dramatically faster on large codebases, and macro leakage across boundaries finally ends.

Adoption is still ramping — GCC 14+, Clang 16+, and MSVC support modules with growing CMake integration, but many libraries remain header-only in practice. New greenfield code on recent toolchains can start modular; existing projects migrate gradually, mixing #include and import during transition.

//how it works
1

Export once, import fast

export module math; plus export-marked declarations form the interface — importers get compiled metadata, not re-parsed text.

2

No order dependence

Import order doesn't matter and macros don't leak across module boundaries — two historic header pains, gone.

3

Partitions organize

module math:core splits big modules into separately-authored partitions under one importable name.

4

Check toolchain maturity

Compiler plus build-system support is still uneven — verify your exact versions before committing a codebase.

//example
// math.cppm
export module math;
export int triple(int x) { return x * 3; }
// ---- app.cpp ----
// import math;
// int main() { return triple(7); } // 21
Output
21
1The .cppm file declares module math and exports triple — the interface is explicit, not inferred from headers.
2The consumer imports the compiled interface by name; no include path or order juggling.
3triple(7) evaluates to 21 through the module boundary with full type checking.
//use cases
Slashing rebuild times on header-heavy codebases
Ending macro leakage and include-order fragility
Structuring large libraries as partitioned modules
Mixing import and #include pragmatically during migration

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C++ yourself — building beats watching.