{"id":171,"date":"2017-06-29T10:53:27","date_gmt":"2017-06-29T10:53:27","guid":{"rendered":"http:\/\/cppdepend.com\/wordpress\/?p=171"},"modified":"2017-08-11T16:36:58","modified_gmt":"2017-08-11T16:36:58","slug":"the-rise-of-the-new-language-mc","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/the-rise-of-the-new-language-mc\/","title":{"rendered":"The rise of the new language MC++"},"content":{"rendered":"<p>During the last few years we talk about the \u201cC++ Renaissance\u201d. We have to admit that Microsoft was a major part of this movement, I remember this <a href=\"http:\/\/channel9.msdn.com\/Shows\/Going+Deep\/Craig-Symonds-and-Mohsen-Agsen-C-Renaissance\">video<\/a> where\u00a0Craig Symonds and Mohsen Agsen talked about it.<\/p>\n<p>In 2011 Microsoft announced in many articles the comeback of\u00a0C++, and Microsoft C++ experts like Herb Sutter did many conferences to explain why C++ was\u00a0back and mostly recommended the use of Modern C++. At\u00a0the same time the standard C++11 was approved and we began to talk about C++ as a new language.<!--more--><span id=\"more-867\"><\/span><\/p>\n<p>By\u00a02011, C++ had been in use for\u00a0more than 30 years. It was not easy to convince developers that the new C++ actually simplified many frustrating facets of C++ usage, and that there was a new modern way to improve the C++ Code.<\/p>\n<p>Let\u2019s take the memory management as example, which is maybe the most criticized mechanism in C++. For many years the object allocation was done by the new keyword, and the developer must never forget to invoke delete somewhere in the code. \u00a0The \u201cModern C++\u201d resolved this issue and promotes the use of a shared pointer.<\/p>\n<p>When c++0x was announced a few years ago, I thought that it would\u00a0not impact the C++ language, but I was wrong. Take a look at this code snippet from Folly. It looks like it\u2019s developed using a new language.<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c0.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-449\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c0.png\" alt=\"c0\" width=\"677\" height=\"306\" \/><\/a><\/p>\n<p>The same situation\u00a0occurs in almost all the folly source code; its implementation looks very different to c++03 source code.<\/p>\n<p><strong>How to prevent the big impact of the C++ past?<\/strong><\/p>\n<p>There\u2019s no magic solution. We can hope that C++ compilers could help us by emitting some warnings related to the deprecated usage, like the old string manipulation (strcpy,strcat,\u2026). But this solution \u00a0will not have a big impact. The modern C++ is more new idioms to learn and practice.<\/p>\n<p>Another solution would\u00a0be to consider that a new language named\u00a0\u201c<strong>Modern C++<\/strong>\u201d should be\u00a0created. Let\u2019s do the same search on the web as before, but this time we search for \u201cModern C++\u201d object allocation, the first link\u00a0will talk about smart pointers.<\/p>\n<p><strong>Discovering the new language MC++<\/strong><\/p>\n<p>The better approach to discover the power of MC++ is to explore the source code of a mature project using its features. Folly from facebook is a very good candidate.<\/p>\n<p>Let&#8217;s discover some MC++ \u00a0features used in Folly:<\/p>\n<p><strong>1- auto<\/strong><\/p>\n<p>C++11 introduces \u00a0type inference capability using the auto keyword, which means that the compiler\u00a0infer the type of a variable\u00a0at the point of declaration. Folly uses \u00a0auto for almost all its variable declarations, here\u2019s an example from its source code<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-439\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c1.png\" alt=\"c1\" width=\"596\" height=\"300\" \/><\/a><\/p>\n<p>Using the auto keyword allows you\u00a0to spend less time having to write out things the compiler already knows.<\/p>\n<p><strong>2- nullptr<\/strong><\/p>\n<p>The constant\u00a00\u00a0has had the double role of constant integer and null pointer constant.C++11 corrects this by introducing a new keyword to serve as a distinguished null pointer constant:nulptr<\/p>\n<p>In the Folly source code all null pointers are represented by the new keyword nullptr, there \u2018s no place where the constant 0 is \u00a0used.<\/p>\n<p><strong>3- shared_ptr<\/strong><\/p>\n<p>Smart pointer is not a new concept, many libraries implemented it\u00a0many years ago. The popular one is <a href=\"http:\/\/www.boost.org\/doc\/libs\/1_50_0\/libs\/smart_ptr\/shared_ptr.htm\">boost::shared_ptr<\/a>.\u00a0What\u2019s new is its standardization and no further\u00a0use of an external library to work with smart pointers.<\/p>\n<p>Folly uses the standardized shared pointer extensively, only a few raw pointers remain in its source code.<\/p>\n<p><strong>4-\u00a0Strongly-typed enums<\/strong><\/p>\n<p>\u201cTraditional\u201d enums in C++ export their enumerators in the surrounding scope ,which can lead to name collisions if two different enums in the same have scope defined enumerators with the same name,<\/p>\n<p>C++11 introduces the enum class keywords. They no longer export their enumerators in the surrounding scope. Moreover\u00a0we can also now inherit from an enum.<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-441\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c2.png\" alt=\"c2\" width=\"679\" height=\"88\" \/><\/a><\/p>\n<p><strong>5- static assert<\/strong><\/p>\n<p>C++11 introduces a new way to test assertions at compile-time, using the new keyword static_assert. This feature is very useful to add conditions to the template parameters, as shown in this template class from Folly source code:<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-442\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c3.png\" alt=\"c3\" width=\"733\" height=\"213\" \/><\/a><\/p>\n<p><strong>6-\u00a0<b>Variadic template<\/b><\/strong><\/p>\n<p>Variadic template\u00a0is a template, which can take an arbitrary number of template arguments of any type. Both the classes &amp; functions can be variadic. Folly defines many variadic templates. Here are two variadic template functions from the Folly source code:<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-443\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c4.png\" alt=\"c4\" width=\"751\" height=\"175\" \/><\/a><\/p>\n<p><strong>7- Range-based for loops<\/strong><\/p>\n<p>C++11 augmented the for statement to support the \u201cforeach\u201d paradigm of iterating over collections. It makes the code more simpler and cleaner. Folly uses this feature extensively. Here\u2019s an example:<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-455\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c6.png\" alt=\"c6\" width=\"705\" height=\"173\" \/><\/a><\/p>\n<p><strong>8-Initializer lists<\/strong><\/p>\n<p>In C++03 Initializer lists concern only arrays. In C++11\u00a0they are not just for arrays anymore. The mechanism for accepting a\u00a0<b>{}<\/b>-list is a function (often a constructor) accepting an argument of type\u00a0<b>std::initializer_list&lt;T&gt;<\/b>. Here\u2019s an example of function accepting std::initializer_list as argument<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c7.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-456\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c7.png\" alt=\"c7\" width=\"708\" height=\"74\" \/><\/a><\/p>\n<p>And here\u2019s how it\u2019s invoked<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c8.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-457\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c8.png\" alt=\"c8\" width=\"710\" height=\"131\" \/><\/a><\/p>\n<p><strong>9- noexcept<\/strong><\/p>\n<p>If a function cannot throw an exception or if the program isn\u2019t written to handle exceptions thrown by a function, that function can be declared\u00a0<b>noexcept.<\/b><\/p>\n<p>Here\u2019s an example from Folly source code<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c9.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-460\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c9.png\" alt=\"c9\" width=\"749\" height=\"88\" \/><\/a><\/p>\n<p><strong>10- move<\/strong><\/p>\n<p>C++11 has introduced the concept of rvalue references (specified with &amp;&amp;) to differentiate a reference to an lvalue or an rvalue. An lvalue is an object that has a name, while an rvalue is an object that does not have a name (a temporary object). The move semantics allow modifying of rvalues.<\/p>\n<p>For that C++11 introduces two new special member functions: the\u00a0<em>move constructor\u00a0<\/em>and the\u00a0<em>move assignment operator<\/em>.<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c12.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-466\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c12.png\" alt=\"c12\" width=\"730\" height=\"81\" \/><\/a><\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c10.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-465\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c10.png\" alt=\"c10\" width=\"731\" height=\"225\" \/><\/a><\/p>\n<p>Here\u2019s a <a href=\"http:\/\/www.stroustrup.com\/move.pdf\">good document<\/a> that better explains better the benefits of move semantics.<\/p>\n<p><strong>11-lambda<\/strong><\/p>\n<p>C++11 provides the ability to create\u00a0anonymous functions, called lambda functions, you can refer <a href=\"http:\/\/www.stroustrup.com\/C++11FAQ.html#lambda\">here<\/a> for more details about this new feature.<\/p>\n<p>Folly uses it in many functions. Here\u2019s an example from its source code:<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c14.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-467\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c14.png\" alt=\"c14\" width=\"623\" height=\"417\" \/><\/a><\/p>\n<p><strong>12-\u00a0Explicitly defaulted and deleted special member functions<\/strong><\/p>\n<p>In C++03, the compiler provides, for classes that do not provide them for themselves, a default constructor, a copy constructor, a copy assignment operator (<code>operator=<\/code>), and a destructor. The programmer can override these defaults by defining custom versions.<\/p>\n<p>However, there is very little control over the creation of these defaults. Making a class inherently non-copyable, for example, requires declaring a private copy constructor and copy assignment operator and not defining them.<\/p>\n<p>In C++11, certain features can be explicitly disabled. For example, the following type is non-copyable, which makes the code more simple and clean.<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c15.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-468\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c15.png\" alt=\"c15\" width=\"725\" height=\"102\" \/><\/a><\/p>\n<p><strong>13- override identifier<\/strong><\/p>\n<p>In C++03, it is possible to accidentally create a new virtual function, when one intended to override a base class function.<\/p>\n<p>The\u00a0<code>override<\/code>\u00a0special identifier means that the compiler will check the base class(es) to see if there is a virtual function with this exact signature, and if there is not, the compiler will indicate an error.<\/p>\n<p>Folly uses this new feature extensively:<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c18.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-469\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c18.png\" alt=\"c18\" width=\"670\" height=\"358\" \/><\/a><\/p>\n<p><strong>14- std::thread<\/strong><\/p>\n<p>A thread class (<code>std::thread<\/code>) is provided which takes a\u00a0function object\u00a0\u2014 and an optional series of arguments to pass to it \u2014 to run in the new thread.<\/p>\n<p>In C++11 working with threads is more simplified.\u00a0This is the new standard way to define a new thread, taken from Folly source code:<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c20.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-470\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c20.png\" alt=\"c20\" width=\"708\" height=\"301\" \/><\/a><\/p>\n<p><strong>15-\u00a0Unordered containers<\/strong><\/p>\n<p>A unordered container is a kind of hash table. C++11 offers four standard ones:<\/p>\n<ul>\n<li>unordered_map<\/li>\n<li>unordered_set<\/li>\n<li>unordered_multimap<\/li>\n<li>unordered_multiset<\/li>\n<\/ul>\n<p>Folly uses these new containers\u00a0in many places<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c21.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-471\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c21.png\" alt=\"c21\" width=\"738\" height=\"90\" \/><\/a><\/p>\n<p><strong>Conclusion:<\/strong><\/p>\n<p>Some\u00a0advice to new C++ developers: consider that C++ has\u00a0changed its name, and make sur all your search inquiries\u00a0on the web always use \u201cModern C++\u201d instead of C++. The results will be very different and using C++ will mostly give you the past practices.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>During the last few years we talk about the \u201cC++ Renaissance\u201d. We have to admit that Microsoft was a major part of this movement, I remember this video where\u00a0Craig Symonds and Mohsen Agsen talked about it. In 2011 Microsoft announced in many articles the comeback of\u00a0C++, and Microsoft C++ experts like Herb Sutter did many &hellip; <a href=\"https:\/\/cppdepend.com\/blog\/the-rise-of-the-new-language-mc\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;The rise of the new language MC++&#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":[12,30],"class_list":["post-171","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c11","tag-modern-c"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/171","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=171"}],"version-history":[{"count":6,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/171\/revisions"}],"predecessor-version":[{"id":246,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/171\/revisions\/246"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}