{"id":319,"date":"2018-02-09T23:24:02","date_gmt":"2018-02-09T23:24:02","guid":{"rendered":"http:\/\/cppdepend.com\/blog\/?p=319"},"modified":"2023-05-31T18:13:30","modified_gmt":"2023-05-31T18:13:30","slug":"15-essential-c11-features-to-enhance-your-c-projects","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/15-essential-c11-features-to-enhance-your-c-projects\/","title":{"rendered":"15 Essential C++11 Features to Enhance Your C++ Projects."},"content":{"rendered":"<p>Over recent years, there has been much discussion about the &#8220;Renaissance of C++&#8221;. It&#8217;s undeniable that Microsoft played a significant role in this resurgence.<\/p>\n<p>In 2011 Microsoft announced in many articles the comeback of C++, and Microsoft C++ experts like Herb Sutter attended many conferences to explain why C++ was back and mostly recommended the use of Modern C++. At the same time, the standard C++11 was approved and we began to talk about C++ as a new language.<!--more--><span id=\"more-171\"><\/span><span id=\"more-867\"><\/span><\/p>\n<p>By&nbsp;2011, C++ had been in use for&nbsp;more 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>For the C++ developers who do not yet switch to C++11, they can use clang-tidy to get suggestions on where they can modernize the codebase or try the <a href=\"https:\/\/www.cppdepend.com\/documentation\/modernize-legacy-code\">CppDepend&nbsp;modernization feature<\/a>. And here are the 15 features mostly used in the C++ open-source libraries.<\/p>\n<p>Let&#8217;s take as an example &nbsp;<a href=\"https:\/\/github.com\/facebook\/folly\">Folly<\/a>&nbsp;released six&nbsp;years ago by Facebook,&nbsp;Folly is a&nbsp;large collection of reusable C++ library components that internally at Facebook are used extensively. And&nbsp;&nbsp;here\u2019s the motivations from their website behind &nbsp;its utility:<\/p>\n<blockquote><p>Folly (acronymed loosely after Facebook Open Source Library) is a library of C++11 components designed with practicality and efficiency in mind. It complements (as opposed to competing against) offerings such as Boost and of course&nbsp;<code>std<\/code>. In fact, we embark on defining our own component only when something we need is either not available, or does not meet the needed performance profile.<\/p><\/blockquote>\n<p>Let&#8217;s explore from its source code 15 C++11 features:<\/p>\n<p><strong>1- auto<\/strong><\/p>\n<p>C++11 introduces type inference capability using the auto keyword, which means that the compiler&nbsp;infers the type of a variable&nbsp;at the point of declaration. Folly uses &nbsp;auto&nbsp;for almost all its variable declarations, here\u2019s an example from its source code<\/p>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/c1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-439\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/c1.png\" alt=\"c1\" width=\"596\" height=\"300\"><\/a><\/p>\n<p>Using the auto keyword permits you to spend less time having to write out things the compiler already knows.<\/p>\n<p><strong>2- nullptr<\/strong><\/p>\n<p>The constant&nbsp;0&nbsp;has 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, the new keyword nullptr represents all null pointers, there\u2018s no place where the constant 0 is used.<\/p>\n<p><strong>3- shared_ptr<\/strong><\/p>\n<p>The smart pointer is not a new concept, many libraries implemented it&nbsp;many years ago, the popular one is&nbsp;<a href=\"http:\/\/www.boost.org\/doc\/libs\/1_50_0\/libs\/smart_ptr\/shared_ptr.htm\">boost::shared_ptr<\/a>, what\u2019s new is its standardization, and no need anymore to use an external library to work with smart pointers.<\/p>\n<p>Folly uses extensively the standardized shared pointer, only a few raw pointers remain in its source code.<\/p>\n<p><strong>4-&nbsp;Strongly-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 scope define 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,&nbsp;we can also now inherit from an enum.<\/p>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/c2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-441\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/c2.png\" alt=\"c2\" width=\"679\" height=\"88\"><\/a><\/p>\n<p>[adrotate banner=&#8221;3&#8243;]<\/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.javadepend.com\/Blog\/wp-content\/uploads\/c3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-442\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/c3.png\" alt=\"c3\" width=\"733\" height=\"213\"><\/a><\/p>\n<p><strong>6-&nbsp;<b>Variadic template<\/b><\/strong><\/p>\n<p>The variadic template&nbsp;is 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.javadepend.com\/Blog\/wp-content\/uploads\/c4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-443\" src=\"http:\/\/www.javadepend.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 &#8220;for&#8221; statement to support the \u201cforeach\u201d paradigm of iterating over collections. it makes the code more simple and cleaner. Folly uses this feature extensively, here\u2019s an example of Folly<\/p>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/c6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-455\" src=\"http:\/\/www.javadepend.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 are not just for arrays anymore. The mechanism for accepting a&nbsp;<b>{}<\/b>-list is a function (often a constructor) accepting an argument of type&nbsp;<b>std::initializer_list&lt;T&gt;<\/b>. Here\u2019s an example of a function accepting std::initializer_list as an argument<\/p>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/c7.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-456\" src=\"http:\/\/www.javadepend.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.javadepend.com\/Blog\/wp-content\/uploads\/c8.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-457\" src=\"http:\/\/www.javadepend.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&nbsp;<b>noexcept.<\/b><\/p>\n<p>Here\u2019s an example from Folly&#8217;s source code<\/p>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/c9.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-460\" src=\"http:\/\/www.javadepend.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 rvalues.<\/p>\n<p>For that, C++11 introduces two new special member functions: the <em>move constructor&nbsp;<\/em>and the&nbsp;<em>move assignment operator<\/em>.<\/p>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/c12.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-466\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/c12.png\" alt=\"c12\" width=\"730\" height=\"81\"><\/a><\/p>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/c10.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-465\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/c10.png\" alt=\"c10\" width=\"731\" height=\"225\"><\/a><\/p>\n<p>Here\u2019s a&nbsp;<a href=\"http:\/\/www.stroustrup.com\/move.pdf\">good document<\/a>&nbsp;that explains better the benefits of move semantics.<\/p>\n<p><strong>11-lambda<\/strong><\/p>\n<p>C++11 provides the ability to create&nbsp;anonymous functions, called <a href=\"http:\/\/www.stroustrup.com\/C++11FAQ.html#lambda\">lambda functions<\/a>.<\/p>\n<p>Folly uses it in many functions, here\u2019s an example from its source code:<\/p>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/c14.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-467\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/c14.png\" alt=\"c14\" width=\"623\" height=\"417\"><\/a><\/p>\n<p><strong>12-&nbsp;Explicitly 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.javadepend.com\/Blog\/wp-content\/uploads\/c15.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-468\" src=\"http:\/\/www.javadepend.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&nbsp;<code>override<\/code>&nbsp;special 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 extensively this new feature:<\/p>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/c18.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-469\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/c18.png\" alt=\"c18\" width=\"670\" height=\"358\"><\/a><\/p>\n<p>[adrotate banner=&#8221;3&#8243;]<\/p>\n<p><strong>14- std::thread<\/strong><\/p>\n<p>A thread class (<code>std::thread<\/code>) is provided which takes a&nbsp;function object&nbsp;\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, here\u2019s from Folly source code the new standard way to define a new thread:<\/p>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/c20.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-470\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/c20.png\" alt=\"c20\" width=\"708\" height=\"301\"><\/a><\/p>\n<p><strong>15-&nbsp;Unordered containers<\/strong><\/p>\n<p>An 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 in many places these new containers<\/p>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/c21.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-471\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/c21.png\" alt=\"c21\" width=\"738\" height=\"90\"><\/a><\/p>\n<p>[adrotate banner=&#8221;3&#8243;]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Over recent years, there has been much discussion about the &#8220;Renaissance of C++&#8221;. It&#8217;s undeniable that Microsoft played a significant role in this resurgence. In 2011 Microsoft announced in many articles the comeback of C++, and Microsoft C++ experts like Herb Sutter attended many conferences to explain why C++ was back and mostly recommended the &hellip; <a href=\"https:\/\/cppdepend.com\/blog\/15-essential-c11-features-to-enhance-your-c-projects\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;15 Essential C++11 Features to Enhance Your C++ Projects.&#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":[178,12,13],"class_list":["post-319","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-15f-in-c","tag-c11","tag-cpp"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/319","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=319"}],"version-history":[{"count":12,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/319\/revisions"}],"predecessor-version":[{"id":1462,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/319\/revisions\/1462"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}