{"id":5909,"date":"2020-05-12T05:14:45","date_gmt":"2020-05-12T05:14:45","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/cpp20-a-first-module\/"},"modified":"2023-09-28T06:47:36","modified_gmt":"2023-09-28T06:47:36","slug":"cpp20-a-first-module","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/cpp20-a-first-module\/","title":{"rendered":"C++20: A Simple math Module"},"content":{"rendered":"<p>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.<\/p>\n<p><!--more--><\/p>\n<h2>\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-8397 size-full\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/TimelineCpp20Modules.png\" alt=\"\" width=\"1081\" height=\"399\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/TimelineCpp20Modules.png 1081w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/TimelineCpp20Modules-300x111.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/TimelineCpp20Modules-1030x380.png 1030w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/TimelineCpp20Modules-768x283.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/TimelineCpp20Modules-705x260.png 705w\" sizes=\"auto, (max-width: 1081px) 100vw, 1081px\" \/><\/h2>\n<h2>The Long History of Modules in C++<\/h2>\n<h2><img loading=\"lazy\" decoding=\"async\" class=\" alignright size-full wp-image-5905\" style=\"float: right;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/wg21-structure-2019-11.png\" alt=\"wg21 structure 2019 11\" width=\"400\" height=\"293\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/wg21-structure-2019-11.png 1500w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/wg21-structure-2019-11-300x220.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/wg21-structure-2019-11-1024x750.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/wg21-structure-2019-11-768x563.png 768w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/h2>\n<p>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.<\/p>\n<p>In 2004, Daveed Vandevoorde wrote the proposal <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2004\/n1736.pdf\">N1736.pdf<\/a>, 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 (<a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2018\/p0947r1.html\">P0947<\/a>) for modules. In 2019, the Modules TS and the ATOM proposal were merged into the C++20 committee draft (<a href=\"https:\/\/github.com\/cplusplus\/draft\/releases\/tag\/n4842\">N4842<\/a>), the syntax I present in my posts to modules.<\/p>\n<p>The C++ standardization process is democratic. Section <a href=\"https:\/\/isocpp.org\/std\/\">Standardization<\/a> gives you more information about the standard and the standardization process. The image to the right shows the various study groups.<\/p>\n<p>Explaining modules from the user&#8217;s perspective is quite easy, but this will not hold for the implementer&#8217;s perspective. My plan for this post is to start with simple modules math and add more features to it as we go.<\/p>\n<h2>The <span style=\"font-family: 'courier new', courier;\">math<\/span> Modul<\/h2>\n<p>First, here is my first module:<span style=\"font-family: courier new, courier;\"><br \/>\n<\/span><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ math.ixx<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> module math;\n\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">add<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> fir, <span style=\"color: #007788; font-weight: bold;\">int<\/span> sec){\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> fir <span style=\"color: #555555;\">+<\/span> sec;\n} \n<\/pre>\n<\/div>\n<p>The expression <span style=\"font-family: courier new, courier;\">export module math<\/span> is the module declaration. By putting <span style=\"font-family: courier new, courier;\">export<\/span> before the function adds<span style=\"font-family: courier new, courier;\">,<\/span> <span style=\"font-family: courier new, courier;\">add <\/span>is exported and can, therefore, be used by a consumer of my module.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ client.cpp<\/span>\n\nimport math;\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\n   \n   add(<span style=\"color: #ff6600;\">2000<\/span>, <span style=\"color: #ff6600;\">20<\/span>);\n   \n}\n<\/pre>\n<\/div>\n<p><span style=\"font-family: courier new, courier;\">import math<\/span> imports the module <span style=\"font-family: courier new, courier;\">math<\/span> and makes the exported names in the module visible to the <span style=\"font-family: 'courier new', courier;\">client.cpp<\/span>. Before I build the module, let me say a few words about module declaration files.<\/p>\n<h3>Module Declaration Files<\/h3>\n<p>Did you notice the strange name of the module: <span style=\"font-family: courier new, courier;\">math.ixx<\/span>.<\/p>\n<ul>\n<li>cl.exe (Microsoft) uses the required extension <strong><span style=\"font-family: courier new, courier;\">ixx. <\/span><\/strong>The <span style=\"font-family: courier new, courier;\"><strong>ixx<\/strong> <\/span>stands for a module interface source.<\/li>\n<li>Clang uses the extension<span style=\"font-family: courier new, courier;\">cppm.<\/span>\u00a0cppm stands presumably for a <span style=\"font-family: 'courier new', courier;\">cpp<\/span> module declaration.<strong>\u00a0Wrong!!! The documentation to Clang is misleading. Please stop using the <span style=\"font-family: 'courier new', courier;\">cppm<\/span> extension until you read my next post. Use the extension <span style=\"font-family: 'courier new', courier;\">cpp<\/span>. I assume you don&#8217;t want to make the identical Odyssey such as me.<\/strong><\/li>\n<li>I don&#8217;t know of a GCC extension.<\/li>\n<\/ul>\n<h3>Compile the Module <span style=\"font-family: courier new, courier;\">math<\/span><\/h3>\n<p>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: <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/cpp\/modules-cpp?view=vs-2019\">Overview of modules in C++<\/a> and\u00a0<a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/c-modules-conformance-improvements-with-msvc-in-visual-studio-2019-16-5\/\">C++ Modules conformance improvements with MSVC in Visual Studio 2019 16.5<\/a>. In contrast, the lack of introductions to the Clang and GCC compilers makes it quite challenging to use modules.<\/p>\n<p>Here are more details about my used Microsoft compiler:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5906\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/MicrosoftCompiler.png\" alt=\"MicrosoftCompiler\" width=\"500\" height=\"140\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/MicrosoftCompiler.png 2058w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/MicrosoftCompiler-300x84.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/MicrosoftCompiler-1024x287.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/MicrosoftCompiler-768x215.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/MicrosoftCompiler-1536x430.png 1536w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/MicrosoftCompiler-2048x573.png 2048w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>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 <span style=\"font-family: 'courier new', courier;\">\/std:cpplatest.<\/span><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\">cl.exe \/experimental:module \/c math.ixx           <span style=\"color: #0099ff; font-style: italic;\">\/\/ 1<\/span>\ncl.exe \/experimental:module client.cpp math.obj   <span style=\"color: #0099ff; font-style: italic;\">\/\/ 2<\/span>                            \n<\/pre>\n<\/div>\n<ol>\n<li>Creates an obj file <span style=\"font-family: 'courier new', courier;\">math.obj<\/span> and an <em>IFC<\/em> file <span style=\"font-family: 'courier new', courier;\">math.ifc<\/span>. The <em>IFC<\/em> file contains the metadata description of the module interface. The binary format of the <em>IFC<\/em> is modeled after the <a href=\"http:\/\/www.stroustrup.com\/gdr-bs-macis09.pdf\">Internal Program Representation<\/a>\u00a0by Gabriel Dos Reis and Bjarne Stroustrup (2004\/2005).\u00a0<span style=\"font-family: courier new, courier;\"><br \/>\n<\/span><\/li>\n<li>Creates the executable<span style=\"font-family: courier new, courier;\">\u00a0client<\/span><span style=\"font-family: courier new, courier;\">.exe. <\/span>Without the implicitly used\u00a0<span style=\"font-family: 'courier new', courier;\">math.ifc<\/span> file from the first step, the linker can not find the module.<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5907\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/moduleNotFound1.png\" alt=\"moduleNotFound1\" width=\"550\" height=\"268\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/moduleNotFound1.png 2461w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/moduleNotFound1-300x146.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/moduleNotFound1-1024x497.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/moduleNotFound1-768x373.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/moduleNotFound1-1536x746.png 1536w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/moduleNotFound1-2048x994.png 2048w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/p>\n<p>For obvious reasons, I will not show you the output of the program execution. Let me change this.<\/p>\n<h3>Global Module Fragment<\/h3>\n<p>The global module fragment is meant to compose module interfaces. It&#8217;s a place to use preprocessor directives such as\u00a0<span style=\"font-family: 'courier new', courier;\">#include<\/span>\u00a0so the module interface can compile. The module interface does not export the code in the global module fragment.<\/p>\n<p>The second version of the module <span style=\"font-family: courier new, courier;\">math<\/span> supports the two functions <span style=\"font-family: courier new, courier;\">add<\/span> and <span style=\"font-family: courier new, courier;\">getProduct<\/span>.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ math1.ixx<\/span>\nmodule;                   <span style=\"color: #0099ff; font-style: italic;\">\/\/ global module fragment (1)<\/span>\n\n<span style=\"color: #009999;\">#include &lt;numeric&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;vector&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> module math;       <span style=\"color: #0099ff; font-style: italic;\">\/\/ module declaration (2)<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">add<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> fir, <span style=\"color: #007788; font-weight: bold;\">int<\/span> sec){\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> fir <span style=\"color: #555555;\">+<\/span> sec;\n}\n\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">getProduct<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;&amp;<\/span> vec) {\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> std<span style=\"color: #555555;\">::<\/span>accumulate(vec.begin(), vec.end(), <span style=\"color: #ff6600;\">1<\/span>, std<span style=\"color: #555555;\">::<\/span>multiplies<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span>());\n}\n<\/pre>\n<\/div>\n<p>I included the necessary headers between the global module fragment (line 1) and the module declaration (line 2).<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ client1.cpp<\/span>\n\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;vector&gt;<\/span>\n\nimport math;\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;   \n   \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"add(2000, 20): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> add(<span style=\"color: #ff6600;\">2000<\/span>, <span style=\"color: #ff6600;\">20<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n    \n    std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> myVec{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">4<\/span>, <span style=\"color: #ff6600;\">5<\/span>, <span style=\"color: #ff6600;\">6<\/span>, <span style=\"color: #ff6600;\">7<\/span>, <span style=\"color: #ff6600;\">8<\/span>, <span style=\"color: #ff6600;\">9<\/span>, <span style=\"color: #ff6600;\">10<\/span>};\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"getProduct(myVec): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> getProduct(myVec) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n   \n}\n<\/pre>\n<\/div>\n<p>The client imports the module <span style=\"font-family: courier new, courier;\">math<\/span> and uses its functionality:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5908\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/client1.png\" alt=\"client1\" width=\"300\" height=\"164\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/client1.png 1026w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/client1-300x165.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/client1-1024x562.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/client1-768x421.png 768w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>Maybe, you don&#8217;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 &#8220;<a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/cpp-modules-in-visual-studio-2017\/\">Using C++ Modules in Visual Studio 2017<\/a>&#8221; from the Microsoft C++ team blog.<\/p>\n<ul>\n<li>C++ modules in Visual Studio 2017\n<ul>\n<li><span style=\"font-family: Courier New;\"><code>std.regex<\/code><\/span> provides the content of the header <span style=\"font-family: Courier New, Courier, monospace;\"><code>&lt;regex&gt;<\/code><\/span><\/li>\n<li><span style=\"font-family: Courier New, Courier, monospace;\"><code>std.filesystem<\/code><\/span> provides the content of the header <code><span style=\"font-family: Courier New, Courier, monospace;\">&lt;experimental\/filesystem<\/span>&gt;<\/code><\/li>\n<li><span style=\"font-family: Courier New, Courier, monospace;\"><code>std.memory<\/code><\/span> provides the content of the header <span style=\"font-family: Courier New, Courier, monospace;\"><code>&lt;memory&gt;<\/code><\/span><\/li>\n<li><span style=\"font-family: Courier New, Courier, monospace;\"><code>std.threading<\/code><\/span> provides the contents of headers <span style=\"font-family: Courier New, Courier, monospace;\"><code>&lt;atomic&gt;<\/code>, <code>&lt;condition_variable&gt;<\/code>, <code>&lt;future&gt;<\/code>, <code>&lt;mutex&gt;<\/code>, <code>&lt;shared_mutex&gt;<\/code>,and <code>&lt;thread&gt;<\/code><\/span><\/li>\n<li class=\"\"><span style=\"font-family: Courier New, Courier, monospace;\"><code>std.core<\/code><\/span> provides everything else in the C++ Standard Library<\/li>\n<\/ul>\n<div><\/div>\n<\/li>\n<\/ul>\n<p>To use the Microsoft Standard Library modules, specify the exception handling model (<a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/build\/reference\/eh-exception-handling-model?view=vs-2019\"><span style=\"font-family: courier new, courier;\">\/EHsc)<\/span><\/a> and the multithreading library (<a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/build\/reference\/md-mt-ld-use-run-time-library?view=vs-2019\"><span style=\"font-family: courier new, courier;\">\/MD)<\/span><\/a>.\u00a0 Additionally, you have to use the flag\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/build\/reference\/std-specify-language-standard-version?view=vs-2019\"><span style=\"font-family: courier new, courier;\">\/std:c++latest<\/span><\/a>.<\/p>\n<p>Here are the modified versions of the interface file<span style=\"font-family: courier new, courier;\"> math2.ixx<\/span> and the source file <span style=\"font-family: courier new, courier;\">client2.cpp<\/span>.<\/p>\n<ul>\n<li><span style=\"font-family: courier new, courier;\">math2.ixx<\/span><\/li>\n<\/ul>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ math2.ixx<\/span>\nmodule;                   \n\nimport std.core;                            <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> module math;       \n\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">add<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> fir, <span style=\"color: #007788; font-weight: bold;\">int<\/span> sec){\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> fir <span style=\"color: #555555;\">+<\/span> sec;\n}\n\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">getProduct<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;&amp;<\/span> vec) {\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> std<span style=\"color: #555555;\">::<\/span>accumulate(vec.begin(), vec.end(), <span style=\"color: #ff6600;\">1<\/span>, std<span style=\"color: #555555;\">::<\/span>multiplies<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span>());\n}\n<\/pre>\n<\/div>\n<ul>\n<li><span style=\"font-family: courier new, courier;\">client2.cpp<\/span><\/li>\n<\/ul>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ client2.cpp<\/span>\n\nimport std.core;                  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\n\nimport math;\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;   \n   \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"add(2000, 20): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> add(<span style=\"color: #ff6600;\">2000<\/span>, <span style=\"color: #ff6600;\">20<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n    \n    std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> myVec{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">4<\/span>, <span style=\"color: #ff6600;\">5<\/span>, <span style=\"color: #ff6600;\">6<\/span>, <span style=\"color: #ff6600;\">7<\/span>, <span style=\"color: #ff6600;\">8<\/span>, <span style=\"color: #ff6600;\">9<\/span>, <span style=\"color: #ff6600;\">10<\/span>};\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"getProduct(myVec): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> getProduct(myVec) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n   \n}\n<\/pre>\n<\/div>\n<p>Both files use in line (1) the module<span style=\"font-family: courier new, courier;\"> std.core.<\/span><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>My first module <span style=\"font-family: courier new, courier;\">math.ixx, math1.ixx,<\/span> and <span style=\"font-family: courier new, courier;\">math2.ixx<\/span> defined its functionality in one file. In the <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-20-module-interface-unit-and-module-implementation-unit\">next post<\/a>, I will separate the module definition into a module interface unit and a module implementation unit.<\/p>\n<p style=\"text-align: justify;\">\n","protected":false},"excerpt":{"rendered":"<p>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.<\/p>\n","protected":false},"author":21,"featured_media":8397,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[375],"tags":[443],"class_list":["post-5909","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-20","tag-modules"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5909","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=5909"}],"version-history":[{"count":2,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5909\/revisions"}],"predecessor-version":[{"id":8402,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5909\/revisions\/8402"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/8397"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5909"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}