Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The export keyword in C++20 modules dictates the external linkage and visibility of entities within a module interface unit. It explicitly designates which declarations and definitions are accessible to other translation units that import the module, establishing the module’s public interface while keeping unexported entities strictly encapsulated within the module’s purview.

Primary Module Interface Declaration

To define a translation unit as the primary interface for a module, the export keyword must precede the module keyword. This must be the first non-comment, non-preprocessor line in the file (following the optional global module fragment).
export module core_system;

Exporting Declarations

You can export entities such as functions, classes, variables, templates, and aliases. The export specifier can be applied to individual declarations, grouped in blocks, or applied to entire namespaces.
export module core_system;

// 1. Single Declaration Export
export void initializeSystem();
export struct SystemConfig { int threads; };

// 2. Block Export
export {
    int getSystemStatus();
    constexpr int MAX_THREADS = 16;
    template<typename T> class SystemQueue { /* ... */ };
}

// 3. Namespace Export
// Exports all declarations contained within the namespace
export namespace sys_utils {
    void logEvent();
    class Logger;
}

Exporting Imports (Transitive Inclusion)

A module can re-export another module or header unit. Any translation unit that imports the current module will implicitly import the re-exported module.
export module core_system;

// Consumers of 'core_system' will also see the exported entities of 'memory_pool'
export import memory_pool; 

// Consumers will also see the contents of the standard header
export import <vector>; 

Technical Constraints and Linkage Rules

  1. Namespace Scope Requirement: The export keyword can only appear at namespace scope (either the global namespace or a named namespace). It cannot be used at block scope (inside functions) or class scope (inside class definitions).
export class Manager {
    export void doWork(); // ERROR: Cannot export class members individually
};
  1. Linkage Restrictions: You cannot export entities that possess internal linkage. This includes variables or functions marked static, or entities declared within an unnamed (anonymous) namespace.
export static int internalCounter = 0; // ERROR: internal linkage

export namespace {
    void helperFunction(); // ERROR: unnamed namespace implies internal linkage
}
  1. Declaration Order: If an entity is exported, its first declaration in the translation unit must contain the export specifier. Subsequent declarations or definitions of that same entity in the same translation unit do not require the export keyword, though it is permitted.
export void process(); // First declaration must be exported
void process() { ... } // Definition does not need the keyword
  1. Implicit Exports: When a class or struct is exported, all of its public, protected, and private members are implicitly exported. However, private and protected members remain subject to standard C++ access control rules; they are visible to the compiler in the importing translation unit (for memory layout and instantiation purposes) but cannot be accessed directly.
Master C++ with Deep Grasping Methodology!Learn More