`module` — C++ Keyword
`module` — C++ Keyword
The module keyword in C++20: declares or introduces a named module.
`module` — C++ Keyword
The module keyword in C++20: declares or introduces a named module.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
moduleDeclares the current translation unit as a module unit in C++20's module system. Modules replace #include-based code sharing with an explicit, faster, and more hygienic mechanism.
module; // global module fragment (for #includes)
export module name; // primary module interface unit
module name; // module implementation unit
export module name : partition; // module partition interface
module name : partition; // module partition implementation
// --- math.cppm (module interface unit) ---
export module math;
export int add(int a, int b) { return a + b; }
export double square(double x) { return x * x; }
// --- main.cpp ---
import math;
#include <print>
int main() {
std::println("{}", add(3, 4)); // 7
std::println("{:.1f}", square(5.0)); // 25.0
}
.cppm or .ixx extensions (toolchain-dependent).module; fragment before the module declaration allows legacy #include headers that are not yet modularized.int main() {
// Pick one facility from this reference page.
// Write the smallest program that exercises its main precondition,
// complexity rule, or lifetime constraint before scaling up.
return 0;
}