{"id":1035,"date":"2018-04-09T12:16:52","date_gmt":"2018-04-09T12:16:52","guid":{"rendered":"http:\/\/cppdepend.com\/blog\/?p=1035"},"modified":"2018-04-09T12:36:00","modified_gmt":"2018-04-09T12:36:00","slug":"the-best-of-c-is-whats-coming","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/the-best-of-c-is-whats-coming\/","title":{"rendered":"The best of C++ is what&#8217;s coming"},"content":{"rendered":"<p>Since 2011 many new features were added to the standards, some of them are now very used like auto and nullptr, some others are rarely used. However, the most important changes that can bring the language to the next level are in the pipe.<!--more--><\/p>\n<p><strong>Modules<\/strong><\/p>\n<p>The legacy \u00a0<tt class=\"docutils literal\"><span class=\"pre\">#include<\/span><\/tt>\u00a0mechanism is still there and it has many disadvantages, here are some of them from this interesting\u00a0<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>Modules improve access to libraries with 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:<\/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\u00a0cleaner.\u00a0The\u00a0module import loads 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><strong>MetaClass<\/strong><\/p>\n<p>Herb Sutter\u00a0contributes for many years to make C++ a better language, and the last year he proposes the metaclasses feature. here&#8217;s from his <a href=\"https:\/\/herbsutter.com\/2017\/07\/26\/metaclasses-thoughts-on-generative-c\/\">post<\/a> the motivation behind adding it:<\/p>\n<p><em>I\u2019ve been working on an experimental new C++ language feature tentatively called \u201cmetaclasses\u201d that aims to make C++ programming both more powerful and simpler.<\/em><\/p>\n<p>And here&#8217;s from the <a href=\"https:\/\/herbsutter.files.wordpress.com\/2017\/07\/p0707r1.pdf\">proposal<\/a>, a definition of metaclasses and the benefits of using it:<\/p>\n<p><em>Metaclasses(provisional name) let programmers write a new kind of efficient abstraction: a user-defined named subset of classes that share common characteristics \u2013 including user-defined rules, defaults, and generated functions \u2013 by writing a custom transformation from normal C++ source code to a normal C++ class definition. There is no type system bifurcation; the generated class is a normal class. <\/em><\/p>\n<p><em>Primary goals: <\/em><\/p>\n<p><em>\u2022 Expand C++\u2019s abstraction vocabulary beyond class\/struct\/union\/enum which are the type categories hardwired into the language. <\/em><\/p>\n<p><em>\u2022 Enable providing longstanding best practices as reusable libraries instead of English guides\/books, to have an easily adopted vocabulary (e.g., interface, value) instead of lists of rules to be memorized (e.g., remember this coding pattern to write an abstract base class or value type, relying on tools to find mistakes). <\/em><\/p>\n<p><em>\u2022 Enable writing compiler-enforced patterns for any purpose: coding standards(e.g., many Core Guidelines \u201cenforce\u201d rules), API requirements (e.g., rules a class must follow to work with a hardware interface library, a browser extension, a callback mechanism), and any other pattern for classes. <\/em><\/p>\n<p><em>\u2022 Enable writing many new \u201cspecialized types\u201d features(e.g., as we did in C++11 with enum class) as ordinary library code instead of pseudo-English <\/em>standardese<em>, with equal usability and efficiency, so that they can be unit-tested and debugged using normal tools, developed\/distributed without updating\/shipping a new compiler, and go through LEWG\/LWG as code instead of EWG\/CWG as <\/em>standardese<em>. As a consequence, enable standardizing valuable extensions that we\u2019d likely never standardize in the core language because they are too narrow (e.g., interface), but could readily standardize as a small self-contained library. <\/em><\/p>\n<p><em>\u2022 Eliminate the need to invent non-C++ \u201cside languages\u201d and special compilers, such as Qt moc, COM MIDL, and C++\/CX, to express the information their systems need but cannot be expressed in today\u2019s C++ (such as specialized types for properties, event callbacks, and similar abstractions).<\/em><\/p>\n<p>With Modules and Metaclasses, the C++ will be more powerful and easy to use, and in the future, many new features will be added. what we can say is Thanks to all the C++ contributors who help to improve the language and long life to\u00a0 C++ \ud83d\ude42<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since 2011 many new features were added to the standards, some of them are now very used like auto and nullptr, some others are rarely used. However, the most important changes that can bring the language to the next level are in the pipe.<\/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":[13],"class_list":["post-1035","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-cpp"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/1035","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=1035"}],"version-history":[{"count":10,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/1035\/revisions"}],"predecessor-version":[{"id":1053,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/1035\/revisions\/1053"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=1035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=1035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=1035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}