> ## 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.

# C++ Module Declaration

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 the `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.

```cpp theme={"dark"}
export module core.network;

export void connect(); // Visible to importers
void internal_setup(); // Hidden from importers
```

* The `export module` keywords 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.

```cpp theme={"dark"}
module core.network;

// Implicitly imports the primary interface of 'core.network'
void internal_setup() {
    // Implementation details
}
```

* The `module` keyword (without `export`) 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.

```cpp theme={"dark"}
export module core.network:sockets;

export class Socket { /* ... */ };
```

**Implementation Partition**
Organizes internal implementation details. It cannot be exported and is only visible to other units within the same module that explicitly import it.

```cpp theme={"dark"}
module core.network:buffers;

class BufferManager { /* ... */ };
```

## 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.

```cpp theme={"dark"}
module;

#include <vector> // Expands into declarations attached to the global scope
#include <string>

export module core.data;

export std::vector<std::string> process_data();
```

* Declared using the global module fragment introducer, a syntax construct consisting of the `module` keyword 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.

```cpp theme={"dark"}
export module math.engine;

export int calculate(int a, int b);

module :private; // Ends the exported interface

// Everything below is unreachable by importers
int calculate(int a, int b) {
    return a + b;
}
```

* 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

1. **Global Scope:** A module declaration must appear at the global namespace scope. It cannot be nested inside a `namespace`, `class`, or function.
2. **Singularity:** A translation unit can contain at most one module declaration (excluding the `module;` global fragment and `module :private;` fragment).
3. **Ordering:** The module declaration must be the first declaration in the translation unit, except for declarations introduced within the global module fragment.
4. **Attributes:** The C++ grammar allows an optional attribute specifier sequence on a module declaration. Attributes cannot precede the `module` or `export` keywords; 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.

```cpp theme={"dark"}
// Correct attribute placement for an implementation-defined attribute.
// Standard attributes like [[deprecated]] are ill-formed here.
export module core.network [[vendor::specific_attribute]];
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C++ Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
