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 reasons, 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 math.math1 and math.math2. I used a point to separate the module math from its submodules. This point is not necessary.


Rainer D 6 P2 500x500Modernes C++ Mentoring

Be part of my mentoring programs:

  • "Fundamentals for C++ Professionals" (open)
  • "Design Patterns and Architectural Patterns with C++" (open)
  • "C++20: Get the Details" (open)
  • "Concurrency with Modern C++" (starts March 2024)
  • Do you want to stay informed: Subscribe.


    • 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 slight 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 only interested in 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 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 import more granular 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 the partitions export 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 challenging 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;

    Like 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 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 one 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 are more modules in C++20. For example, modules introduce header units, and they distinguish between global and private module fragments. 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, 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, Stephen Kelley, Kyle Dean, Tusar Palauri, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, moon, Philipp Lenk, Hobsbawm, Charles-Jianye Chen, and Keith Jeffery.

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

    My special thanks to Embarcadero
    My special thanks to PVS-Studio
    My special thanks to Tipi.build 
    My special thanks to Take Up Code
    My special thanks to SHAVEDYAKS


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

    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

    Online Seminars (German)

    Contact Me

    Modernes C++ Mentoring,



    0 replies

    Leave a Reply

    Want to join the discussion?
    Feel free to contribute!

    Leave a Reply

    Your email address will not be published. Required fields are marked *