For decades, C++ programmers have been accustomed to organising reusable code around header files (.h, .hpp) and implementation files (.cpp). We write declarations in headers, include them wherever required, and let the preprocessor and compiler do their work.
It is a familiar model, but it has its limitations. Repeated header processing, macro pollution, complicated dependencies, accidental exposure of implementation details and cascading recompilation have been persistent challenges in large C++ projects.
C++20 Modules introduce something more fundamental than a new way of writing #include.
They encourage us to move from file-oriented code organisation towards explicit, language-level software interfaces and dependencies.
I recently experimented with this paradigm by implementing the routing of commands (task) (Command Design Pattern) along the chain of Chain of Responsibility design pattern using C++20 Modules. The exercise turned out to be an interesting way to understand what this change means for C++ programmers.
From #include to import
Consider the traditional approach:
#include "level.h"
#include "routing_core.h"The preprocessor effectively inserts the contents of these headers into the source file. The compiler subsequently processes the resulting translation unit.
Now consider the module-based approach:
import routing.level;
import routing.core;This is not textual inclusion. The compiler consumes compiled module interface information, commonly called a Binary Module Interface (BMI).
The distinction is important. A module is a language-level entity with a name, an interface, and explicitly declared dependencies. It is no longer simply a collection of declarations inserted into another source file.
Here's the source code of my work...
level.cppm
export module routing.level;
export enum class Level {
Level1,
Level2,
Level3
};
export enum class CommandType {
Tea,
Coffee
};
routing_core.cppm
module;
#include <memory>
#include <string>
#include <iostream>
export module routing.core;
import routing.level;
// Match the export status of the definitions below
export class Task;
export class Receiver;
export class Task {
private:
CommandType type;
std::string description;
Level level;
Receiver* receiver;
public:
Task(CommandType inType, std::string inDescription, Level inLevel, Receiver* inReceiver);
~Task() = default;
void execute();
CommandType getCommandType() const { return type; }
Level getLevel() const { return level; }
std::string getDescription() const { return description; }
Receiver* getReceiver() const { return receiver; }
};
export class Receiver {
protected:
std::unique_ptr<Receiver> successor;
public:
Receiver() = default;
explicit Receiver(std::unique_ptr<Receiver> inSuccessor)
: successor(std::move(inSuccessor)) {}
virtual ~Receiver() = default;
virtual bool handleCommandImpl(Task* command) = 0;
void handleCommand(Task* command) {
bool handledAtThisNode = handleCommandImpl(command);
if (!handledAtThisNode && successor) {
successor->handleCommand(command);
}
}
Receiver* getSuccessor() const { return successor.get(); }
};
export class ReceiverLevel1 : public Receiver {
public:
explicit ReceiverLevel1(std::unique_ptr<Receiver> inSuccessor = nullptr)
: Receiver(std::move(inSuccessor)) {}
bool handleCommandImpl(Task* command) override;
};
export class ReceiverLevel2 : public Receiver {
public:
explicit ReceiverLevel2(std::unique_ptr<Receiver> inSuccessor = nullptr)
: Receiver(std::move(inSuccessor)) {}
bool handleCommandImpl(Task* command) override;
};
export class ReceiverLevel3 : public Receiver {
public:
explicit ReceiverLevel3(std::unique_ptr<Receiver> inSuccessor = nullptr)
: Receiver(std::move(inSuccessor)) {}
bool handleCommandImpl(Task* command) override;
};
routing_core.cpp
module;
#include <string>
#include <utility>
#include <iostream>
module routing.core;
// --- Task Implementations ---
Task::Task(CommandType inType, std::string inDescription, Level inLevel, Receiver* inReceiver)
: type(inType), description(std::move(inDescription)), level(inLevel), receiver(inReceiver) {}
void Task::execute() {
if (receiver) {
receiver->handleCommand(this); // Types match 100% perfectly now!
}
}
// --- Receiver Level Implementations ---
bool ReceiverLevel1::handleCommandImpl(Task* command) {
if (command->getLevel() == Level::Level1) {
std::cout << "[Receiver Level 1] Handled command: " << command->getDescription() << "\n";
return true;
}
std::cout << "[Receiver Level 1] Passing along...\n";
return false;
}
bool ReceiverLevel2::handleCommandImpl(Task* command) {
if (command->getLevel() == Level::Level2) {
std::cout << "[Receiver Level 2] Handled command: " << command->getDescription() << "\n";
return true;
}
std::cout << "[Receiver Level 2] Passing along...\n";
return false;
}
bool ReceiverLevel3::handleCommandImpl(Task* command) {
if (command->getLevel() == Level::Level3) {
std::cout << "[Receiver Level 3] Handled command: " << command->getDescription() << "\n";
return true;
}
std::cout << "[Receiver Level 3] End of chain. Command unhandled: " << command->getDescription() << "\n";
return false;
}
main.cpp
#include <memory>
#include <iostream>
import routing.level;
import routing.core;
int main() {
auto chain = std::make_unique<ReceiverLevel1>(
std::make_unique<ReceiverLevel2>(
std::make_unique<ReceiverLevel3>()
)
);
Task task1(CommandType::Tea, "Morning Tea", Level::Level1, chain.get());
Task task2(CommandType::Coffee, "Afternoon Espresso", Level::Level2, chain.get());
std::cout << "--- Executing Task 1 ---\n";
task1.execute();
std::cout << "\n--- Executing Task 2 ---\n";
task2.execute();
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.30)
project(CommandAndChainOfResponsibility_Module CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
add_executable(CommandAndChainOfResponsibility_Module
main.cpp
routing_core.cpp
)
target_sources(CommandAndChainOfResponsibility_Module
PUBLIC
FILE_SET cxx_modules TYPE CXX_MODULES FILES
level.cppm
routing_core.cppm
)
My experiment: Command and Chain of Responsibility
The example implements a simple task-routing mechanism.
A task enters a chain of three receivers. Each receiver checks whether it can handle the task. If it cannot, the task moves to the next receiver.
The architecture is straightforward:
Taskrepresents the command.Receiverdefines the common handling mechanism.ReceiverLevel1,ReceiverLevel2andReceiverLevel3provide concrete implementations.
The interesting part, however, is how these components are organised using Modules.
1. A separate module for common types
The file level.cppm defines:
export module routing.level;
export enum class Level {
Level1, Level2, Level3
};
export enum class CommandType {
Tea, Coffee
};The export module declaration establishes a named module, while export makes selected declarations available to importing code.
The module becomes the owner of these common types.
2. The core module
The file routing_core.cppm declares:
export module routing.core;
import routing.level;This establishes an explicit dependency: routing.core depends on routing.level.
The module exports the Task, Receiver and concrete receiver classes. Their public declarations form the interface through which other components interact with the routing system.
The actual function implementations are separated into routing_core.cpp:
module routing.core;Notice that this is an implementation unit of the existing module, not another module.
The application therefore does not need to know how the receivers process commands internally. It only needs the exported interface.
3. The application
The main program consumes the modules:
import routing.level;
import routing.core;It creates the receiver chain using std::unique_ptr:
auto chain = std::make_unique<ReceiverLevel1>(
std::make_unique<ReceiverLevel2>(
std::make_unique<ReceiverLevel3>()
)
);Tasks are created and executed through the chain.
The Command and Chain of Responsibility patterns remain conceptually unchanged. What changes is the way their software components are packaged, exposed and consumed.
That distinction is central to understanding Modules.
The real paradigm shift
I see four particularly important changes.
1. From textual inclusion to explicit dependencies
With traditional headers, dependencies are often established indirectly through a chain of #include directives. Modules make these relationships explicit through import.
The compiler and build system can understand the dependency graph and arrange module compilation accordingly.
2. From implicit visibility to deliberate interfaces
In the header model, including a file makes its declarations available to the translation unit.
Modules introduce explicit exports. A programmer decides which declarations form the public interface and which remain internal.
This encourages better API design and more disciplined encapsulation.
3. From file organisation to component-oriented architecture
A file is a physical organisation mechanism. A module is a logical software component.
Instead of thinking only in terms of Task.h, Receiver.h and their corresponding implementation files, we can think in terms of components such as:
routing.level
routing.core
routing.applicationThis becomes particularly valuable as software grows.
4. A better foundation for compilation efficiency
Traditional headers can be processed repeatedly across translation units. Modules allow compilers to reuse compiled interface information.
This can reduce redundant parsing and improve build times, although actual improvements depend on the compiler, project structure, and build configuration.
What this means for future C++ development
Imagine developing a scientific computing or graphics framework. Its logical components might be:
simulation.math
simulation.physics
simulation.collision
simulation.graphics
simulation.applicationThe physics module can depend on mathematics. Graphics can expose a rendering interface without exposing all its internal OpenGL implementation details. The application imports the components it needs.
This is a much more deliberate way of thinking about software architecture.
Of course, Modules do not replace namespaces, classes, templates, the linker or libraries. Nor do they automatically make poorly designed software modular. They provide language-level mechanisms that make explicit interfaces and dependencies easier to express.
The transition will also be gradual. Existing C++ headers and libraries remain relevant, and modern projects can mix #include and import while migrating.
A change in the way we think
What makes C++20 Modules interesting to me is not merely the introduction of a new keyword called import.
It is the opportunity to reconsider how we structure reusable software.
For years, C++ programmers have worked around the limitations of the header-based model using forward declarations, include guards, carefully managed dependencies and elaborate build configurations.
Modules offer a more direct language-level approach to defining software boundaries.
My small Command and Chain of Responsibility experiment illustrates this nicely: the design patterns remain the same, but their interfaces, dependencies and implementation are organised as explicit software components.
Perhaps the most significant change is this: instead of thinking primarily about which files need to include which other files, we can increasingly think about which software components depend on which interfaces.
That, in my opinion, is the real paradigm shift that C++20 Modules bring to modern C++ programming.
And here's the original write-up of this basic design pattern concept for routing commands along the chain of responsibility...





