{"id":20,"date":"2016-10-14T09:13:43","date_gmt":"2016-10-14T09:13:43","guid":{"rendered":"http:\/\/cppdepend.com\/wordpress\/?p=20"},"modified":"2018-02-09T19:11:26","modified_gmt":"2018-02-09T19:11:26","slug":"learn-from-folly-source-code-the-new-c11-features","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/learn-from-folly-source-code-the-new-c11-features\/","title":{"rendered":"Learn from Folly source code the new C++11 features."},"content":{"rendered":"<p>Six\u00a0years ago Facebook released their C++ library named <a href=\"https:\/\/github.com\/facebook\/folly\">Folly<\/a>\u00a0, it&#8217;s a\u00a0<span style=\"color: #000000;\">large collection of reusable C++ library components that internally at Facebook are used extensively.<\/span><\/p>\n<p>But many mature C++ open source libraries exist, why introduce another one ? Here&#8217;s the motivations from their website behind \u00a0its utility:<\/p>\n<blockquote><p><span style=\"color: #333333;\">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\u00a0<\/span><code style=\"color: #333333;\">std<\/code><span style=\"color: #333333;\">. 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.<\/span><\/p><\/blockquote>\n<p><!--more--><\/p>\n<p>Here&#8217;s for example a <a href=\"https:\/\/github.com\/facebook\/folly\/blob\/master\/folly\/docs\/FBVector.md\">detailled explanation<\/a> why Folly introduced another vector class FBVector. And as claimed by the Folly developers, it&#8217;s a library of C++11 components, which is totally confirmed if you take a look in their source code,\u00a0the C++11 features are <span style=\"color: #000000;\">extensively used, Moreover almost all new C++11 features are used.\u00a0<\/span><\/p>\n<p>When c++0x was announced few years ago, I thought that it will not impact a lot the C++ language, but I was wrong, take a look at this code snippet from Folly, it looks like it&#8217;s 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 remark concern almost all the folly source code, \u00a0its implementation \u00a0looks different to \u00a0c++03 source code.<\/p>\n<p>For developers \u00a0interested to master the\u00a0new C++11 features, a better\u00a0approach is to discover how mature libraries \u00a0use them.Folly is a very good candidate to explore the new features. Let&#8217;s discover some of them\u00a0from\u00a0its\u00a0source code.<\/p>\n<p><strong>1- auto<\/strong><\/p>\n<p><span style=\"color: #000000;\">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 <\/span>for almost all its variable declarations, here&#8217;s 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\u00a0permits to\u00a0<span style=\"color: #000000;\">\u00a0spend less time having to write out things the compiler already knows.<\/span><\/p>\n<p><strong>2- nullptr<\/strong><\/p>\n<p>T<span style=\"color: #252525;\">he constant\u00a00<\/span><span style=\"color: #252525;\">\u00a0has had the double role of constant integer and null pointer constant.<\/span><span style=\"color: #252525;\">C++11 corrects this by introducing a new keyword to serve as a distinguished null pointer constant:nulptr<\/span><\/p>\n<p>In the Folly source code all null pointers are represented by the new keyword nullptr, there &#8216;s 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>, what&#8217;s new is its\u00a0\u00a0standardization, 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 few raw pointers remain in its source code.<\/p>\n<p><strong>4-\u00a0Strongly-typed enums<\/strong><\/p>\n<p><span style=\"color: #111111;\">&#8220;Traditional&#8221; 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 define enumerators with the same name,<\/span><\/p>\n<p><span style=\"color: #111111;\">C++11 introduces the \u00a0enum class\u00a0\u00a0keywords. They no longer export their enumerators in the surrounding scope. Moreover\u00a0<\/span>we 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><span style=\"color: #000000;\">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.<\/span>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><span style=\"color: #111111;\">C++11 augmented the for statement to support the &#8220;foreach&#8221; paradigm of iterating over collections. it makes the code more simple and cleaner. Folly uses extensively this feature, h<\/span>ere&#8217;s an example for Folly<\/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><span style=\"color: #000000;\">In C++03 Initializer lists concern only arrays, in C++11 are not just for arrays any more. The mechanism for accepting a\u00a0<\/span><b>{}<\/b><span style=\"color: #000000;\">-list is a function (often a constructor) accepting an argument of type\u00a0<\/span><b>std::initializer_list&lt;T&gt;<\/b><span style=\"color: #000000;\">. Here&#8217;s an example of function accepting std::initializer_list as argument<\/span><\/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&#8217;s how it&#8217;s 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><span style=\"color: #000000;\">If a function cannot throw an exception or if the program isn&#8217;t written to handle exceptions thrown by a function, that function can be declared\u00a0<\/span><b>noexcept.<\/b><\/p>\n<p>Here&#8217;s 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><span style=\"color: #111111;\">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.<\/span><\/p>\n<p><span style=\"color: #333333;\">For that C++11 introduces two new special member functions: the\u00a0<\/span><em style=\"color: #333333;\">move constructor\u00a0<\/em><span style=\"color: #333333;\">and the\u00a0<\/span><em style=\"color: #333333;\">move assignment operator<\/em><span style=\"color: #333333;\">.<\/span><\/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&#8217;s a <a href=\"http:\/\/www.stroustrup.com\/move.pdf\">good document<\/a> that explains better the benefits of\u00a0\u00a0move semantics.<\/p>\n<p><strong>11-lambda<\/strong><\/p>\n<p><span style=\"color: #252525;\">C++11 provides the ability to create\u00a0anonymous functions<\/span><span style=\"color: #252525;\">, 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.<\/span><\/p>\n<p>Folly uses it in many functions , here&#8217;s 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><span style=\"color: #252525;\">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 (<\/span><code style=\"color: black;\">operator=<\/code><span style=\"color: #252525;\">), and a destructor. The programmer can override these defaults by defining custom versions.<\/span><\/p>\n<p><span style=\"color: #252525;\">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.<\/span><\/p>\n<p><span style=\"color: #252525;\">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.\u00a0<\/span><\/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><span style=\"color: #252525;\">In C++03, it is possible to accidentally create a new virtual function, when one intended to override a base class function.<\/span><\/p>\n<p><span style=\"color: #252525;\">The\u00a0<\/span><code style=\"color: black;\">override<\/code><span style=\"color: #252525;\">\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.<\/span><\/p>\n<p>Folly uses extensively this new feature:<\/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><span style=\"color: #252525;\">A thread class (<\/span><code style=\"color: black;\">std::thread<\/code><span style=\"color: #252525;\">) is provided which takes a\u00a0function object<\/span><span style=\"color: #252525;\">\u00a0\u2014 and an optional series of arguments to pass to it \u2014 to run in the new thread.<\/span><\/p>\n<p><span style=\"color: #252525;\">In C++11 working with threads is more simplified, here&#8217;s from Folly source code\u00a0the\u00a0new standard way to defines a new thread:\u00a0<\/span><\/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><span style=\"color: #000000;\">A unordered container is a kind of hash table. C++11 offers four standard ones:<\/span><\/p>\n<ul style=\"color: #000000;\">\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.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>Folly uses almost all the new C++11 features, exploring its source code will give you a better idea of the new C++ capabilities, and how they are implemented.<\/p>\n<p>I encourage any C++ developer interested in the C++11 to downlad the Folly source code and discover the power of the new language.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Six\u00a0years ago Facebook released their C++ library named Folly\u00a0, it&#8217;s a\u00a0large collection of reusable C++ library components that internally at Facebook are used extensively. But many mature C++ open source libraries exist, why introduce another one ? Here&#8217;s the motivations from their website behind \u00a0its utility: Folly (acronymed loosely after Facebook Open Source Library) is &hellip; <a href=\"https:\/\/cppdepend.com\/blog\/learn-from-folly-source-code-the-new-c11-features\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Learn from Folly source code the new C++11 features.&#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,13,14,16,15],"class_list":["post-20","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c11","tag-cpp","tag-cpp11","tag-facebook","tag-folly"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/20","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=20"}],"version-history":[{"count":2,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/20\/revisions"}],"predecessor-version":[{"id":318,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/20\/revisions\/318"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=20"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=20"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=20"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}