C++20: Structure Modules


When your module becomes bigger you want to divide its functionality into manageable components. C++20 modules offer two approaches: submodules and partitions. Let me discuss both approaches in this post.


Before I start I want to make a short disclaimer. For simplicity reason, I ignore in this post the separation of the module interface unit and the module implementation unit. This means I define each module in one file. Additionally, I don't use namespaces. I described both features, which you should use, in my previous post "C++20: Module Interface Unit and Module Implementation Unit".

The idea of a submodule is straightforward. Consequently, I start with them.


A module can import modules and then re-export them.

The module math imports in the following example the submodules math.math1 and math.math2.

  • Module math
// mathModule.ixx

export module math;

export import math.math1;
export import math.math2;


The expression export import math.math1 imports the module math.math1 and re-exports it as part of the module  math.

For completeness, here are the modules math.math1 and math.math2. I used a point to separate the module math from its submodules. This point is not necessary.

  • Submodule math.math1
// mathModule1.ixx

export module math.math1;          // (1)   

export int add(int fir, int sec) { // (2)
    return fir + sec;


  • Submodule math.math2
// mathModule2.ixx

export module math.math2;     // (1)   

export {                      // (2)
    int mul(int fir, int sec) {
        return fir * sec;


If you look carefully, you recognize a small difference in the export statements (2) in the modules math.math1 and math.math2. math.math1 uses an export specifier and math.math2 as a so-called export group or export block.

From the client's perspective, using the math module is straightforward.

  • Client program
// mathModuleClient.cpp

import std.core;
import math;

int main() {

    std::cout << std::endl;

    std::cout << "add(3, 4): " << add(3, 4) << std::endl;
    std::cout << "mul(3, 4): " << mul(3, 4) << std::endl;


Compiling, linking, and executing the program works as expected with the Microsoft implementation of modules:


cl.exe /std:c++latest /c /experimental:module mathModule1.ixx /EHsc /MD  // (3)
cl.exe /std:c++latest /c /experimental:module mathModule2.ixx /EHsc /MD  // (3)
cl.exe /std:c++latest /c /experimental:module mathModule.ixx /EHsc /MD   // (3)
cl.exe /std:c++latest /experimental:module mathModuleClient.cpp mathModule1.obj mathModule2.obj mathModule.obj /EHsc /MD // (4)


Each compilation process (3) produces two artifacts: The IFC file (interface file ) *.ifc, which is implicitly used in (4), and the  *.obj file, which is explicitly used in (4).

I already mentioned that a submodule is just a module. Each submodule has a module declaration (1). Consequently, I can create a second client that is only interested in the math.math1 module.

  •  Second client program
// mathModuleClient1.cpp

import std.core;
import math.math1;

int main() {

    std::cout << std::endl;

    std::cout << "add(3, 4): " << add(3, 4) << std::endl;


It's sufficient to compile the new client program and link it. The existing module math.math1 just works fine.

cl.exe /std:c++latest /experimental:module mathModuleClient1.cpp mathModule1.obj  /EHsc /MD



The division of modules into modules and submodules is a means for the module designer to give the user of the module the possibility to more granular import parts of the module. This observation does not apply to module partitions.

Module Partitions

A module can be divided into partitions. Each partition consists of a module interface unit (partition interface file) and zero or more module implementation units (see "C++20: Module Interface Unit and Module Implementation Unit"). The names, which the partitions exports are imported and re-exported by the primary module interface unit (primary interface file). The name of a partition must begin with the name of the module. The partitions can not exist on their own.

The description of module partitions is more difficult to understand than its implementation. In the following lines, I rewrite the math module and its submodules math.math1 and math.math2 to module partitions. In this straightforward process, I refer to the shortly introduced terms of module partitions.

  •  Primary interface file mathPartition.ixx


// mathPartition.ixx

export module math;   // (1)

export import :math1; // (2)
export import :math2; // (2)


The primary interface file consists of the module declaration (1). It imports and re-exports the partitions math1 and math2 using colons (2). The name of the partitions must begin with the name of the module. Consequently, you don't have to specify them.

  • Module partitions (mathPartition1.ixx, and mathPartition2.ixx)
// mathPartition1.ixx

export module math:math1;     // (1)   

export int add(int fir, int sec) {
    return fir + sec;


// mathPartition2.ixx

export module math:math2;     // (1)   

export { 
    int mul(int fir, int sec) {
        return fir * sec;


Similar to the module declaration, (1) declares a module interface partition. A module interface partition is also a module interface unit. The name math stands for the module and the name math1 or math2 for the partition.

  • Client program
// mathModuleClient.cpp

import std.core;
import math;

int main() {

    std::cout << std::endl;

    std::cout << "add(3, 4): " << add(3, 4) << std::endl;
    std::cout << "mul(3, 4): " << mul(3, 4) << std::endl;

You may have already assumed it: the client program is identical to the client program I previously used with submodules. The same observation holds for the creation of the executable.

cl.exe /std:c++latest /c /experimental:module mathPartition1.ixx /EHsc /MD
cl.exe /std:c++latest /c /experimental:module mathPartition2.ixx /EHsc /MD
cl.exe /std:c++latest /c /experimental:module mathPartition.ixx /EHsc /MD
cl.exe /std:c++latest /experimental:module mathModuleClient.cpp mathPartition1.obj mathPartition2.obj mathPartition.obj /EHsc /MD


What's next?

There is more to modules in C++20. For example, modules introduce header units, and they distinguish between global and private module fragment. Finally, I want to write about linkage.



Thanks a lot to my Patreon Supporters: Meeting C++, Matt Braun, Roman Postanciuc, Venkata Ramesh Gudpati, Tobias Zindl, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Markus Falkner, Darshan Mody, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Wolfgang Gärtner, Jon Hess, Christian Wittenhorst, Louis St-Amour, Stephan Roslen, Venkat Nandam, Jose Francisco, Douglas Tinkham, and Lakshman.


Thanks in particular to: Bitwyre Technologies


Thanks in particular to:   crp4



I'm happy to give online-seminars or face-to-face seminars world-wide. Please call me if you have any questions.

Bookable Seminars (Online)

Standard Seminars 

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.

Contact Me

Modernes C++,


Tags: modules, C++20


0 #1 Program Studio 2020-06-01 12:25
Thank you for providing information about data structure.
0 #2 Klaus 2020-06-08 07:17
Many thanks for this, Rainer!
I have two additional questions:
There are many hints regarding performance benefits during compilation. However, I am very interested in seeing any insights regarding run-time performance.

Is there any change regarding the linkage process for DLLs?


My Newest E-Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code


Today 2684

Yesterday 5163

Week 47841

Month 202436

All 4823330

Currently are 119 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments