{"id":5678,"date":"2019-05-05T18:17:00","date_gmt":"2019-05-05T18:17:00","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-20-more-details-to-modules\/"},"modified":"2023-06-26T10:11:04","modified_gmt":"2023-06-26T10:11:04","slug":"c-20-more-details-to-modules","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-20-more-details-to-modules\/","title":{"rendered":"More Details to Modules"},"content":{"rendered":"<p>&nbsp;My last post gave you an introduction to modules in C++20. This post shows how to use existing modules.<\/p>\n<p><!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5674\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/boxes-2624231_1280.jpg\" alt=\"boxes 2624231 1280\" width=\"400\" height=\"411\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/boxes-2624231_1280.jpg 1245w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/boxes-2624231_1280-292x300.jpg 292w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/boxes-2624231_1280-996x1024.jpg 996w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/boxes-2624231_1280-768x790.jpg 768w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>Before I begin this post, let me shortly sum up where we ended in my first post to <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-20-modules\">modules<\/a>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"A_Short_Recap\"><\/span>A Short Recap<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>I created a module <span style=\"font-family: courier new, courier;\">math1<\/span>, which consisted of a module interface unit and a module implementation unit, and a client which used it. Here are the three source files.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Module_Interface_Unit\"><\/span>Module Interface Unit<span class=\"ez-toc-section-end\"><\/span><\/h3>\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.cppm<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> module math1;\r\n\r\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);\r\n<\/pre>\n<\/div>\n<h3><span class=\"ez-toc-section\" id=\"Module_Implementation_Unit\"><\/span>Module Implementation Unit<span class=\"ez-toc-section-end\"><\/span><\/h3>\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.cpp<\/span>\r\n\r\nmodule math1;\r\n\r\n<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){\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> fir <span style=\"color: #555555;\">+<\/span> sec;\r\n}\r\n<\/pre>\n<\/div>\n<h3><span class=\"ez-toc-section\" id=\"Client\"><\/span>Client<span class=\"ez-toc-section-end\"><\/span><\/h3>\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;\">\/\/ main1.cpp<\/span>\r\n\r\nimport math1;\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n   \r\n   add(<span style=\"color: #ff6600;\">2000<\/span>, <span style=\"color: #ff6600;\">20<\/span>);\r\n   \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I compiled the program with a current clang and cl.exe compiler. From now on, I will stick with the cl.exe compiler because the compile line is slightly shorter. As promised in my last post, let me show you the program&#8217;s output.<\/p>\n<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Using_a_Standard_Module\"><\/span>Using a Standard Module<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Essentially, neither the module interface nor the implementation unit changed in module math2.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Module_Interface_Unit-2\"><\/span>Module Interface Unit<span class=\"ez-toc-section-end\"><\/span><\/h3>\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.cppm<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> module math2;\r\n\r\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);\r\n<\/pre>\n<\/div>\n<h3><span class=\"ez-toc-section\" id=\"Module_Implementation_Unit-2\"><\/span>Module Implementation Unit<span class=\"ez-toc-section-end\"><\/span><\/h3>\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.cpp<\/span>\r\n\r\nmodule math2;\r\n\r\n<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){\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> fir <span style=\"color: #555555;\">+<\/span> sec;\r\n}\r\n<\/pre>\n<\/div>\n<h3><span class=\"ez-toc-section\" id=\"Client-2\"><\/span>Client<span class=\"ez-toc-section-end\"><\/span><\/h3>\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;\">\/\/ main2.cpp<\/span>\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/#include &lt;iostream&gt;<\/span>\r\n\r\nimport std.core;\r\n\r\nimport math2;\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n\t\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n   \r\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;\r\n   \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Thanks to the module <span style=\"font-family: courier new, courier;\">std.core<\/span>, I can show the result of the addition.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5675\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/math2.png\" alt=\"math2\" width=\"300\" height=\"134\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/math2.png 902w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/math2-300x134.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/math2-768x343.png 768w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>Using the header<span style=\"font-family: courier new, courier;\"> &lt;iostream&gt;<\/span> would also be possible. Of course, I hear your question about which modules are available. Here is what I have from the &#8220;Using C++ Modules in Visual Studio 2017&#8221; post from the Microsoft C++ team blog.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"C_Modules_in_Visual_Studio_2017\"><\/span>C++ Modules in Visual Studio 2017<span class=\"ez-toc-section-end\"><\/span><\/h2>\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>, <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<p>Modules provide a higher abstraction than headers. This makes it quite comfortable to use them. Additionally, you can specify which name of a module should be exported or not.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Export_versus_Non-Export\"><\/span>Export versus Non-Export<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The next module <span style=\"font-family: courier new, courier;\">math3<\/span> is a little more complicated than the previous one. Here is the interface.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Module_Interface_Unit-3\"><\/span>Module Interface Unit<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>&nbsp;<\/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;\">\/\/ math3.cppm<\/span>\r\n\r\nimport std.core;\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> module math3;\r\n\r\n<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);\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">mult<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> fir, <span style=\"color: #007788; font-weight: bold;\">int<\/span> sec);\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> <span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">doTheMath<\/span>();\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The module interface unit contains the exporting module declaration: <span style=\"font-family: Courier New, Courier, monospace;\">export module math3;. <\/span>The module declaration starts the so-called <b>module purview<\/b>. Only names after the module purview declared with <span style=\"font-family: courier new, courier;\">export<\/span> are exported. If not, the name is not visible outside the module and has module linkage. This holds in particular for the function <span style=\"font-family: Courier New, Courier, monospace;\">add<\/span> but not for the <span style=\"font-family: courier new, courier;\">mult<\/span> and <span style=\"font-family: courier new, courier;\">doTheMath<\/span>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Module_Implementation_Unit-3\"><\/span>Module Implementation Unit<span class=\"ez-toc-section-end\"><\/span><\/h3>\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;\">\/\/ math3.cpp<\/span>\r\n\r\nmodule math3;\r\n\r\n<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){\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> fir <span style=\"color: #555555;\">+<\/span> sec;\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">mult<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> fir, <span style=\"color: #007788; font-weight: bold;\">int<\/span> sec){\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> fir <span style=\"color: #555555;\">*<\/span> sec;\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">doTheMath<\/span>(){\r\n\tstd<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;\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>There is nothing to add to the module implementation unit. The <span style=\"font-family: Courier New, Courier, monospace;\">main <\/span>program is more interesting.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Client-3\"><\/span>Client<span class=\"ez-toc-section-end\"><\/span><\/h3>\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;\">\/\/ main3.cpp<\/span>\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ #include &lt;iostream&gt;    \/\/ (1)<\/span>\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ #include &lt;numeric&gt;     \/\/ (1)<\/span>\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ #include &lt;string&gt;      \/\/ (1)<\/span>\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ #include &lt;vector&gt;      \/\/ (1)<\/span>\r\nimport std.core;          <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n\r\nimport math3;\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n\t\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n   \r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ std::cout &lt;&lt; \"add(2000, 20): \" &lt;&lt; add(2000, 20) &lt;&lt; std::endl;   \/\/ (3)<\/span>\r\n\t\r\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: #555555;\">=<\/span> {<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>};\r\n\t\r\n    std<span style=\"color: #555555;\">::<\/span>string doc <span style=\"color: #555555;\">=<\/span> <span style=\"color: #cc3300;\">\"std::accumulate(myVec.begin(), myVec.end(), mult): \"<\/span>; \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> prod <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>accumulate(myVec.begin(), myVec.end(), <span style=\"color: #ff6600;\">1<\/span>, mult);\r\n\t\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> doc <span style=\"color: #555555;\">&lt;&lt;<\/span> prod <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; \r\n\t\r\n    doTheMath();\r\n\t\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>You see, modules are pretty comfortable in my case. Instead of using the four headers in the line (1),&nbsp; I&#8217;m okay with a simple<span style=\"font-family: courier new, courier;\"> import std.core<\/span> in line (2). That was it. Here is the output of the program.<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5676\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/math3.png\" alt=\"math3\" width=\"500\" height=\"123\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/math3.png 1656w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/math3-300x74.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/math3-1024x253.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/math3-768x190.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/math3-1536x379.png 1536w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>Now, to the question: What happens if I add the function <span style=\"font-family: courier new, courier;\">add<\/span> in line (3). To recap, <span style=\"font-family: courier new, courier;\">add<\/span> is not exported and has module linkage.&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5677\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/math3Add.PNG\" alt=\"math3Add\" width=\"700\" height=\"243\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/math3Add.PNG 3214w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/math3Add-300x104.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/math3Add-1024x355.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/math3Add-768x266.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/math3Add-1536x532.png 1536w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/05\/math3Add-2048x710.png 2048w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/p>\n<p>The compiler complains that the function <span style=\"font-family: courier new, courier;\">add<\/span> is used in the main program, but the name <span style=\"font-family: courier new, courier;\">add<\/span> is not visible.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Further_Details\"><\/span>Further Details<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>First, you can export in various ways.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Export\"><\/span>Export<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Exporting names with export specifiers, such as in <span style=\"font-family: courier new, courier;\">math3.cppm<\/span> is tedious.<\/p>\n<h4>Export Specifier<\/h4>\n<div style=\"background: #f0f3f3 none repeat scroll 0% 0%; overflow: auto; width: auto; border-width: 0.1em 0.1em 0.1em 0.8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic; font-family: courier new, courier;\">\/\/ math3.cppm<\/span>\r\n\r\nimport std.core;\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> module math3;\r\n\r\n<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);\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">mult<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> fir, <span style=\"color: #007788; font-weight: bold;\">int<\/span> sec);\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> <span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">doTheMath<\/span>()<\/pre>\n<\/div>\n<div id=\"simple-translate\">&nbsp;<\/div>\n<div>Instead of an export specifier, you can use an exported group.<\/div>\n<div>\n<h4>Exported Group<\/h4>\n<div style=\"background: #f0f3f3 none repeat scroll 0% 0%; overflow: auto; width: auto; border-width: 0.1em 0.1em 0.1em 0.8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ math3.cppm<\/span>\r\n\r\nimport std.core;\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> module math3;\r\n\r\n<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);<br \/><br \/><span style=\"color: #006699; font-weight: bold;\">export {<\/span> <span style=\"color: #007788; font-weight: bold;\"><br \/>  <br \/>    int<\/span> <span style=\"color: #cc00ff;\">mult<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> fir, <span style=\"color: #007788; font-weight: bold;\">int<\/span> sec);\r\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">doTheMath<\/span>();<br \/><br \/>}<\/pre>\n<\/div>\n<div id=\"simple-translate\">&nbsp;<\/div>\n<div>The third variation is to use an exported namespace.<\/div>\n<div>\n<h4>Exported Namespace<\/h4>\n<\/div>\n<div style=\"background: #f0f3f3 none repeat scroll 0% 0%; overflow: auto; width: auto; border-width: 0.1em 0.1em 0.1em 0.8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ math3.cppm<\/span>\r\n\r\nimport std.core;\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> module math3;<br \/><br \/><span style=\"color: #006699; font-weight: bold;\">namespace math3<\/span> {<br \/><br \/>    <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);<br \/><br \/>}<br \/><br \/><span style=\"color: #006699; font-weight: bold;\">export namespace math3 {<\/span> <span style=\"color: #007788; font-weight: bold;\"><br \/>  <br \/>    int<\/span> <span style=\"color: #cc00ff;\">mult<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> fir, <span style=\"color: #007788; font-weight: bold;\">int<\/span> sec);\r\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">doTheMath<\/span>();<br \/><br \/>}<\/pre>\n<\/div>\n<div id=\"simple-translate\">&nbsp;<\/div>\n<div>All three variations are semantically equivalent.<\/div>\n<div>&nbsp;<\/div>\n<p>It may also be quite comfortable to re-export a module<\/p>\n<h4>Re-Export a Module<\/h4>\n<p>Sometimes, you want to export something which you imported from another module. If you don&#8217;t export the imported module, the imported module has, consequently, module linkage, and its names are not visible outside the module. Here is a concrete example.<\/p>\n<h4>Visible versus Invisible<\/h4>\n<p>Imagine, I want to import and use the module <span style=\"font-family: courier new, courier;\">math.core<\/span> and <span style=\"font-family: courier new, courier;\">math.core2<\/span> in a new module <span style=\"font-family: courier new, courier;\">math<\/span>. Here is the module interface unit of <span style=\"font-family: courier new, courier;\">math.core <\/span>and<span style=\"font-family: courier new, courier;\"> math.core2.<br \/><\/span><\/p>\n<ul>\n<li>Re-exported modules<\/li>\n<\/ul>\n<p>&nbsp;<\/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;\">\/\/ module interface unit of math.core<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> math.core\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> mult(<span style=\"color: #007788; font-weight: bold;\">int<\/span> fir, <span style=\"color: #007788; font-weight: bold;\">int<\/span> sec); \r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<\/div>\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;\">\/\/ module interface unit of math.core2<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> math.core2\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> add(<span style=\"color: #007788; font-weight: bold;\">int<\/span> fir, <span style=\"color: #007788; font-weight: bold;\">int<\/span> sec); \r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Next, here is the new module <span style=\"font-family: courier new, courier;\">math<\/span>.<\/p>\n<ul>\n<li>The new module <span style=\"font-family: courier new, courier;\">math<\/span><\/li>\n<\/ul>\n<p>&nbsp;<\/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;\">\/\/ module interface unit of math<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> module math;\r\n\r\nimport math.core;           <span style=\"color: #0099ff; font-style: italic;\">\/\/ not exported with mult<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> import math.core2;   <span style=\"color: #0099ff; font-style: italic;\">\/\/ exported with add<\/span>\r\n\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ module implementation unit of math<\/span>\r\n\r\nmult(<span style=\"color: #ff6600;\">1100<\/span>, <span style=\"color: #ff6600;\">2<\/span>);             <span style=\"color: #0099ff; font-style: italic;\">\/\/ fine<\/span>\r\nadd(<span style=\"color: #ff6600;\">2000<\/span>, <span style=\"color: #ff6600;\">20<\/span>);             <span style=\"color: #0099ff; font-style: italic;\">\/\/ fine<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>As you can see, it&#8217;s fine to use the exported and non-exported names in the module <span style=\"font-family: courier new, courier;\">math<\/span>. But the module <span style=\"font-family: courier new, courier;\">math.core<\/span> is not exported. Only a client using the module math will see the difference.<\/p>\n<ul>\n<li>Client<\/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;\">\/\/ Client<\/span>\r\n\r\nimport math\r\n\r\n<span style=\"color: #cc00ff;\">mult<\/span>(<span style=\"color: #ff6600;\">1100<\/span>, <span style=\"color: #ff6600;\">2<\/span>);             <span style=\"color: #0099ff; font-style: italic;\">\/\/ ERROR<\/span>\r\nadd(<span style=\"color: #ff6600;\">2000<\/span>, <span style=\"color: #ff6600;\">20<\/span>);             <span style=\"color: #0099ff; font-style: italic;\">\/\/ fine<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The function <span style=\"font-family: courier new, courier;\">mult<\/span> has module linkage and is not visible outside the module. Only the function <span style=\"font-family: courier new, courier;\">add<\/span> is visible.<\/p>\n<h4>Repackage Modules<\/h4>\n<p>There is a comfortable way to repackage modules. Just put them in an exported group.<\/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: #006699; font-weight: bold;\">export<\/span> module math;\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">export<\/span>{\r\n\r\n    import math.core;\r\n    import math.core2;\r\n    import math.basics;\r\n\t\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>This makes all names visible for a client which imports the module <span style=\"font-family: courier new, courier;\">math<\/span>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Whats_next\"><\/span>What&#8217;s next?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>With my next post, I begin the last main topic of the C++ core guidelines: rules to the standard library. Believe it or not, many professional C++ developers don&#8217;t use the standard template library (STL). This holds, in particular, true for the algorithms of the STL.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp;My last post gave you an introduction to modules in C++20. This post shows how to use existing modules.<\/p>\n","protected":false},"author":21,"featured_media":5674,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[443],"class_list":["post-5678","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modern-c","tag-modules"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5678","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=5678"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5678\/revisions"}],"predecessor-version":[{"id":6788,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5678\/revisions\/6788"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5674"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}