{"id":598,"date":"2018-03-06T22:02:28","date_gmt":"2018-03-06T22:02:28","guid":{"rendered":"http:\/\/cppdepend.com\/blog\/?p=598"},"modified":"2023-05-31T16:09:26","modified_gmt":"2023-05-31T16:09:26","slug":"modernize-cpp-codebase-banned-features-guide","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/modernize-cpp-codebase-banned-features-guide\/","title":{"rendered":"Modernize C++ Codebase: Banned C++ Features Guide"},"content":{"rendered":"<p>The refactoring is defined as the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure.<\/p>\n<p>We can enumerate\u00a0three\u00a0decisions made by the managers concerning the code refactoring:<!--more--><\/p>\n<ul>\n<li>\u00a0<strong>Iterative refactoring<\/strong>:\u00a0 Your\u00a0application can\u2019t be developed perfectly at the first iteration, even if the team has the best of the architects, designers, and developers. The easy way to do refactoring without investing a lot of money and without wasting time is to integrate it into the development process and do it after each iteration.<\/li>\n<\/ul>\n<ul>\n<li>\u00a0<strong>Refactoring when it&#8217;s necessary<\/strong>: after the application is deployed, there are some\u00a0 bugs reported, if resolving them take a lot of time or some clients needs are very complex to develop and integrate to the existing system, a refactoring can be a good solution to improve the quality of the code base, but in this case it can be very risky and we have to take care to avoid the regression in the existing code.<\/li>\n<li>\u00a0<strong>Not refactor<\/strong>: sometimes even if there are many problems in the existing application, a refactoring is never begun, because the boss does not want to invest in this process, and the support team has to manage the stress generated by all the bugs and feedback.<\/li>\n<\/ul>\n<p>If your team try to refactor regularly their C++ projects and they chose to use some new C++11\/C++14 features, it&#8217;s preferable to know how the other known C++ projects refactored their code to use the new standards.<\/p>\n<p>The Chrome browser is a good project to check, they refactor regularly their code base and as soon as the new standards were approved and supported by the compilers, the Chrome development team chose to move forward to this new standards.<\/p>\n<p>The Chrome browser is a mature C++ project and it&#8217;s very interesting to explore how the code is implemented and designed. Indeed, the chrome browser is used by millions of users which will impose to their development team to have an efficient code.<\/p>\n<p>If you decide to refactor your C++ code base in order to use the new standards feature, it&#8217;s recommended to take a look to this interesting <a href=\"https:\/\/chromium-cpp.appspot.com\/\">google\u00a0document<\/a> which enumerates the allowed C++11\/C++14 features and also the banned ones.<\/p>\n<p>What&#8217;s more relevant in this document is the banned features and the reason why there are banned, it&#8217;s highly recommended to take a look at this list and check if it&#8217;s also applicable to your projects.<\/p>\n<p>Here are from the document the banned C++11\/C++14\u00a0 features:<\/p>\n<h3 id=\"blacklist_banned\">C++11 Banned Features<\/h3>\n<table id=\"banned_list\" class=\"unlined striped\">\n<tbody>\n<tr>\n<th>Feature or Library<\/th>\n<th>Snippet<\/th>\n<th>Description<\/th>\n<th>Documentation Link<\/th>\n<th>Notes and Discussion Thread<\/th>\n<\/tr>\n<tr>\n<td>Inline Namespaces<\/td>\n<td><code>inline namespace foo { ... }<\/code><\/td>\n<td>Allows better versioning of namespaces<\/td>\n<td><a href=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/namespace#Inline_namespaces\">Inline namespaces<\/a><\/td>\n<td>Banned in the\u00a0<a href=\"https:\/\/google.github.io\/styleguide\/cppguide.html#Namespaces\">Google Style Guide<\/a>. Unclear how it will work with components.<\/td>\n<\/tr>\n<tr>\n<td><code>long long<\/code>\u00a0Type<\/td>\n<td><code>long long\u00a0<i>var<\/i>\u00a0=\u00a0<i>value<\/i>;<\/code><\/td>\n<td>An integer of at least 64 bits<\/td>\n<td><a href=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/types\">Fundamental types<\/a><\/td>\n<td>Use a stdint.h type if you need a 64bit number.\u00a0<a href=\"https:\/\/groups.google.com\/a\/chromium.org\/forum\/#!topic\/chromium-dev\/RxugZ-pIDxk\">Discussion thread<\/a><\/td>\n<\/tr>\n<tr>\n<td>Ref-qualified Member Functions<\/td>\n<td><code>class T {<br \/>\nvoid f() &amp; {}<br \/>\nvoid f() &amp;&amp; {}<br \/>\n};<br \/>\nt.f();\u00a0\u00a0\/\/ first<br \/>\nT().f();\u00a0\u00a0\/\/ second<br \/>\nstd::move(t).f();\u00a0\u00a0\/\/ second<\/code><\/td>\n<td>Allows class member functions to only bind to |this| as an rvalue or lvalue.<\/td>\n<td><a href=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/member_functions#const-.2C_volatile-.2C_and_ref-qualified_member_functions\">const-, volatile-, and ref-qualified member functions<\/a><\/td>\n<td>Banned in the\u00a0<a href=\"https:\/\/google.github.io\/styleguide\/cppguide.html#C++11\">Google Style Guide<\/a>. May only be used in Chromium with explicit approval from\u00a0<code>styleguide\/c++\/OWNERS<\/code>.\u00a0<a href=\"https:\/\/groups.google.com\/a\/chromium.org\/forum\/#!topic\/cxx\/gowclr2LPQA\">Discussion Thread<\/a><\/td>\n<\/tr>\n<tr>\n<td>User-Defined Literals<\/td>\n<td><code><i>type<\/i>\u00a0<i>var<\/i>\u00a0=\u00a0<i>literal_value<\/i>_<i>type<\/i><\/code><\/td>\n<td>Allows user-defined literal expressions<\/td>\n<td><a href=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/user_literal\">User-defined literals<\/a><\/td>\n<td>Banned in the\u00a0<a href=\"https:\/\/google.github.io\/styleguide\/cppguide.html#Operator_Overloading\">Google Style Guide<\/a>.<\/td>\n<\/tr>\n<tr>\n<td>thread_local storage class<\/td>\n<td><code>thread_local int foo = 1;<\/code><\/td>\n<td>Puts variables into thread local storage.<\/td>\n<td><a href=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/storage_duration\">Storage duration<\/a><\/td>\n<td>Some surprising effects on Mac (<a href=\"https:\/\/groups.google.com\/a\/chromium.org\/forum\/#!topic\/chromium-dev\/2msN8k3Xzgs\">discussion<\/a>,\u00a0<a href=\"https:\/\/groups.google.com\/a\/chromium.org\/forum\/#!topic\/cxx\/h7O5BdtWCZw\">fork<\/a>). Use\u00a0<a href=\"https:\/\/cs.chromium.org\/chromium\/src\/base\/threading\/sequence_local_storage_slot.h\">SequenceLocalStorageSlot<\/a>\u00a0for sequence support, and\u00a0<a href=\"https:\/\/cs.chromium.org\/chromium\/src\/base\/threading\/thread_local.h\">ThreadLocal<\/a>\/<a href=\"https:\/\/cs.chromium.org\/chromium\/src\/base\/threading\/thread_local_storage.h\">ThreadLocalStorage<\/a>otherwise.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3 id=\"blacklist_banned\">C++14 Banned Features<\/h3>\n<table id=\"banned_list\" class=\"unlined striped\">\n<tbody>\n<tr>\n<th>Feature<\/th>\n<th>Snippet<\/th>\n<th>Description<\/th>\n<th>Documentation Link<\/th>\n<th>Notes and Discussion Thread<\/th>\n<\/tr>\n<tr>\n<td>Function return type deduction<\/td>\n<td><code>auto f() { return 42; }<br \/>\ndecltype(auto) g() { return 42; }<\/code><\/td>\n<td>Allows the return type of a function to be automatically deduced from its return statements, according to either template or\u00a0<code>decltype<\/code>\u00a0rules.<\/td>\n<td><a href=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/function#Return_type_deduction\">Return type deduction<\/a><\/td>\n<td>Temporarily banned since it\u00a0<a href=\"https:\/\/bugs.llvm.org\/show_bug.cgi?id=34185\">can cause infinite loops in clang<\/a>. We expect to allow this once that bug is fixed. Usage should be rare, primarily for abstract template code.\u00a0<a href=\"https:\/\/groups.google.com\/a\/chromium.org\/d\/topic\/cxx\/-Ox7YgRS_no\/discussion\">Discussion thread<\/a><\/td>\n<\/tr>\n<tr>\n<td>Generic lambdas<\/td>\n<td><code>[](const auto&amp; x) {\u00a0<i>...<\/i>\u00a0}<\/code><\/td>\n<td>Allows lambda argument types to be deduced using\u00a0<code>auto<\/code>\u00a0(according to the rules that apply to templates).<\/td>\n<td><a href=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/lambda\">lambda expressions<\/a><\/td>\n<td>Temporarily banned since it\u00a0<a href=\"https:\/\/bugs.llvm.org\/show_bug.cgi?id=33561\">can cause infinite loops in clang<\/a>. We expect to allow this once that bug is fixed.\u00a0<a href=\"https:\/\/chromium-review.googlesource.com\/#\/c\/chromium\/src\/+\/855776\">Discussion thread<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3 id=\"blacklist_stdlib\">C++11 Banned Library Features<\/h3>\n<table id=\"blacklist_lib_list\" class=\"unlined striped\">\n<tbody>\n<tr>\n<th>Feature<\/th>\n<th>Snippet<\/th>\n<th>Description<\/th>\n<th>Documentation Link<\/th>\n<th>Notes and Discussion Thread<\/th>\n<\/tr>\n<tr>\n<td>Aligned storage<\/td>\n<td><code>std::aligned_storage&lt;10, 128&gt;<\/code><\/td>\n<td>Uninitialized storage for objects requiring specific alignment.<\/td>\n<td><a href=\"http:\/\/en.cppreference.com\/w\/cpp\/types\/aligned_storage\">std::aligned_storage<\/a><\/td>\n<td>MSVC 2017&#8217;s implementation does not align on boundaries greater than sizeof(double) = 8 bytes. Use\u00a0<code>alignas(128) char foo[10];<\/code>\u00a0instead.\u00a0<a href=\"https:\/\/codereview.chromium.org\/2932053002\">Patch where this was discovered<\/a>.<\/td>\n<\/tr>\n<tr>\n<td>Bind Operations<\/td>\n<td><code>std::bind(<i>function<\/i>,\u00a0<i>args<\/i>, ...)<\/code><\/td>\n<td>Declares a function object bound to certain arguments<\/td>\n<td><a href=\"http:\/\/en.cppreference.com\/w\/cpp\/utility\/functional\/bind\">std::bind<\/a><\/td>\n<td>Use\u00a0<code>base::Bind<\/code>\u00a0instead. Compared to\u00a0<code>std::bind<\/code>,\u00a0<code>base::Bind<\/code>\u00a0helps prevent lifetime issues by preventing binding of capturing lambdas and by forcing callers to declare raw pointers as\u00a0<code>Unretained<\/code>.\u00a0<a href=\"https:\/\/groups.google.com\/a\/chromium.org\/forum\/#!topic\/cxx\/SoEj7oIDNuA\">Discussion thread<\/a><\/td>\n<\/tr>\n<tr>\n<td>C Floating-Point Environment<\/td>\n<td><code>&lt;cfenv&gt;<\/code>,\u00a0<code>&lt;fenv.h&gt;<\/code><\/td>\n<td>Provides floating point status flags and control modes for C-compatible code<\/td>\n<td><a href=\"http:\/\/en.cppreference.com\/w\/cpp\/header\/cfenv\">Standard library header &lt;cfenv&gt;<\/a><\/td>\n<td>Banned by the\u00a0<a href=\"https:\/\/google.github.io\/styleguide\/cppguide.html#C++11\">Google Style Guide<\/a>\u00a0due to concerns about compiler support.<\/td>\n<\/tr>\n<tr>\n<td>Date and time utilities<\/td>\n<td><code>&lt;chrono&gt;<\/code><\/td>\n<td>A standard date and time library<\/td>\n<td><a href=\"http:\/\/en.cppreference.com\/w\/cpp\/chrono\">Date and time utilities<\/a><\/td>\n<td>Overlaps with\u00a0<code>Time<\/code>\u00a0APIs in\u00a0<code>base\/<\/code>. Keep using the\u00a0<code>base\/<\/code>\u00a0classes.<\/td>\n<\/tr>\n<tr>\n<td>Exceptions<\/td>\n<td><code>&lt;exception&gt;<\/code><\/td>\n<td>Enhancements to exception throwing and handling<\/td>\n<td><a href=\"http:\/\/en.cppreference.com\/w\/cpp\/header\/exception\">Standard library header &lt;exception&gt;<\/a><\/td>\n<td>Exceptions are banned by the\u00a0<a href=\"https:\/\/google.github.io\/styleguide\/cppguide.html#Exceptions\">Google Style Guide<\/a>\u00a0and disabled in Chromium compiles. Note that the\u00a0<code>noexcept<\/code>\u00a0specifier is explicitly allowed above.\u00a0<a href=\"https:\/\/groups.google.com\/a\/chromium.org\/forum\/#!topic\/chromium-dev\/8i4tMqNpHhg\">Discussion thread<\/a><\/td>\n<\/tr>\n<tr>\n<td>Function Objects<\/td>\n<td><code>std::function<\/code><\/td>\n<td>Wraps a standard polymorphic function<\/td>\n<td><a href=\"http:\/\/en.cppreference.com\/w\/cpp\/utility\/functional\/function\">std::function<\/a><\/td>\n<td>Use\u00a0<code>base::Callback<\/code>\u00a0instead. Compared to\u00a0<code>std::function<\/code>,\u00a0<code>base::Callback<\/code>\u00a0directly supports Chromium&#8217;s refcounting classes and weak pointers and deals with additional thread safety concerns.\u00a0<a href=\"https:\/\/groups.google.com\/a\/chromium.org\/forum\/#!topic\/cxx\/SoEj7oIDNuA\">Discussion thread<\/a><\/td>\n<\/tr>\n<tr>\n<td>Ratio Template Class<\/td>\n<td><code>std::ratio&lt;<i>numerator<\/i>,\u00a0<i>denominator<\/i>&gt;<\/code><\/td>\n<td>Provides compile-time rational numbers<\/td>\n<td><a href=\"http:\/\/en.cppreference.com\/w\/cpp\/numeric\/ratio\/ratio\">std::ratio<\/a><\/td>\n<td>Banned by the\u00a0<a href=\"https:\/\/google.github.io\/styleguide\/cppguide.html#C++11\">Google Style Guide<\/a>\u00a0due to concerns that this is tied to a more template-heavy interface style.<\/td>\n<\/tr>\n<tr>\n<td>Regular Expressions<\/td>\n<td><code>&lt;regex&gt;<\/code><\/td>\n<td>A standard regular expressions library<\/td>\n<td><a href=\"http:\/\/en.cppreference.com\/w\/cpp\/regex\">Regular expressions library<\/a><\/td>\n<td>Overlaps with many regular expression libraries in Chromium. When in doubt, use re2.<\/td>\n<\/tr>\n<tr>\n<td>Shared Pointers<\/td>\n<td><code>std::shared_ptr<\/code><\/td>\n<td>Allows shared ownership of a pointer through reference counts<\/td>\n<td><a href=\"http:\/\/en.cppreference.com\/w\/cpp\/memory\/shared_ptr\">std::shared_ptr<\/a><\/td>\n<td>Needs a lot more evaluation for Chromium, and there isn&#8217;t enough of a push for this feature.\u00a0<a href=\"https:\/\/google.github.io\/styleguide\/cppguide.html#Ownership_and_Smart_Pointers\">Google Style Guide<\/a>.\u00a0<a href=\"https:\/\/groups.google.com\/a\/chromium.org\/forum\/#!topic\/cxx\/aT2wsBLKvzI\">Discussion Thread<\/a><\/td>\n<\/tr>\n<tr>\n<td>Thread Library<\/td>\n<td><code>&lt;thread&gt;<\/code>\u00a0and related headers, including<br \/>\n<code>&lt;future&gt;<\/code>,\u00a0<code>&lt;mutex&gt;<\/code>,\u00a0<code>&lt;condition_variable&gt;<\/code><\/td>\n<td>Provides a standard multithreading library using\u00a0<code>std::thread<\/code>\u00a0and associates<\/td>\n<td><a href=\"http:\/\/en.cppreference.com\/w\/cpp\/thread\">Thread support library<\/a><\/td>\n<td>Overlaps with many classes in\u00a0<code>base\/<\/code>. Keep using the\u00a0<code>base\/<\/code>\u00a0classes for now.\u00a0<code>base::Thread<\/code>\u00a0is tightly coupled to\u00a0<code>MessageLoop<\/code>\u00a0which would make it hard to replace. We should investigate using standard mutexes, or unique_lock, etc. to replace our locking\/synchronization classes.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3 id=\"blacklist_stdlib\">C++14 Banned Library Features<\/h3>\n<p>This section lists C++14 library features that are not allowed in the Chromium codebase.<\/p>\n<table id=\"blacklist_lib_list\" class=\"unlined striped\">\n<tbody>\n<tr>\n<th>Feature<\/th>\n<th>Snippet<\/th>\n<th>Description<\/th>\n<th>Documentation Link<\/th>\n<th>Notes and Discussion Thread<\/th>\n<\/tr>\n<tr>\n<td><code>std::chrono<\/code>\u00a0literals<\/td>\n<td><code>using namespace std::chrono_literals;<br \/>\nauto timeout = 30s;<\/code><\/td>\n<td>Allows\u00a0<code>std::chrono<\/code>\u00a0types to be more easily constructed.<\/td>\n<td><a href=\"http:\/\/en.cppreference.com\/w\/cpp\/chrono\/operator%22%22s\">std::literals::chrono_literals::operator&#8221;&#8221;s<\/a><\/td>\n<td>Banned because\u00a0<code>&lt;chrono&gt;<\/code>\u00a0is banned.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Conclusion:<\/strong><\/p>\n<p>Exploring the source code of some known open source projects to discover their design and implementation choices is maybe the good way to write an efficient code. Indeed, the know C++ projects are in general developed by C++ experts who try to apply as they can the best practices.<\/p>\n<p>To resume take a look at the Chromium source code and be sure it will not be a waste of time.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The refactoring is defined as the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure. We can enumerate\u00a0three\u00a0decisions made by the managers concerning the code refactoring:<\/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,35,36,50],"class_list":["post-598","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c11","tag-c14","tag-c17","tag-modernizer"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/598","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=598"}],"version-history":[{"count":14,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/598\/revisions"}],"predecessor-version":[{"id":1487,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/598\/revisions\/1487"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}