`import` — C++ Keyword

`import` — C++ Keyword

The import keyword in C++20: imports a named module or header unit into the current translation unit.

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.

import

Makes the exported names of a named module or a header unit available in the current translation unit. Introduced in C++20.

Syntax

import module-name;
import <header>;         // import standard library header as header unit
import "header.h";       // import local header as header unit

Example

// --- greeting.cppm ---
export module greeting;

export void say_hello(const char* name);

// --- greeting.cpp ---
module greeting;
#include <print>

void say_hello(const char* name) {
    std::println("Hello, {}!", name);
}

// --- main.cpp ---
import greeting;

int main() {
    say_hello("World");   // Hello, World!
}

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