All Recipes
Home/Modules/Module Partitions
📦intermediate

Module Partitions

Organize large modules into partitions

Example Code

cpp
// geometry.cppm - Primary module interface
export module geometry;
export import :shapes; // Re-export shapes partition
export import :utils; // Re-export utils partition
// geometry-shapes.cppm - Shapes partition
export module geometry:shapes;
export struct Point {
double x, y;
};
export struct Circle {
Point center;
double radius;
};
export struct Rectangle {
Point topLeft;
double width, height;
};
// geometry-utils.cppm - Utils partition
export module geometry:utils;
import :shapes;
export double distance(Point a, Point b) {
double dx = b.x - a.x;
double dy = b.y - a.y;
return std::sqrt(dx*dx + dy*dy);
}
export double area(const Circle& c) {
return 3.14159 * c.radius * c.radius;
}
export double area(const Rectangle& r) {
return r.width * r.height;
}
// main.cpp
import geometry;
#include <iostream>
int main() {
Circle c{{0, 0}, 5};
Rectangle r{{0, 0}, 10, 20};
std::cout << "Circle area: " << area(c) << std::endl;
std::cout << "Rectangle area: " << area(r) << std::endl;
return 0;
}

Explanation

Module partitions allow splitting a module into multiple files while maintaining a single logical module. The primary interface uses 'export import :partition;' to re-export partition contents.

Key Points

  • 1Partitions use ':name' syntax
  • 2'export import' re-exports symbols
  • 3One primary interface per module
  • 4Partitions enable better organization