C++20: A Simple math Module
Modules are one of the four prominent features of C++20. They overcome the restrictions of header files and promise a lot: faster build-times, fewer violations of the One-Definition-Rule, less usage of the preprocessor. Today, I want to create a simple math module.
The Long History of Modules in C++
Modules may be older than you think. My short historic detour should give only an idea of how long it takes to get something such valuable into the C++ standard.
In 2004, Daveed Vandevoorde wrote the proposal N1736.pdf, describing the first-time module idea. It took until 2012 to get a dedicated Study Group (SG2, Modules) for modules. In 2017, Clang 5.0 and MSVC 19.1 provided the first implementation. One year later, the Modules TS (technical specification) was finalized. Around the same time, Google proposed the so-called ATOM (Another Take On Modules) proposal (P0947) for modules. In 2019, the Modules TS and the ATOM proposal were merged into the C++20 committee draft (N4842), the syntax I present in my posts to modules.
The C++ standardization process is democratic. Section Standardization gives you more information about the standard and the standardization process. The image to the right shows the various study groups.
Explaining modules from the user’s perspective is quite easy, but this will not hold for the implementer’s perspective. My plan for this post is to start with simple modules math and add more features to it as we go.
The math Modul
First, here is my first module:
// math.ixx export module math; export int add(int fir, int sec){ return fir + sec; }
The expression export module math is the module declaration. By putting export before the function adds, add is exported and can, therefore, be used by a consumer of my module.
// client.cpp import math; int main() { add(2000, 20); }
import math imports the module math and makes the exported names in the module visible to the client.cpp. Before I build the module, let me say a few words about module declaration files.
Module Declaration Files
Did you notice the strange name of the module: math.ixx.
Modernes C++ Mentoring
Do you want to stay informed: Subscribe.
- cl.exe (Microsoft) uses the required extension ixx. The ixx stands for a module interface source.
- Clang uses the extensioncppm. cppm stands presumably for a cpp module declaration. Wrong!!! The documentation to Clang is misleading. Please stop using the cppm extension until you read my next post. Use the extension cpp. I assume you don’t want to make the identical Odyssey such as me.
- I don’t know of a GCC extension.
Compile the Module math
To compile the module, you must use a very current Clang, GCC, or cl.exe compiler. I go into this post with cl.exe on Windows. The Microsoft blog provides two excellent introductions to modules: Overview of modules in C++ and C++ Modules conformance improvements with MSVC in Visual Studio 2019 16.5. In contrast, the lack of introductions to the Clang and GCC compilers makes it quite challenging to use modules.
Here are more details about my used Microsoft compiler:
These are the steps to compile and use the module with the Microsoft compiler. I only show the minimal command line. With an older Microsoft compiler, you must use at least /std:cpplatest.
cl.exe /experimental:module /c math.ixx // 1 cl.exe /experimental:module client.cpp math.obj // 2
- Creates an obj file math.obj and an IFC file math.ifc. The IFC file contains the metadata description of the module interface. The binary format of the IFC is modeled after the Internal Program Representation by Gabriel Dos Reis and Bjarne Stroustrup (2004/2005).
- Creates the executable client.exe. Without the implicitly used math.ifc file from the first step, the linker can not find the module.
For obvious reasons, I will not show you the output of the program execution. Let me change this.
Global Module Fragment
The global module fragment is meant to compose module interfaces. It’s a place to use preprocessor directives such as #include so the module interface can compile. The module interface does not export the code in the global module fragment.
The second version of the module math supports the two functions add and getProduct.
// math1.ixx module; // global module fragment (1) #include <numeric> #include <vector> export module math; // module declaration (2) export int add(int fir, int sec){ return fir + sec; } export int getProduct(const std::vector<int>& vec) { return std::accumulate(vec.begin(), vec.end(), 1, std::multiplies<int>()); }
I included the necessary headers between the global module fragment (line 1) and the module declaration (line 2).
// client1.cpp #include <iostream> #include <vector> import math; int main() { std::cout << std::endl; std::cout << "add(2000, 20): " << add(2000, 20) << std::endl; std::vector<int> myVec{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; std::cout << "getProduct(myVec): " << getProduct(myVec) << std::endl; std::cout << std::endl; }
The client imports the module math and uses its functionality:
Maybe, you don’t like to use a Standard Library Header anymore. Microsoft supports modules for all STL headers. Here is what I have found in the post “Using C++ Modules in Visual Studio 2017” from the Microsoft C++ team blog.
- C++ modules in Visual Studio 2017
std.regex
provides the content of the header<regex>
std.filesystem
provides the content of the header<experimental/filesystem>
std.memory
provides the content of the header<memory>
std.threading
provides the contents of headers<atomic>
,<condition_variable>
,<future>
,<mutex>
,<shared_mutex>
,and<thread>
std.core
provides everything else in the C++ Standard Library
To use the Microsoft Standard Library modules, specify the exception handling model (/EHsc) and the multithreading library (/MD). Additionally, you have to use the flag /std:c++latest.
Here are the modified versions of the interface file math2.ixx and the source file client2.cpp.
- math2.ixx
// math2.ixx module; import std.core; // (1) export module math; export int add(int fir, int sec){ return fir + sec; } export int getProduct(const std::vector<int>& vec) { return std::accumulate(vec.begin(), vec.end(), 1, std::multiplies<int>()); }
- client2.cpp
// client2.cpp import std.core; // (1) import math; int main() { std::cout << std::endl; std::cout << "add(2000, 20): " << add(2000, 20) << std::endl; std::vector<int> myVec{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; std::cout << "getProduct(myVec): " << getProduct(myVec) << std::endl; std::cout << std::endl; }
Both files use in line (1) the module std.core.
What’s next?
My first module math.ixx, math1.ixx, and math2.ixx defined its functionality in one file. In the next post, I will separate the module definition into a module interface unit and a module implementation unit.
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, 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, Philipp Lenk, Charles-Jianye Chen, Keith Jeffery,and Matt Godbolt.
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 |
Seminars
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
Contact Me
- Mobil: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,
Leave a Reply
Want to join the discussion?Feel free to contribute!