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 artefacts: 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.


Rainer D 6 P2 540x540Modernes C++ Mentoring

Stay informed about my mentoring programs.



Subscribe via E-Mail.

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 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: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, and Dominik Vošček.


Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, and Slavko Radman.



My special thanks to Embarcadero CBUIDER STUDIO FINAL ICONS 1024 Small


My special thanks to PVS-Studio PVC Logo


My special thanks to Tipi.build tipi.build logo


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

Bookable (Online)


Standard Seminars (English/German)

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

  • C++ - The Core Language
  • C++ - The Standard Library
  • C++ - Compact
  • C++11 and C++14
  • Concurrency with Modern C++
  • Design Pattern and Architectural Pattern with C++
  • Embedded Programming with Modern C++
  • Generic Programming (Templates) with C++


  • Clean Code with Modern C++
  • C++20

Contact Me

Modernes C++,


Tags: Modules


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?



Stay Informed about my Mentoring


English 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

Interactive Course: The All-in-One Guide to C++20

Subscribe to the newsletter (+ pdf bundle)

All tags

Blog archive

Source Code


Today 5076

Yesterday 7929

Week 20893

Month 165064

All 11646218

Currently are 176 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments