{"id":6207,"date":"2021-09-02T20:46:38","date_gmt":"2021-09-02T20:46:38","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-20-modules-private-module-fragment-and-header-units\/"},"modified":"2023-09-28T06:49:14","modified_gmt":"2023-09-28T06:49:14","slug":"c-20-modules-private-module-fragment-and-header-units","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-20-modules-private-module-fragment-and-header-units\/","title":{"rendered":"C++20 Modules: Private Module Fragment and Header Units"},"content":{"rendered":"<p>In the last weeks, I learned something new about modules in C++20: private modules fragments and header units. Consequently, I make a short detour in this post and present these new features.<\/p>\n<p><!--more--><\/p>\n<p data-wp-editing=\"1\">\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\" \/><\/p>\n<p>You may wonder why I didn&#8217;t complete my promised post about variadic templates. The reason is simple. The next <a href=\"https:\/\/www.modernescpp.com\/index.php\/what-pdf-bundle-do-you-want-make-your-choice\">pdf-bundle<\/a> I publish next week is about C++20 modules, and I want to incorporate this post in this bundle. Before I do that, I have to write this post.<\/p>\n<p>Private module fragments and header units make dealing with modules in C++20 way more comfortable.<\/p>\n<p>I intentionally use the newest Visual Studio compiler in this post because its C++20 modules support is almost complete. The newest GCC and Clang only partially support modules.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"private_Module_Fragment\"><\/span><code>private<\/code> Module Fragment<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>I&#8217;m unsure if you have the facts about the module interface unit and implementation unit ready. Therefore, let me repeat the essential facts.<\/p>\n<p>When you want to separate your module into an interface and an implementation, you should structure it into a module interface unit and one or more module implementation units.<\/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<p><!-- HTML generated using hilite.me --><\/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;\">\/\/ mathInterfaceUnit2.ixx<\/span>\n\nmodule;                   \n\n#include &lt;vector&gt;                           \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: #006699; font-weight: bold;\">namespace<\/span> math {\n\n    <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);\n \n    <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\n}\n<\/pre>\n<\/div>\n<ul>\n<li>The module interface unit contains the exporting module declaration: <span style=\"font-family: courier new, courier;\">export module math<\/span>.<\/li>\n<li>The names <span style=\"font-family: courier new, courier;\">add<\/span>, and <span style=\"font-family: courier new, courier;\">getProduct<\/span> are exported.<\/li>\n<li>A module can have only one module interface unit.<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Module_Implementation_Unit\"><\/span>Module Implementation Unit<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><!-- HTML generated using hilite.me --><\/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;\">\/\/ mathImplementationUnit2.cpp<\/span>\n\nmodule math;\n\n<span style=\"color: #009999;\">#include &lt;numeric&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">namespace<\/span> math {\n\n    <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){\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> fir <span style=\"color: #555555;\">+<\/span> sec;\n    }\n\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> getProduct(<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}\n<\/pre>\n<\/div>\n<ul>\n<li>The module implementation unit contains non-exporting module declarations: <span style=\"font-family: courier new, courier;\">module math;<\/span><\/li>\n<li>A module can have more than one module implementation unit.<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Main_Program\"><\/span>Main Program<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><!-- HTML generated using hilite.me --><\/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;\">\/\/ client4.cpp<\/span>\n\n#include &lt;iostream&gt;\n#include &lt;vector&gt;\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;\">\"math::add(2000, 20): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> math<span style=\"color: #555555;\">::<\/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;\">\"math::getProduct(myVec): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> math<span style=\"color: #555555;\">::<\/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<ul>\n<li>\u00a0From the user&#8217;s perspective, only the namespace <span style=\"font-family: courier new, courier;\">math was added<\/span>.\u00a0<span style=\"font-family: courier new, courier;\"><br \/>\n<\/span><\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Building_the_Executable\"><\/span>Building the Executable<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Manually building the executable includes a few steps.<\/p>\n<p><!-- HTML generated using hilite.me --><\/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 <span style=\"color: #555555;\">\/<\/span>std<span style=\"color: #555555;\">:<\/span>c<span style=\"color: #555555;\">++<\/span>latest <span style=\"color: #555555;\">\/<\/span>c mathInterfaceUnit2.ixx <span style=\"color: #555555;\">\/<\/span>EHsc                <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\ncl.exe <span style=\"color: #555555;\">\/<\/span>std<span style=\"color: #555555;\">:<\/span>c<span style=\"color: #555555;\">++<\/span>latest <span style=\"color: #555555;\">\/<\/span>c mathImplementationUnit2.cpp <span style=\"color: #555555;\">\/<\/span>EHsc            <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\ncl.exe <span style=\"color: #555555;\">\/<\/span>std<span style=\"color: #555555;\">:<\/span>c<span style=\"color: #555555;\">++<\/span>latest <span style=\"color: #555555;\">\/<\/span>c client4.cpp <span style=\"color: #555555;\">\/<\/span>EHsc                           <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\ncl.exe client4.obj mathInterfaceUnit2.obj mathImplementationUnit2.obj   <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\n<\/pre>\n<\/div>\n<ol>\n<li>Creates the object file <span style=\"font-family: courier new, courier;\">mathInterfaceUnit2.ob<\/span>j and the module interface file <span style=\"font-family: courier new, courier;\">math.ifc.<\/span><\/li>\n<li>Creates the object file<span style=\"font-family: courier new, courier;\"> mathImplementationUnit2.obj.<\/span><\/li>\n<li>Creates the object file <span style=\"font-family: courier new, courier;\">client4.obj.<\/span><\/li>\n<li>Creates the executable <span style=\"font-family: courier new, courier;\">client4.exe.<\/span><\/li>\n<\/ol>\n<p>You should specify the exception handling model (\/EHsc) for the Microsoft compiler.\u00a0 Additionally, 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>Finally, here is the output of the program:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6205\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/client4.png\" alt=\"client4\" width=\"450\" height=\"173\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/client4.png 478w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/client4-300x115.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/p>\n<p>One of the significant advantages of structuring modules into a module interface unit and one or more module implementation units is that modifications in the module implementation units do not affect the module interface unit and, therefore, require no recompilation.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Private_Module_Fragment\"><\/span><code>Private<\/code> Module Fragment<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Thanks to a private module fragment, you can implement a module in one file and declare its last part as its implementation using <code>module :private;<\/code>. Consequently, a modification of the private module fragment does not cause recompilation. The following module declaration file<code> mathInterfaceUnit3.ixx<\/code> refactors the module interface unit <code>mathInterfaceUnit2.ixx<\/code> and the module implementation unit <code>mathImplementationUnit2.cpp<\/code> into one file.<\/p>\n<p><!-- HTML generated using hilite.me --><\/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;\">\/\/ mathInterfaceUnit3.ixx<\/span>\n\nmodule;                   \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;       \n\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> <span style=\"color: #006699; font-weight: bold;\">namespace<\/span> math {\n\n   <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);\n\n   <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\n}\n\nmodule <span style=\"color: #555555;\">:<\/span><span style=\"color: #006699; font-weight: bold;\">private<\/span>;               <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\n\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) {\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> fir <span style=\"color: #555555;\">+<\/span> sec;\n}\n\n<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><code>module: private;<\/code> (line 1) denotes the start of the <code>private<\/code> module fragment. A modification in this optional last part of a module declaration file does not cause its recompilation.<\/p>\n<p>I already presented header units in a previous post. Now, I can use them<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Header_Units\"><\/span>Header Units<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Header units are a smooth way to transition from headers to modules. You have to replace the <code>#include<\/code> directive with the new <code>import<\/code> statement.<\/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: #009999;\">#include &lt;vector&gt;      =&gt; import &lt;vector&gt;;<\/span>\n<span style=\"color: #009999;\">#include \"myHeader.h\"  =&gt; import \"myHeader.h\"; <\/span>\n<\/pre>\n<\/div>\n<p>First, <span style=\"font-family: courier new, courier;\">import<\/span> respects the same lookup rules as <span style=\"font-family: courier new, courier;\">include<\/span>. This means in the case of the quotes (<span style=\"font-family: courier new, courier;\">&#8220;myHeader.h&#8221;<\/span>), the lookup first searches in the local directory before it continues with the system search path.<\/p>\n<p>Second, this is way more than text replacement. In this case, the compiler generates something module-like from the import directive and treats the result as a module. The importing module statement gets all exportable names for the header. The exportable names include macros. Importing these synthesized header units is faster and comparable in speed to <a href=\"https:\/\/en.wikipedia.org\/wiki\/Precompiled_header\">precompiled headers<\/a>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Modules_are_not_Precompiled_Header\"><\/span>Modules are not Precompiled Header<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Precompiled headers are a non-standardized way to compile headers in an intermediate form that is faster to process for the compiler. The Microsoft compiler uses the extension<code> .pch<\/code> , and the GCC compiler<code> .gch<\/code> for precompiled headers. The main difference between precompiled headers and modules is that modules can selectively export names. Only in a module are exported names visible outside the module.<\/p>\n<p>After this short remainder, let me try it out.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Use_of_Header_Units\"><\/span>Use of Header Units<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The following example consists of three files. The header file<code> head.h<\/code>, declaring the function <code>hello<\/code>, its implementation file<code> head.cpp<\/code>, defining the function <code>hello<\/code>, and the client file <code>helloWorld3.cpp<\/code> using the function <code>hello<\/code>.<\/p>\n<p><!-- HTML generated using hilite.me --><\/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;\">\/\/ head.h<\/span>\n\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">hello<\/span>();\n<\/pre>\n<\/div>\n<p>Only the implementation file <code>head.cpp<\/code> and the client file <code>helloWorld3.cpp<\/code> are special. They import the header file<code> head.h: import \"head.h\";.<\/code><\/p>\n<p><!-- HTML generated using hilite.me --><\/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;\">\/\/ head.cpp<\/span>\n\nimport <span style=\"color: #cc3300;\">\"head.h\"<\/span>;\n\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">hello<\/span>() {\n\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Hello World: header units<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\n\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n}\n<\/pre>\n<\/div>\n<p><!-- HTML generated using hilite.me --><\/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%;\">\/\/ helloWorld3.cpp\n\nimport <span style=\"color: #cc3300;\">\"head.h\"<\/span>;\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\n\n    hello();\n\n}\n<\/pre>\n<\/div>\n<p>These are the necessary step to use header units.<\/p>\n<p><!-- HTML generated using hilite.me --><\/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 <span style=\"color: #555555;\">\/<\/span>std<span style=\"color: #555555;\">:<\/span>c<span style=\"color: #555555;\">++<\/span>latest <span style=\"color: #555555;\">\/<\/span>EHsc <span style=\"color: #555555;\">\/<\/span>exportHeader head.h \ncl.exe <span style=\"color: #555555;\">\/<\/span>c <span style=\"color: #555555;\">\/<\/span>std<span style=\"color: #555555;\">:<\/span>c<span style=\"color: #555555;\">++<\/span>latest <span style=\"color: #555555;\">\/<\/span>EHsc <span style=\"color: #555555;\">\/<\/span>headerUnit head.h<span style=\"color: #555555;\">=<\/span>head.h.ifc head.cpp\ncl.exe <span style=\"color: #555555;\">\/<\/span>std<span style=\"color: #555555;\">:<\/span>c<span style=\"color: #555555;\">++<\/span>latest <span style=\"color: #555555;\">\/<\/span>EHsc <span style=\"color: #555555;\">\/<\/span>headerUnit head.h<span style=\"color: #555555;\">=<\/span>head.h.ifc helloWorld3.cpp head.obj  \n<\/pre>\n<\/div>\n<ul>\n<li>The flag<code> \/exportHeader<\/code> (first line) causes the creation of the ifc file<code> head.h.ifc<\/code> from the header file<code> head.h<\/code>. The ifc file contains the metadata description of the module interface.<\/li>\n<li>The implementation file<code> head.cpp<\/code> (second line) and the client file <code>helloWordl3.cpp<\/code> (third line) use the header unit. The flag<code> \/headerUnit head.h=head.h.ifc<\/code> imports the header and tells the compiler or linker the name of the ifc file for the specified header.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6206\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/helloWorld3.png\" alt=\"helloWorld3\" width=\"347\" height=\"149\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/helloWorld3.png 347w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/helloWorld3-300x129.png 300w\" sizes=\"auto, (max-width: 347px) 100vw, 347px\" \/><\/p>\n<div>There is one drawback with header units. Not all headers are importable. Which headers are importable is implementation-defined, but the C++ standard guarantees all standard library headers are importable. The ability to import excludes C headers.<\/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>In my <a href=\"https:\/\/www.modernescpp.com\/index.php\/from-variadic-templates-to-fold-expressions\">next post<\/a>, I will use variadic templates to implement the C++ idiom for a fully generic factory. One implementation of this life-saving C++ idiom is<code> std::make_unique<\/code>.<\/p>\n<\/div>\n<div id=\"simple-translate\">\n<div>\n<div class=\"simple-translate-button isShow\" style=\"background-image: url('moz-extension:\/\/981aa874-2db4-44d3-a97f-b02a72476831\/icons\/512.png'); height: 22px; width: 22px; top: 351px; left: 0px;\"><\/div>\n<div class=\"simple-translate-panel\" style=\"width: 300px; height: 200px; top: 0px; left: 0px; font-size: 13px; background-color: #ffffff;\">\n<div class=\"simple-translate-result-wrapper\" style=\"overflow: hidden;\">\n<div class=\"simple-translate-move\" draggable=\"draggable\"><\/div>\n<div class=\"simple-translate-result-contents\">\n<p class=\"simple-translate-result\" dir=\"auto\" style=\"color: #000000;\">\n<p class=\"simple-translate-candidate\" dir=\"auto\" style=\"color: #737373;\">\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In the last weeks, I learned something new about modules in C++20: private modules fragments and header units. Consequently, I make a short detour in this post and present these new features.<\/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-6207","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\/6207","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=6207"}],"version-history":[{"count":3,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6207\/revisions"}],"predecessor-version":[{"id":8405,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6207\/revisions\/8405"}],"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=6207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}