`export` — C++ Keyword

`export` — C++ Keyword

The export keyword in C++20: marks declarations as visible to importers of a module.

How to use this reference page

Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.

  • Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
  • Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
  • Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.

export

Marks a declaration as part of a module's public interface, making it visible to translation units that import the module. Introduced in C++20.

Syntax

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

Example

// --- 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
}

Notes

Example in practice

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;
}