`export` — C++ Keyword
`export` — C++ Keyword
The export keyword in C++20: marks declarations as visible to importers of a module.
`export` — C++ Keyword
The export keyword in C++20: marks declarations as visible to importers of a module.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
exportMarks a declaration as part of a module's public interface, making it visible to translation units that import the module. Introduced in C++20.
export declaration; // export a single declaration
export { declarations } // export block
export module name; // introduce a module interface unit
export import partition; // re-export a module partition
// --- shapes.cppm ---
export module shapes;
// Export individual declarations
export struct Point { double x, y; };
export double distance(Point a, Point b);
// Export block
export {
struct Circle {
Point center;
double radius;
};
double area(Circle c);
}
// Internal helper – NOT exported
static double sq(double x) { return x * x; }
// --- shapes.cpp ---
module shapes;
#include <cmath>
double distance(Point a, Point b) {
return std::sqrt(sq(a.x - b.x) + sq(a.y - b.y));
}
double area(Circle c) {
return 3.14159265 * sq(c.radius);
}
// --- main.cpp ---
import shapes;
#include <print>
int main() {
Point p1{0, 0}, p2{3, 4};
std::println("{}", distance(p1, p2)); // 5
}
export module) can be exported.constexpr functions can all be exported.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;
}