    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: C++20: A Simple Math Module</title>
        <link>https://members.accu.org/index.php/journals/2822</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">Overload Journal #158 - August 2020 + Programming Topics</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c413/">o158</a>
                    (7)
<br />

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c65/">Programming</a>
                    (877)
<br />

                                            <a href="https://members.accu.org/index.php/journals/c413-65/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c413+65/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;C++20: A Simple Math Module</h1>
<p><strong>Author:</strong>&nbsp;Bob Schmidt</p>
<p>
<strong>Date:</strong> 01 August 2020 17:59:51 +01:00 or Sat, 01 August 2020 17:59:51 +01:00</p>
<p><strong>Summary:</strong>&nbsp;Modules are one big change introduced by C++20. Rainer Grimm describes how to build a mathematics module.</p>
<p><strong>Body:</strong>&nbsp;<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.</p>

<h2>The long history of modules in C++</h2>

<p>Modules may be older than you think. My short historic detour should give you an idea how long it takes to get something so valuable into the C++ standard.</p>

<p>In 2004, Daveed Vandevoorde wrote proposal N1736.pdf [<a href="#[N1736]">N1736</a>], which described the idea of modules for the first time. 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="#[P0947]">P0947</a>] for modules. In 2019, the Modules TS and the ATOM proposal was merged into the C++20 committee draft [<a href="#[N4842]">N4842</a>], which is the syntax I use when writing about modules.</p>

<p>The C++ standardization process is democratic. The section Standardization [<a href="#[ISO]">ISO</a>] gives you more information about the standard and the standardization process. Figure 1 shows the various study groups.</p>

<table class="sidebartable">
	<tr>
		<td><img src="/content/images/journals/ol158/Grimm/Grimm-01.png" /></td>
	</tr>
	<tr>
		<td class="title">Figure 1</td>
	</tr>
</table>

<p>Explaining modules from a userâ€™s perspective is quite easy, but this does not hold true for the implementerâ€™s perspective. My plan for this article is to start with a simple modules math and add more features to it as we go.</p>

<h2>The math module</h2>

<p>First, here is my first module:</p>

<pre class="programlisting">
  // math.ixx
  export module math;
  export int add(int fir, int sec){
    return fir + sec;
  }</pre>

<p>The expression <code>export module math</code> is the module declaration. By putting <code>export</code> before the function <code>add</code>, <code>add</code> is exported and can, therefore, be used by a consumer of my module. </p>

<pre class="programlisting">
  // client.cpp
  import math;
  int main() {
    add(2000, 20);
  }</pre>

<p><code>import math</code> imports the module <code>math</code> and makes the exported names in the module visible to the <span class="filename">client.cpp</span>. Let me say a few words about module declaration files before I build the module.</p>

<h3>Module declaration files</h3>

<p>Did you noticed the strange name of the module: <span class="filename">math.ixx</span>.</p>

<ul>
	<li><span class="filename">cl.exe</span> (Microsoft) uses the required extension <span class="filename">ixx</span>. The <span class="filename">ixx</span> stands for a module interface source.</li>
	
	<li>Clang uses the extension <span class="filename">cppm</span>. <span class="filename">cppm</span> presumably stands for a <span class="filename">cpp</span> module declaration. Wrong!!! The documentation to Clang is misleading. Stop using the <span class="filename">cppm</span> extension until have you read my post about my attempt [<a href="#[Rainer20]">Rainer20</a>]. Use the extension <span class="filename">cpp</span>. I assume you donâ€™t want to make the same Odyssey as me.</li>
	
	<li>I donâ€™t know of a GCC extension.</li>
</ul>

<h3>Compile the module math</h3>

<p>To compile the module, you have to use a very current Clang, GCC, or <span class="filename">cl.exe</span> compiler. In this article, Iâ€™m using <span class="filename">cl.exe</span> on Windows. The Microsoft blog provides two excellent introductions to modules: Overview of modules in C++ [<a href="#[Microsoft-1]">Microsoft-1</a>] and C++ Modules conformance improvements with MSVC in Visual Studio 2019 16.5 [<a href="#[Microsoft-2]">Microsoft-2</a>]. In contrast, the lack of introductions to the Clang and GCC compilers makes it quite difficult to use modules.</p>

<p>Figure 2 shows more details of the Microsoft compiler I used.</p>

<table class="sidebartable">
	<tr>
		<td><img src="/content/images/journals/ol158/Grimm/Grimm-02.png" /></td>
	</tr>
	<tr>
		<td class="title">Figure 2</td>
	</tr>
</table>

<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 have to use at least <code>/std:cpplatest</code>.</p>

<pre class="programlisting">
 cl.exe /experimental:module /c math.ixx &#9312;
 cl.exe /experimental:module client.cpp math.obj &#9313;</pre>

<p>&#9312; Creates an obj file <span class="filename">math.obj</span> and an IFC file <span class="filename">math.ifc</span>. 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). </p>

<p>&#9313; Creates the executable <span class="filename">client.exe</span>. Without the implicitly used <span class="filename">math.ifc</span> file from the first step, the linker can not find the module (see Figure 3).</p>

<table class="sidebartable">
	<tr>
		<td><img src="/content/images/journals/ol158/Grimm/Grimm-03.png" /></td>
	</tr>
	<tr>
		<td class="title">Figure 3</td>
	</tr>
</table>

<p>For obvious reasons, I am not showing you the output of the program execution. Let me change this.</p>

<h2>Global module fragment</h2>

<p>The global module fragment is meant to compose module interfaces. Itâ€™s a place to use preprocessor directives such as <code>#include</code> so that the module interface can compile. The code in the global module fragment is not exported by the module interface.</p>

<p>The second version of the module <code>math</code> supports the two functions <code>add</code> and <code>getProduct</code> (see Listing 1).</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
module;              // global module fragment (1)

#include &lt;numeric&gt;
#include &lt;vector&gt;

export module math;  // module declaration (2)

export int add(int fir, int sec)
{
  return fir + sec;
}
export int getProduct(const std::vector&lt;int&gt;&amp; vec)
{
  return std::accumulate(vec.begin(), vec.end(),
  1, std::multiplies&lt;int&gt;());
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 1</td>
	</tr>
</table>

<p>I included the necessary headers between the global module fragment (line 1) and the module declaration (line 2).</p>

<p>The client imports the module math and uses its functionality (see Listing 2 and Figure 4).</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
#include &lt;iostream&gt;
#include &lt;vector&gt;

import math;

int main() {
  std::cout &lt;&lt; std::endl;   
  std::cout &lt;&lt; &quot;add(2000, 20): &quot; 
    &lt;&lt; add(2000, 20) &lt;&lt; std::endl;
  std::vector&lt;int&gt; myVec{1, 2, 3, 4, 5, 6, 7, 8, 
    9, 10};
  std::cout &lt;&lt; &quot;getProduct(myVec): &quot; 
    &lt;&lt; getProduct(myVec) &lt;&lt; std::endl;
  std::cout &lt;&lt; std::endl;
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 2</td>
	</tr>
</table>

<table class="sidebartable">
	<tr>
		<td><img src="/content/images/journals/ol158/Grimm/Grimm-04.png" /></td>
	</tr>
	<tr>
		<td class="title">Figure 4</td>
	</tr>
</table>

<p>Maybe, you donâ€™t like using a Standard Library Header anymore. Microsoft supports modules for all STL headers. Here is what I found out from the Microsoft C++ team blog [<a href="#[Microsoft-3]">Microsoft-3</a>]:</p>

<ul>
	<li><code>std.regex</code> provides the content of the header <code>&lt;regex&gt;</code></li>
	
	<li><code>std.filesystem</code> provides the content of the header <code>&lt;experimental/filesystem&gt;</code></li>
	
	<li><code>std.memory</code> provides the content of the header <code>&lt;memory&gt;</code></li>
	
	<li><code>std.threading</code> provides the contents of headers:
		<ul>
			<li><code>&lt;atomic&gt;</code></li>
			<li><code>&lt;condition_variable&gt;</code></li>
			<li><code>&lt;future&gt;</code></li>
			<li><code>&lt;mutex&gt;</code></li>
			<li><code>&lt;shared_mutex&gt;</code></li>
			<li><code>&lt;thread&gt;</code></li>
		</ul>
	</li>
	<li><code>std.core</code> provides everything else in the C++ Standard Library</li>
</ul>

<p>To use the Microsoft Standard Library modules, you have to specify the exception handling model (<code>/EHsc</code>) and the multithreading library (<code>/MD</code>). Additionally, you have to use the flag <code>/std:c++latest</code>.</p>
<p>Listing 3 and Listing 4 are the modified versions of the interface file <span class="filename">math2.ixx</span> and the source file <span class="filename">client2.cpp</span> respectively. </p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
module;

import std.core;         // (1)

export module math;

export int add(int fir, int sec)
{
    return fir + sec;
}

export int getProduct(const std::vector&lt;int&gt;&amp; vec)
{
  return std::accumulate(vec.begin(),
                         vec.end(),
                         1,
                         std::multiplies&lt;int&gt;());
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 3</td>
	</tr>
</table>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
import std.core;         // (1)

import math;

int main() {
  std::cout &lt;&lt; std::endl;

  std::cout &lt;&lt; &quot;add(2000, 20): &quot; &lt;&lt; add(2000, 20)
    &lt;&lt; std::endl;

  std::vector&lt;int&gt; myVec{1, 2, 3, 4, 5, 6, 7, 8,
    9, 10};

  std::cout &lt;&lt; &quot;getProduct(myVec): &quot; 
    &lt;&lt; getProduct(myVec) &lt;&lt; std::endl;
 
 std::cout &lt;&lt; std::endl;
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 4</td>
	</tr>
</table>

<p>Both files use â€“ in line (1) â€“ the module <code>std.core</code>.</p>

<p class="EditorIntro">This article was first published on Rainerâ€™s blog on 12 May 2020: <a href="https://www.modernescpp.com/index.php/cpp20-a-first-module">https://www.modernescpp.com/index.php/cpp20-a-first-module</a></p>

<h2>References</h2>

<p class="bibliomixed"><a id="[ISO]"></a>[ISO] â€˜Standardizationâ€™: <a href="https://isocpp.org/std/">https://isocpp.org/std/</a></p>

<p class="bibliomixed"><a id="[Microsoft-1]"></a>[Microsoft-1] â€˜Overview of modules in C++â€™: <a href="https://docs.microsoft.com/en-us/cpp/cpp/modules-cpp?view=vs-2019">https://docs.microsoft.com/en-us/cpp/cpp/modules-cpp?view=vs-2019</a></p>

<p class="bibliomixed">[Microsoft-2]<a id="[Microsoft-2]"></a> â€˜C++ Modules conformance improvements with MSVC in Visual Studio 2019 16.5â€™: <a href="https://devblogs.microsoft.com/cppblog/c-modules-conformance-improvements-with-msvc-in-visual-studio-2019-16-5/">https://devblogs.microsoft.com/cppblog/c-modules-conformance-improvements-with-msvc-in-visual-studio-2019-16-5/</a></p>

<p class="bibliomixed"><a id="[Microsoft-3]"></a>[Microsoft-3] â€˜Using C++ Modules in Visual Studio 2017â€™: <a href="https://devblogs.microsoft.com/cppblog/cpp-modules-in-visual-studio-2017/">https://devblogs.microsoft.com/cppblog/cpp-modules-in-visual-studio-2017/</a></p>

<p class="bibliomixed"><a id="[N1736]"></a>[N1736] â€˜Modules in C++â€™: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1736.pdf">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1736.pdf</a></p>

<p class="bibliomixed"><a id="[N4842]"></a>[N4842] â€˜N4842 Post-Belfast 2019 C++ working draftâ€™: <a href="https://github.com/cplusplus/draft/releases/tag/n4842">https://github.com/cplusplus/draft/releases/tag/n4842</a></p>

<p class="bibliomixed"><a id="[P0947]"></a>[P0947] â€˜Another take on Modulesâ€™: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0947r1.html">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0947r1.html</a></p>

<p class="bibliomixed"><a id="[Rainer20]"></a>[Rainer20] â€˜C++20: Module Interface Unit and Module Implementation Unitâ€™ posted 25 May 2020 at: <a href="https://www.modernescpp.com/index.php/c-20-module-interface-unit-and-module-implementation-unit">https://www.modernescpp.com/index.php/c-20-module-interface-unit-and-module-implementation-unit</a></p>

<p class="bio"><span class="author"><b>Rainer Grimm</b></span> Rainer has 20 years of experience as a software developer and software architect, a good 10 years as a training manager and seminar leader, and 3 years as a team leader in software. He has published several books on C ++, 70 articles on C ++, Python and Haskell for Linux-Magazin and iX Magazin and presents at specialist conferences.</p>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
