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
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.
- Submodule math.math1
- Submodule math.math2
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
Compiling, linking, and executing the program works as expected with the Microsoft implementation of modules:
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
It’s sufficient to compile the new client program and link it. The existing module math.math1 works fine.
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.
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
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)
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
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.
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, 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, Rob North, Bhavith C Achar, and Marco Parri Empoli.
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|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
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++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,