For five decades since 1972, C and C++ relied on textual header file inclusion via #include. C++20 Modules represent the biggest architectural upgrade to the compilation model in C++ history, eliminating macro pollution, header guards, and sluggish build times.
math_module.ixx)// Declare the module name
export module MathModule;
// Export specific functions to consumers
export double calculateInterest(double principal, double rate, int years) {
return principal * (rate / 100.0) * years;
}
main.cppimport MathModule;
import std; // C++23 standard library module
int main() {
double interest = calculateInterest(10000, 7.5, 3);
std::println("Calculated Interest: USD {:.2f}", interest); // 2250.00
return 0;
}
Boost your C++ proficiency by studying these closely connected core modules:
Review how legacy preprocessor headers operate.
Overview of C++20 and C++23 standards.
Another cutting-edge C++20 core language feature.