A C++ module declaration establishes a translation unit as a module unit or a module partition, dictating its linkage, visibility, and reachability boundaries. Introduced in C++20, it replaces the traditional preprocessor-based inclusion model with a semantic, compiler-enforced boundary using theDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
module and export keywords.
The module declaration must be the first declaration in a translation unit, optionally preceded by the global module fragment. Comments and preprocessor directives are resolved prior to the parsing of declarations and do not violate this ordering requirement.
Primary Module Interface Declaration
The primary module interface defines the public surface of a module. A module must have exactly one primary interface translation unit.- The
export modulekeywords designate this file as the primary interface. - Module names consist of one or more identifiers separated by dots (e.g.,
core.network). The dots represent logical hierarchy, not physical file paths.
Module Implementation Declaration
A module implementation unit provides the definitions for declarations made in the module interface. It does not export anything and is not imported by consumers.- The
modulekeyword (withoutexport) binds this translation unit to the named module. - Declarations within this unit have module linkage by default, meaning they are visible across the module but hidden from outside translation units.
Module Partitions
A module can be divided into multiple translation units called partitions. A partition name is appended to the module name, separated by a colon (:).
Interface Partition
Contributes to the public interface of the module. It must be explicitly exported by the primary module interface.
Global Module Fragment
The global module fragment allows a module unit to include legacy header files without attaching their contents to the module’s named scope or ownership.- Declared using the global module fragment introducer, a syntax construct consisting of the
modulekeyword followed by a semicolon punctuator (module;). - Can appear at the beginning of any module unit. This includes primary module interfaces, module implementations, module interface partitions, and module implementation partitions.
- It is designed to house preprocessor directives (like
#include), which expand into standard C++ declarations that exist prior to the module declaration.
Private Module Fragment
The private module fragment allows a single-file module to hide implementation details from importers without requiring a separate implementation file.- Declared using
module :private;. - Can only be used in a primary module interface unit that does not have separate module implementation units or partitions.
Structural Constraints
- Global Scope: A module declaration must appear at the global namespace scope. It cannot be nested inside a
namespace,class, or function. - Singularity: A translation unit can contain at most one module declaration (excluding the
module;global fragment andmodule :private;fragment). - Ordering: The module declaration must be the first declaration in the translation unit, except for declarations introduced within the global module fragment.
- Attributes: The C++ grammar allows an optional attribute specifier sequence on a module declaration. Attributes cannot precede the
moduleorexportkeywords; they must appear at the end of the declaration, immediately preceding the semicolon. However, the C++ standard strictly restricts standard attributes (such as[[deprecated]]or[[nodiscard]]) to specific entities, and module declarations are not among them. Applying a standard attribute to a module declaration renders the program ill-formed. This syntax exists solely to support implementation-defined (compiler-specific) attributes.
Master C++ with Deep Grasping Methodology!Learn More





