Modules (C++20)

Modules are a modern alternative to the traditional header/include model. They improve build scalability and reduce accidental macro leakage.

Basic Syntax

// math.ixx
export module math;

export int add(int a, int b) {
  return a + b;
}
// main.cpp
import math;
#include <iostream>

int main() {
  std::cout << add(2, 3) << "\n";
}

Why Modules

Practical Note

Compiler and build-system support is improving, but setup differs across toolchains. For course assignments, headers are still fine unless module support is explicitly required.

[!WARNING] Module support can be compiler-version and build-tool specific. Always verify flags and toolchain docs.