{"id":228,"date":"2017-07-15T23:27:35","date_gmt":"2017-07-15T23:27:35","guid":{"rendered":"http:\/\/cppdepend.com\/wordpress\/?p=228"},"modified":"2018-11-14T10:56:49","modified_gmt":"2018-11-14T10:56:49","slug":"why-the-c-modules-feature-is-very-important-for-the-c-future","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/why-the-c-modules-feature-is-very-important-for-the-c-future\/","title":{"rendered":"Why the C++ modules feature is very important for the C++ future?"},"content":{"rendered":"<p>C++ have\u00a0stagnated for many years, and many developers were\u00a0confident that the language will have the same destiny as Cobol, Fortran and VB6. No new projects will be developed with it and \u00a0C++ developers will just do the maintenance of the existing projects. But against all odds, C++ reborn from its ashes and the new standards changes a lot how the language is used.<!--more--><\/p>\n<p>But the legacy \u00a0<tt class=\"docutils literal\"><span class=\"pre\">#include<\/span><\/tt>\u00a0mechanism is still there. \u00a0After the modernization of the language, it became the next weak link to improve. Indeed, it has many disadvantages, here are some of them from this interesting <a href=\"http:\/\/clang.llvm.org\/docs\/Modules.html#problems-with-the-current-model\">document<\/a>.<\/p>\n<ul class=\"simple\">\n<li><strong>Compile-time scalability<\/strong>: Each time a header is included, the compiler must preprocess and parse the text in that header and every header it includes, transitively. This process must be repeated for every translation unit in the application, which involves a huge amount of redundant work. In a project with\u00a0<em>N<\/em>translation units and\u00a0<em>M<\/em>\u00a0headers included in each translation unit, the compiler is performing\u00a0<em>M x N<\/em>\u00a0work even though most of the\u00a0<em>M<\/em>\u00a0headers are shared among multiple translation units. C++ is particularly bad, because the compilation model for templates forces a huge amount of code into headers.<\/li>\n<li><strong>Fragility<\/strong>:\u00a0<tt class=\"docutils literal\"><span class=\"pre\">#include<\/span><\/tt>\u00a0directives are treated as textual inclusion by the preprocessor, and are therefore subject to any active macro definitions at the time of inclusion. If any of the active macro definitions happens to collide with a name in the library, it can break the library API or cause compilation failures in the library header itself. For an extreme example,\u00a0<tt class=\"docutils literal\"><span class=\"pre\">#define<\/span>\u00a0<span class=\"pre\">std<\/span>\u00a0<span class=\"pre\">\"The<\/span>\u00a0<span class=\"pre\">C++<\/span>\u00a0<span class=\"pre\">Standard\"<\/span><\/tt>\u00a0and then include a standard library header: the result is a horrific cascade of failures in the C++ Standard Library\u2019s implementation. More subtle real-world problems occur when the headers for two different libraries interact due to macro collisions, and users are forced to reorder\u00a0<tt class=\"docutils literal\"><span class=\"pre\">#include<\/span><\/tt>\u00a0directives or introduce\u00a0<tt class=\"docutils literal\"><span class=\"pre\">#undef<\/span><\/tt>\u00a0directives to break the (unintended) dependency.<\/li>\n<li><strong>Conventional workarounds<\/strong>: C programmers have adopted a number of conventions to work around the fragility of the C preprocessor model. Include guards, for example, are required for the vast majority of headers to ensure that multiple inclusion doesn\u2019t break the compile. Macro names are written with<tt class=\"docutils literal\"><span class=\"pre\">LONG_PREFIXED_UPPERCASE_IDENTIFIERS<\/span><\/tt>\u00a0to avoid collisions, and some library\/framework developers even use\u00a0<tt class=\"docutils literal\"><span class=\"pre\">__underscored<\/span><\/tt>\u00a0names in headers to avoid collisions with \u201cnormal\u201d names that (by convention) shouldn\u2019t even be macros. These conventions are a barrier to entry for developers coming from non-C languages, are boilerplate for more experienced developers, and make our headers far uglier than they should be.<\/li>\n<li><strong>Tool confusion<\/strong>: In a C-based language, it is hard to build tools that work well with software libraries, because the boundaries of the libraries are not clear. Which headers belong to a particular library, and in what order should those headers be included to guarantee that they compile correctly? Are the headers C, C++, Objective-C++, or one of the variants of these languages? What declarations in those headers are actually meant to be part of the API, and what declarations are present only because they had to be written as part of the header file?<\/li>\n<\/ul>\n<p>The solution \u00a0addressing\u00a0 these problems was already introduced in the first <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2006\/n2073.pdf\">drafts of C++0x<\/a>, it\u00a0concern the Modules feature. However it was postponned for each new standard specification.<\/p>\n<p>[adrotate banner=&#8221;3&#8243;]<\/p>\n<p>&nbsp;<\/p>\n<p>Modules improve access to libraries \u00a0with more robust and more efficient semantic model. From the user\u2019s perspective, the code looks only slightly different, because one uses an\u00a0<tt class=\"docutils literal\"><span class=\"pre\">import<\/span><\/tt>\u00a0declaration rather than a\u00a0<tt class=\"docutils literal\"><span class=\"pre\">#include<\/span><\/tt>\u00a0preprocessor directive, here\u2019s an example from the C++0x draft:<\/p>\n<pre>import std; \/\/ Module import directive.\r\nint main() {\r\n    std::cout &lt;&lt; \u201cHello World\\n\u201d;\r\n}\r\n<\/pre>\n<p>No need to include many files of STL, just one import is sufficient, the code became more cleaner.\u00a0The\u00a0module import \u00a0loads a binary representation of the\u00a0<tt class=\"docutils literal\"><span class=\"pre\">std\u00a0<\/span><\/tt>module and makes its API available to the application directly. Preprocessor definitions that precede the import declaration have no impact on the API provided by\u00a0<tt class=\"docutils literal\"><span class=\"pre\">std<\/span><\/tt>\u00a0because the module itself was compiled as a separate, standalone module.<\/p>\n<p>Here&#8217;s an interesting post to show how to develop a C++ module using VS2017<\/p>\n<blockquote data-secret=\"5HdlvyeQz0\" class=\"wp-embedded-content\"><p><a href=\"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2017\/05\/05\/cpp-modules-in-visual-studio-2017\/\">Using C++ Modules in Visual Studio 2017<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" src=\"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2017\/05\/05\/cpp-modules-in-visual-studio-2017\/embed\/#?secret=5HdlvyeQz0\" data-secret=\"5HdlvyeQz0\" width=\"600\" height=\"338\" title=\"&#8220;Using C++ Modules in Visual Studio 2017&#8221; &#8212; Visual C++ Team Blog\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p>Conclusion:<\/p>\n<p>The C++ modules will remove the include mechanism hell and will make the use of the other libraries very easy like C# or Java.<\/p>\n<p>Try to create your first module and discover this amazing feature.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++ have\u00a0stagnated for many years, and many developers were\u00a0confident that the language will have the same destiny as Cobol, Fortran and VB6. No new projects will be developed with it and \u00a0C++ developers will just do the maintenance of the existing projects. But against all odds, C++ reborn from its ashes and the new standards &hellip; <a href=\"https:\/\/cppdepend.com\/blog\/why-the-c-modules-feature-is-very-important-for-the-c-future\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Why the C++ modules feature is very important for the C++ future?&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[7,37],"class_list":["post-228","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c","tag-c-modules"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/comments?post=228"}],"version-history":[{"count":8,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/228\/revisions"}],"predecessor-version":[{"id":1200,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/228\/revisions\/1200"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}