Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

module — module declarations

`module` specifies that a translation unit belongs to a named module or starts a module fragment. Distinguish `export module M;`, `module M;`, and `import M;`: they declare an interface, an implementation, and use of a module, respectively.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

export module M;
module M;
export module M:Part;
module M:Part;
module;
module :private;

module is recognized as a keyword in module syntax, rather than unconditionally like int. export module M; starts the primary interface unit, whereas module M; starts an implementation unit. A named module has exactly one primary interface unit. A module is not a namespace: the name M alone does not create a qualifier M::.

export module M:Part; declares an interface partition, and module M:Part; declares an implementation partition. Partitions can be imported only within the same module. Code consuming the interface writes import M;, not module M;. Declarations intended for name lookup by importers are marked export; importing a named module does not make its macros available.

module; starts a global module fragment before the named module declaration, useful for including traditional headers. module :private; starts a private fragment whose declarations are not reachable by importers. It is permitted only in the primary interface unit; a module containing such a fragment cannot have other module units.

The example consists of two separate files. answer exports the function value, and main.cpp checks the result at compile time and runtime. The build must make the compiled interface available to the importer; do not assume compiling the concatenated text is sufficient. This entry targets C++23; the referenced site contains an evolving working draft, not a frozen C++23 edition. A repository compiler flag does not attest operation in a production runtime.

Examples

// answer.cppm
export module answer;
export constexpr int value() { return 42; }

// main.cpp
import answer;
static_assert(value() == 42);

int main() {
    return value() == 42 ? 0 : 1;
}

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources