Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

import — importing modules and header units

`import` makes a named module's exported interface, a partition of the current module, or a header unit available. It does not textually paste contents like `#include`; importing a named module does not make macros available.

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

Syntax

import module_name;
import :partition_name;
import <header>;
import "header";
export import module_name;

import arithmetic; imports a named module's interface: its exported names become available to name lookup after the import declaration. Importing neither creates a namespace nor textually pastes a file. A module name is not a filename.

import :partition_name; imports a partition of the same module and may appear only after its module declaration. import <header>; and import "header"; import a header unit, a separate translation unit formed from an importable header. Directly importing a header unit can make macros available; importing a named module does not.

An import declaration must inhabit global namespace scope, not a function or a named namespace. In a module unit, imports must precede other declarations in the relevant declaration sequence. export import module_name; in a module interface re-exports the import, making it available to consumers of that interface. Cyclic interface dependencies are forbidden.

The example consists of two files: arithmetic.cppm exports square, and main.cpp imports the module and checks the result at compile time. It prints nothing and should exit with status 0. The compiler and build system must support modules and their dependencies; a C++23 flag alone does not attest successful building or execution. The supplied site contains an evolving working draft, not the frozen C++23 text.

Examples

// arithmetic.cppm
export module arithmetic;
export constexpr int square(int x) { return x * x; }

// main.cpp
import arithmetic;
static_assert(square(6) == 36);
int main() {
    return square(6) == 36 ? 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