
{"id":799,"date":"2014-11-18T12:16:05","date_gmt":"2014-11-18T12:16:05","guid":{"rendered":"http:\/\/www.codergears.com\/Blog\/?p=799"},"modified":"2014-11-18T12:26:32","modified_gmt":"2014-11-18T12:26:32","slug":"c-needs-to-modernize-its-legacy-include-mechanism-to-be-really-a-new-language","status":"publish","type":"post","link":"https:\/\/codergears.com\/Blog\/?p=799","title":{"rendered":"C++ needs to modernize its legacy #include mechanism to be really a new language"},"content":{"rendered":"<p>C++ was stagnated for many years, and many developers was confident that the language will have the same destiny as \u00a0Cobol, Fortran and VB6. No new projects will be developed with it\u00a0\u00a0and \u00a0C++ developers will do just the maintenance of existing projects. But against all odds C++ reborn from its ashes and the new standards changes a lot how the language is used.<\/p>\n<p>But the legacy \u00a0<tt class=\"docutils literal\"><span class=\"pre\">#include<\/span><\/tt>\u00a0mechanism still there. \u00a0After the modernisation of the language, it became the \u00a0next 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<p><!--more--><\/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>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&#8217;s 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.<span style=\"color: #333333;\">\u00a0The\u00a0module import <\/span><span style=\"color: #333333;\">\u00a0loads a binary representation of the\u00a0<\/span><tt class=\"docutils literal\"><span class=\"pre\">std\u00a0<\/span><\/tt><span style=\"color: #333333;\">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<\/span><tt class=\"docutils literal\"><span class=\"pre\">std<\/span><\/tt><span style=\"color: #333333;\">\u00a0because the module itself was compiled as a separate, standalone module. <\/span><\/p>\n<p>Introducing modules in C++ will not be an easy task, the standardisation could be postponed many years, it was introduced in c++0x drafts, but each time it was reported to the next standard.\u00a0We hope that it will not be \u00a0like jisgaw (the modular system of \u00a0java) which was postponed many years.<\/p>\n<p>The good news is that Clang implemented the Module feature for ObjectiveC many years ago, and it will be easy for their developers to adapt it for C++ after the standardisation.<\/p>\n<p>Finally if you can please participate to this Poll, to have an idea of what C++ developers want.<\/p>\n<p>[poll id=&#8221;2&#8243;]<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++ was stagnated for many years, and many developers was confident that the language will have the same destiny as \u00a0Cobol, Fortran and VB6. No new projects will be developed with it\u00a0\u00a0and \u00a0C++ developers will do just the maintenance of existing projects. But against all odds C++ reborn from its ashes and the new standards &hellip; <a href=\"https:\/\/codergears.com\/Blog\/?p=799\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;C++ needs to modernize its legacy #include mechanism to be really a new language&#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":[],"tags":[],"class_list":["post-799","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/799","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=799"}],"version-history":[{"count":15,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/799\/revisions"}],"predecessor-version":[{"id":814,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/799\/revisions\/814"}],"wp:attachment":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}