Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1 | # Modern C++ use in Chromium |
| 2 | |
| 3 | _This document is part of the more general |
| 4 | [Chromium C++ style guide](https://chromium.googlesource.com/chromium/src/+/main/styleguide/c++/c++.md). |
| 5 | It summarizes the supported state of new and updated language and library |
| 6 | features in recent C++ standards and the [Abseil](https://abseil.io/about/) |
| 7 | library. This guide applies to both Chromium and its subprojects, though |
| 8 | subprojects can choose to be more restrictive if necessary for toolchain |
| 9 | support._ |
| 10 | |
| 11 | The C++ language has in recent years received an updated standard every three |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 12 | years (C++11, C++14, etc.). For various reasons, Chromium does not immediately |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 13 | allow new features on the publication of such a standard. Instead, once |
Hong Xu | 68ba51a0 | 2022-04-27 22:21:35 | [diff] [blame] | 14 | Chromium supports the toolchain to a certain extent (e.g., build support is |
| 15 | ready), a standard is declared "_initially supported_", with new |
| 16 | language/library features banned pending discussion but not yet allowed. |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 17 | |
| 18 | You can propose changing the status of a feature by sending an email to |
| 19 | [[email protected]](https://groups.google.com/a/chromium.org/forum/#!forum/cxx). |
| 20 | Include a short blurb on what the feature is and why you think it should or |
| 21 | should not be allowed, along with links to any relevant previous discussion. If |
| 22 | the list arrives at some consensus, send a codereview to change this file |
| 23 | accordingly, linking to your discussion thread. |
| 24 | |
| 25 | If an item remains on the TBD list two years after initial support is added, |
| 26 | style arbiters should explicitly move it to an appropriate allowlist or |
| 27 | blocklist, allowing it if there are no obvious reasons to ban. |
| 28 | |
| 29 | The current status of existing standards and Abseil features is: |
| 30 | |
| 31 | * **C++11:** _Default allowed; see banned features below_ |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 32 | * **C++14:** _Default allowed_ |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 33 | * **C++17:** _Default allowed; see banned features below_ |
Peter Kasting | f86817c | 2023-11-13 18:00:11 | [diff] [blame] | 34 | * **C++20:** _Initially supported November 13, 2023; see allowed/banned/TBD |
| 35 | features below_ |
| 36 | * **C++23:** _Not yet officially standardized_ |
Danil Chapovalov | 0e8ed13 | 2023-09-28 08:05:39 | [diff] [blame] | 37 | * **Abseil:** _Default allowed; see banned/TBD features below. The following |
| 38 | dates represent the start of the two-year TBD periods for certain parts of |
| 39 | Abseil:_ |
| 40 | * absl::AnyInvocable: Initially added to third_party June 20, 2022 |
| 41 | * Log library: Initially added to third_party Aug 31, 2022 |
| 42 | * CRC32C library: Initially added to third_party Dec 5, 2022 |
| 43 | * Nullability annotation: Initially added to third_party Jun 21, 2023 |
| 44 | * Overload: Initially added to third_party Sep 27, 2023 |
Mirko Bonadei | dc928a16 | 2023-11-20 08:24:23 | [diff] [blame] | 45 | * NoDestructor: Initially added to third_party Nov 15, 2023 |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 46 | |
Joe Mason | 6a6f258 | 2024-01-30 20:26:43 | [diff] [blame] | 47 | ## Banned features and third-party code |
| 48 | |
| 49 | Third-party libraries may generally use banned features internally, although features |
| 50 | with poor compiler support or poor security properties may make the library |
| 51 | unsuitable to use with Chromium. |
| 52 | |
| 53 | Chromium code that calls functions exported from a third-party library may use |
| 54 | banned library types that are required by the interface, as long as: |
| 55 | |
| 56 | * The disallowed type is used only at the interface, and converted to and from |
| 57 | an equivalent allowed type as soon as practical on the Chromium side. |
| 58 | * The feature is not banned due to security issues or lack of compiler support. |
| 59 | If it is, discuss with |
| 60 | [[email protected]](https://groups.google.com/a/chromium.org/forum/#!forum/cxx) |
| 61 | to find a workaround. |
| 62 | |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 63 | [TOC] |
| 64 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 65 | ## C++11 Banned Language Features {#core-blocklist-11} |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 66 | |
| 67 | The following C++11 language features are not allowed in the Chromium codebase. |
| 68 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 69 | ### Inline Namespaces <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 70 | |
| 71 | ```c++ |
| 72 | inline namespace foo { ... } |
| 73 | ``` |
| 74 | |
| 75 | **Description:** Allows better versioning of namespaces. |
| 76 | |
| 77 | **Documentation:** |
| 78 | [Inline namespaces](https://en.cppreference.com/w/cpp/language/namespace#Inline_namespaces) |
| 79 | |
| 80 | **Notes:** |
| 81 | *** promo |
| 82 | Banned in the |
| 83 | [Google Style Guide](https://google.github.io/styleguide/cppguide.html#Namespaces). |
| 84 | Unclear how it will work with components. |
| 85 | *** |
| 86 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 87 | ### long long Type <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 88 | |
| 89 | ```c++ |
| 90 | long long var = value; |
| 91 | ``` |
| 92 | |
| 93 | **Description:** An integer of at least 64 bits. |
| 94 | |
| 95 | **Documentation:** |
| 96 | [Fundamental types](https://en.cppreference.com/w/cpp/language/types) |
| 97 | |
| 98 | **Notes:** |
| 99 | *** promo |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 100 | Use a `<stdint.h>` type if you need a 64-bit number. |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 101 | |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 102 | [Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/chromium-dev/RxugZ-pIDxk) |
| 103 | *** |
| 104 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 105 | ### User-Defined Literals <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 106 | |
| 107 | ```c++ |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 108 | DistanceType var = 12_km; |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 109 | ``` |
| 110 | |
| 111 | **Description:** Allows user-defined literal expressions. |
| 112 | |
| 113 | **Documentation:** |
| 114 | [User-defined literals](https://en.cppreference.com/w/cpp/language/user_literal) |
| 115 | |
| 116 | **Notes:** |
| 117 | *** promo |
| 118 | Banned in the |
| 119 | [Google Style Guide](https://google.github.io/styleguide/cppguide.html#Operator_Overloading). |
| 120 | *** |
| 121 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 122 | ## C++11 Banned Library Features {#library-blocklist-11} |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 123 | |
| 124 | The following C++11 library features are not allowed in the Chromium codebase. |
| 125 | |
Peter Kasting | 6f79b20 | 2023-08-09 21:25:41 | [diff] [blame] | 126 | ### <cctype>, <ctype.h>, <cwctype>, <wctype.h> <sup>[banned]</sup> |
| 127 | |
| 128 | ```c++ |
| 129 | #include <cctype> |
| 130 | #include <cwctype> |
| 131 | #include <ctype.h> |
| 132 | #include <wctype.h> |
| 133 | ``` |
| 134 | |
| 135 | **Description:** Provides utilities for ASCII characters. |
| 136 | |
| 137 | **Documentation:** |
| 138 | [Standard library header `<cctype>`](https://en.cppreference.com/w/cpp/header/cctype), |
| 139 | [Standard library header `<cwctype>`](https://en.cppreference.com/w/cpp/header/cwctype) |
| 140 | |
| 141 | **Notes:** |
| 142 | *** promo |
| 143 | Banned due to dependence on the C locale as well as UB when arguments don't fit |
| 144 | in an `unsigned char`/`wchar_t`. Use similarly-named replacements in |
| 145 | [third_party/abseil-cpp/absl/strings/ascii.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/strings/ascii.h) |
| 146 | instead. |
| 147 | *** |
| 148 | |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 149 | ### <cfenv>, <fenv.h> <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 150 | |
| 151 | ```c++ |
| 152 | #include <cfenv> |
| 153 | #include <fenv.h> |
| 154 | ``` |
| 155 | |
| 156 | **Description:** Provides floating point status flags and control modes for |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 157 | C-compatible code. |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 158 | |
| 159 | **Documentation:** |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 160 | [Standard library header `<cfenv>`](https://en.cppreference.com/w/cpp/header/cfenv) |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 161 | |
| 162 | **Notes:** |
| 163 | *** promo |
| 164 | Banned by the |
| 165 | [Google Style Guide](https://google.github.io/styleguide/cppguide.html#C++11) |
| 166 | due to concerns about compiler support. |
| 167 | *** |
| 168 | |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 169 | ### <chrono> <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 170 | |
| 171 | ```c++ |
| 172 | #include <chrono> |
| 173 | ``` |
| 174 | |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 175 | **Description:** A standard date and time library. |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 176 | |
| 177 | **Documentation:** |
| 178 | [Date and time utilities](https://en.cppreference.com/w/cpp/chrono) |
| 179 | |
| 180 | **Notes:** |
| 181 | *** promo |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 182 | Overlaps with `base/time`. |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 183 | *** |
| 184 | |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 185 | ### <exception> <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 186 | |
| 187 | ```c++ |
| 188 | #include <exception> |
| 189 | ``` |
| 190 | |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 191 | **Description:** Exception throwing and handling. |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 192 | |
| 193 | **Documentation:** |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 194 | [Standard library header `<exception>`](https://en.cppreference.com/w/cpp/header/exception) |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 195 | |
| 196 | **Notes:** |
| 197 | *** promo |
| 198 | Exceptions are banned by the |
| 199 | [Google Style Guide](https://google.github.io/styleguide/cppguide.html#Exceptions) |
| 200 | and disabled in Chromium compiles. However, the `noexcept` specifier is |
| 201 | explicitly allowed. |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 202 | |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 203 | [Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/chromium-dev/8i4tMqNpHhg) |
| 204 | *** |
| 205 | |
Peter Kasting | c7460d98 | 2023-03-14 21:01:42 | [diff] [blame] | 206 | ### Engines And Generators From <random> <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 207 | |
| 208 | ```c++ |
Peter Kasting | c7460d98 | 2023-03-14 21:01:42 | [diff] [blame] | 209 | std::mt19937 generator; |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 210 | ``` |
| 211 | |
Peter Kasting | c7460d98 | 2023-03-14 21:01:42 | [diff] [blame] | 212 | **Description:** Methods of generating random numbers. |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 213 | |
| 214 | **Documentation:** |
| 215 | [Pseudo-random number generation](https://en.cppreference.com/w/cpp/numeric/random) |
| 216 | |
| 217 | **Notes:** |
| 218 | *** promo |
Peter Kasting | c7460d98 | 2023-03-14 21:01:42 | [diff] [blame] | 219 | Do not use any random number engines or generators from `<random>`. Instead, use |
| 220 | `base::RandomBitGenerator`. (You may use the distributions from `<random>`.) |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 221 | |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 222 | [Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/16Xmw05C-Y0) |
| 223 | *** |
| 224 | |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 225 | ### <ratio> <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 226 | |
| 227 | ```c++ |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 228 | #include <ratio> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 229 | ``` |
| 230 | |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 231 | **Description:** Provides compile-time rational numbers. |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 232 | |
| 233 | **Documentation:** |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 234 | [`std::ratio`](https://en.cppreference.com/w/cpp/numeric/ratio/ratio) |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 235 | |
| 236 | **Notes:** |
| 237 | *** promo |
| 238 | Banned by the |
| 239 | [Google Style Guide](https://google.github.io/styleguide/cppguide.html#C++11) |
| 240 | due to concerns that this is tied to a more template-heavy interface style. |
| 241 | *** |
| 242 | |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 243 | ### <regex> <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 244 | |
| 245 | ```c++ |
| 246 | #include <regex> |
| 247 | ``` |
| 248 | |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 249 | **Description:** A standard regular expressions library. |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 250 | |
| 251 | **Documentation:** |
| 252 | [Regular expressions library](https://en.cppreference.com/w/cpp/regex) |
| 253 | |
| 254 | **Notes:** |
| 255 | *** promo |
| 256 | Overlaps with many regular expression libraries in Chromium. When in doubt, use |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 257 | `third_party/re2`. |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 258 | *** |
| 259 | |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 260 | ### std::bind <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 261 | |
| 262 | ```c++ |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 263 | auto x = std::bind(function, args, ...); |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 264 | ``` |
| 265 | |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 266 | **Description:** Declares a function object bound to certain arguments. |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 267 | |
| 268 | **Documentation:** |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 269 | [`std::bind`](https://en.cppreference.com/w/cpp/utility/functional/bind) |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 270 | |
| 271 | **Notes:** |
| 272 | *** promo |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 273 | Use `base::Bind` instead. Compared to `std::bind`, `base::Bind` helps prevent |
| 274 | lifetime issues by preventing binding of capturing lambdas and by forcing |
| 275 | callers to declare raw pointers as `Unretained`. |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 276 | |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 277 | [Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/SoEj7oIDNuA) |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 278 | *** |
| 279 | |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 280 | ### std::function <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 281 | |
| 282 | ```c++ |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 283 | std::function x = [] { return 10; }; |
| 284 | std::function y = std::bind(foo, args); |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 285 | ``` |
| 286 | |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 287 | **Description:** Wraps a standard polymorphic function. |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 288 | |
| 289 | **Documentation:** |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 290 | [`std::function`](https://en.cppreference.com/w/cpp/utility/functional/function) |
| 291 | |
| 292 | **Notes:** |
| 293 | *** promo |
Marijn Kruisselbrink | e453e56c | 2023-12-05 22:35:13 | [diff] [blame] | 294 | Use `base::{Once,Repeating}Callback` or `base::FunctionRef` instead. Compared |
| 295 | to `std::function`, `base::{Once,Repeating}Callback` directly supports |
| 296 | Chromium's refcounting classes and weak pointers and deals with additional |
| 297 | thread safety concerns. |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 298 | |
| 299 | [Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/SoEj7oIDNuA) |
| 300 | *** |
| 301 | |
| 302 | ### std::shared_ptr <sup>[banned]</sup> |
| 303 | |
| 304 | ```c++ |
| 305 | std::shared_ptr<int> x = std::make_shared<int>(10); |
| 306 | ``` |
| 307 | |
| 308 | **Description:** Allows shared ownership of a pointer through reference counts. |
| 309 | |
| 310 | **Documentation:** |
| 311 | [`std::shared_ptr`](https://en.cppreference.com/w/cpp/memory/shared_ptr) |
| 312 | |
| 313 | **Notes:** |
| 314 | *** promo |
| 315 | Unlike `base::RefCounted`, uses extrinsic rather than intrinsic reference |
| 316 | counting. Could plausibly be used in Chromium, but would require significant |
| 317 | migration. |
| 318 | |
| 319 | [Google Style Guide](https://google.github.io/styleguide/cppguide.html#Ownership_and_Smart_Pointers), |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 320 | [Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/aT2wsBLKvzI) |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 321 | *** |
| 322 | |
| 323 | ### std::{sto{i,l,ul,ll,ull,f,d,ld},to_string} <sup>[banned]</sup> |
| 324 | |
| 325 | ```c++ |
| 326 | int x = std::stoi("10"); |
| 327 | ``` |
| 328 | |
| 329 | **Description:** Converts strings to/from numbers. |
| 330 | |
| 331 | **Documentation:** |
| 332 | [`std::stoi`, `std::stol`, `std::stoll`](https://en.cppreference.com/w/cpp/string/basic_string/stol), |
| 333 | [`std::stoul`, `std::stoull`](https://en.cppreference.com/w/cpp/string/basic_string/stoul), |
| 334 | [`std::stof`, `std::stod`, `std::stold`](https://en.cppreference.com/w/cpp/string/basic_string/stof), |
| 335 | [`std::to_string`](https://en.cppreference.com/w/cpp/string/basic_string/to_string) |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 336 | |
| 337 | **Notes:** |
| 338 | *** promo |
| 339 | The string-to-number conversions rely on exceptions to communicate failure, |
| 340 | while the number-to-string conversions have performance concerns and depend on |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 341 | the locale. Use `base/strings/string_number_conversions.h` instead. |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 342 | *** |
| 343 | |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 344 | ### std::weak_ptr <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 345 | |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 346 | ```c++ |
| 347 | std::weak_ptr<int> x = my_shared_x; |
| 348 | ``` |
| 349 | |
| 350 | **Description:** Allows a weak reference to a `std::shared_ptr`. |
| 351 | |
| 352 | **Documentation:** |
| 353 | [`std::weak_ptr`](https://en.cppreference.com/w/cpp/memory/weak_ptr) |
| 354 | |
| 355 | **Notes:** |
| 356 | *** promo |
| 357 | Banned because `std::shared_ptr` is banned. Use `base::WeakPtr` instead. |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 358 | *** |
| 359 | |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 360 | ### Thread Support Library <sup>[banned]</sup> |
| 361 | |
| 362 | ```c++ |
| 363 | #include <barrier> // C++20 |
| 364 | #include <condition_variable> |
| 365 | #include <future> |
| 366 | #include <latch> // C++20 |
| 367 | #include <mutex> |
| 368 | #include <semaphore> // C++20 |
| 369 | #include <stop_token> // C++20 |
| 370 | #include <thread> |
| 371 | ``` |
| 372 | |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 373 | **Description:** Provides a standard multithreading library using `std::thread` |
| 374 | and associates |
| 375 | |
| 376 | **Documentation:** |
| 377 | [Thread support library](https://en.cppreference.com/w/cpp/thread) |
| 378 | |
| 379 | **Notes:** |
| 380 | *** promo |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 381 | Overlaps with `base/synchronization`. `base::Thread` is tightly coupled to |
| 382 | `base::MessageLoop` which would make it hard to replace. We should investigate |
| 383 | using standard mutexes, or `std::unique_lock`, etc. to replace our |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 384 | locking/synchronization classes. |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 385 | *** |
| 386 | |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 387 | ## C++17 Banned Language Features {#core-blocklist-17} |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 388 | |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 389 | The following C++17 language features are not allowed in the Chromium codebase. |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 390 | |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 391 | ### UTF-8 character literals <sup>[banned]</sup> |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 392 | |
| 393 | ```c++ |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 394 | char x = u8'x'; // C++17 |
| 395 | char8_t x = u8'x'; // C++20 |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 396 | ``` |
| 397 | |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 398 | **Description:** A character literal that begins with `u8` is a character |
| 399 | literal of type `char` (C++17) or `char8_t` (C++20). The value of a UTF-8 |
| 400 | character literal is equal to its ISO 10646 code point value. |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 401 | |
| 402 | **Documentation:** |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 403 | [Character literal](https://en.cppreference.com/w/cpp/language/character_literal) |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 404 | |
| 405 | **Notes:** |
| 406 | *** promo |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 407 | Banned because `char8_t` is banned. Use an unprefixed character or string |
| 408 | literal; it should be encoded in the binary as UTF-8 on all supported platforms. |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 409 | *** |
| 410 | |
| 411 | ## C++17 Banned Library Features {#library-blocklist-17} |
| 412 | |
| 413 | The following C++17 library features are not allowed in the Chromium codebase. |
| 414 | |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 415 | ### Mathematical special functions <sup>[banned]</sup> |
| 416 | |
| 417 | ```c++ |
| 418 | std::assoc_laguerre() |
| 419 | std::assoc_legendre() |
| 420 | std::beta() |
| 421 | std::comp_ellint_1() |
| 422 | std::comp_ellint_2() |
| 423 | std::comp_ellint_3() |
| 424 | std::cyl_bessel_i() |
| 425 | std::cyl_bessel_j() |
| 426 | std::cyl_bessel_k() |
| 427 | std::cyl_neumann() |
| 428 | std::ellint_1() |
| 429 | std::ellint_2() |
| 430 | std::ellint_3() |
| 431 | std::expint() |
| 432 | std::hermite() |
| 433 | std::legendre() |
| 434 | std::laguerre() |
| 435 | std::riemann_zeta() |
| 436 | std::sph_bessel() |
| 437 | std::sph_legendre() |
| 438 | std::sph_neumann() |
| 439 | ``` |
| 440 | |
| 441 | **Description:** A variety of mathematical functions. |
| 442 | |
| 443 | **Documentation:** |
| 444 | [Mathematical special functions](https://en.cppreference.com/w/cpp/numeric/special_functions) |
| 445 | |
| 446 | **Notes:** |
| 447 | *** promo |
| 448 | Banned due to |
| 449 | [lack of libc++ support](https://libcxx.llvm.org/Status/Cxx17.html). |
| 450 | *** |
| 451 | |
| 452 | ### Parallel algorithms <sup>[banned]</sup> |
| 453 | |
| 454 | ```c++ |
| 455 | auto it = std::find(std::execution::par, std::begin(vec), std::end(vec), 2); |
| 456 | ``` |
| 457 | |
| 458 | **Description:** Many of the STL algorithms, such as the `copy`, `find` and |
| 459 | `sort` methods, now support the parallel execution policies: `seq`, `par`, and |
| 460 | `par_unseq` which translate to "sequentially", "parallel" and |
| 461 | "parallel unsequenced". |
| 462 | |
| 463 | **Documentation:** |
| 464 | [`std::execution::sequenced_policy`, `std::execution::parallel_policy`, `std::execution::parallel_unsequenced_policy`, `std::execution::unsequenced_policy`](https://en.cppreference.com/w/cpp/algorithm/execution_policy_tag_t) |
| 465 | |
| 466 | **Notes:** |
| 467 | *** promo |
| 468 | Banned because |
| 469 | [libc++ support is incomplete](https://libcxx.llvm.org/Status/PSTL.html) and the |
| 470 | interaction of its threading implementation with Chrome's is unclear. Prefer to |
| 471 | explicitly parallelize long-running algorithms using Chrome's threading APIs, so |
| 472 | the same scheduler controls, shutdown policies, tracing, etc. apply as in any |
| 473 | other multithreaded code. |
| 474 | *** |
| 475 | |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 476 | ### std::aligned_alloc <sup>[banned]</sup> |
| 477 | |
| 478 | ```c++ |
| 479 | int* p2 = static_cast<int*>(std::aligned_alloc(1024, 1024)); |
| 480 | ``` |
| 481 | |
| 482 | **Description:** Allocates uninitialized storage with the specified alignment. |
| 483 | |
| 484 | **Documentation:** |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 485 | [`std::aligned_alloc`](https://en.cppreference.com/w/cpp/memory/c/aligned_alloc) |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 486 | |
| 487 | **Notes:** |
| 488 | *** promo |
| 489 | [Will be allowed soon](https://crbug.com/1412818); for now, use |
| 490 | `base::AlignedAlloc`. |
| 491 | *** |
| 492 | |
| 493 | ### std::any <sup>[banned]</sup> |
| 494 | |
| 495 | ```c++ |
| 496 | std::any x = 5; |
| 497 | ``` |
| 498 | |
| 499 | **Description:** A type-safe container for single values of any type. |
| 500 | |
| 501 | **Documentation:** |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 502 | [`std::any`](https://en.cppreference.com/w/cpp/utility/any) |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 503 | |
| 504 | **Notes:** |
| 505 | *** promo |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 506 | Banned since workaround for lack of RTTI |
| 507 | [isn't compatible with the component build](https://crbug.com/1096380). See also |
| 508 | `absl::any`. |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 509 | |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 510 | [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/00cpZ07nye4) |
| 511 | *** |
| 512 | |
| 513 | ### std::byte <sup>[banned]</sup> |
| 514 | |
| 515 | ```c++ |
| 516 | std::byte b = 0xFF; |
| 517 | int i = std::to_integer<int>(b); // 0xFF |
| 518 | ``` |
| 519 | |
| 520 | **Description:** The contents of a single memory unit. `std::byte` has the same |
| 521 | size and aliasing rules as `unsigned char`, but does not semantically represent |
| 522 | a character or arithmetic value, and does not expose operators other than |
| 523 | bitwise ops. |
| 524 | |
| 525 | **Documentation:** |
| 526 | [`std::byte`](https://en.cppreference.com/w/cpp/types/byte) |
| 527 | |
| 528 | **Notes:** |
| 529 | *** promo |
| 530 | Banned due to low marginal utility in practice, high conversion costs, and |
| 531 | programmer confusion about "byte" vs. "octet". Use `uint8_t` for the common case |
| 532 | of "8-bit unsigned value", and `char` for the atypical case of code that works |
| 533 | with memory without regard to its contents' values or semantics (e.g allocator |
| 534 | implementations). |
| 535 | |
| 536 | [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/bBY0gZa1Otk) |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 537 | *** |
| 538 | |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 539 | ### std::filesystem <sup>[banned]</sup> |
| 540 | |
| 541 | ```c++ |
| 542 | #include <filesystem> |
| 543 | ``` |
| 544 | |
| 545 | **Description:** A standard way to manipulate files, directories, and paths in a |
| 546 | filesystem. |
| 547 | |
| 548 | **Documentation:** |
| 549 | [Filesystem library](https://en.cppreference.com/w/cpp/filesystem) |
| 550 | |
| 551 | **Notes:** |
| 552 | *** promo |
| 553 | Banned by the [Google Style Guide](https://google.github.io/styleguide/cppguide.html#Other_Features). |
| 554 | *** |
| 555 | |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 556 | ### std::{from,to}_chars <sup>[banned]</sup> |
| 557 | |
| 558 | ```c++ |
| 559 | std::from_chars(str.data(), str.data() + str.size(), result); |
| 560 | std::to_chars(str.data(), str.data() + str.size(), 42); |
| 561 | ``` |
| 562 | |
| 563 | **Description:** Locale-independent, non-allocating, non-throwing functions to |
| 564 | convert values from/to character strings, designed for use in high-throughput |
| 565 | contexts. |
| 566 | |
| 567 | **Documentation:** |
| 568 | [`std::from_chars`](https://en.cppreference.com/w/cpp/utility/from_chars) |
| 569 | [`std::to_chars`](https://en.cppreference.com/w/cpp/utility/to_chars), |
| 570 | |
| 571 | **Notes:** |
| 572 | *** promo |
| 573 | Overlaps with utilities in `base/strings/string_number_conversions.h`, which are |
| 574 | easier to use correctly. |
| 575 | *** |
| 576 | |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 577 | ### std::hardware_{con,de}structive_interference_size <sup>[banned]</sup> |
Anton Bikineev | c6a02258 | 2022-10-10 19:08:58 | [diff] [blame] | 578 | |
| 579 | ```c++ |
| 580 | struct SharedData { |
| 581 | ReadOnlyFrequentlyUsed data; |
| 582 | alignas(std::hardware_destructive_interference_size) std::atomic<size_t> counter; |
| 583 | }; |
| 584 | ``` |
| 585 | |
| 586 | **Description:** The `std::hardware_destructive_interference_size` constant is |
| 587 | useful to avoid false sharing (destructive interference) between variables that |
| 588 | would otherwise occupy the same cacheline. In contrast, |
| 589 | `std::hardware_constructive_interference_size` is helpful to promote true |
| 590 | sharing (constructive interference), e.g. to support better locality for |
| 591 | non-contended data. |
| 592 | |
| 593 | **Documentation:** |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 594 | [`std::hardware_destructive_interference_size`](https://en.cppreference.com/w/cpp/thread/hardware_destructive_interference_size), |
| 595 | [`std::hardware_constructive_interference_size`](https://en.cppreference.com/w/cpp/thread/hardware_destructive_interference_size) |
Anton Bikineev | c6a02258 | 2022-10-10 19:08:58 | [diff] [blame] | 596 | |
| 597 | **Notes:** |
| 598 | *** promo |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 599 | Banned for now since these are |
| 600 | [not supported yet](https://github.com/llvm/llvm-project/issues/60174). Allow |
| 601 | once supported. |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 602 | |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 603 | [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/cwktrFxxUY4) |
Avi Drissman | 37b7d51 | 2022-04-01 22:01:40 | [diff] [blame] | 604 | *** |
| 605 | |
David Benjamin | d9d21aa1 | 2023-10-02 23:29:57 | [diff] [blame] | 606 | ### std::in_place{_type,_index}[_t] <sup>[banned]</sup> |
Avi Drissman | bc6545f4 | 2022-05-03 17:47:38 | [diff] [blame] | 607 | |
| 608 | ```c++ |
Avi Drissman | bc6545f4 | 2022-05-03 17:47:38 | [diff] [blame] | 609 | std::variant<int, float> v{std::in_place_type<int>, 1.4}; |
| 610 | ``` |
| 611 | |
David Benjamin | d9d21aa1 | 2023-10-02 23:29:57 | [diff] [blame] | 612 | **Description:** `std::in_place_type` and `std::in_place_index` are |
| 613 | disambiguation tags for `std::variant` and `std::any` to indicate that the |
| 614 | object should be constructed in-place. |
Avi Drissman | bc6545f4 | 2022-05-03 17:47:38 | [diff] [blame] | 615 | |
| 616 | **Documentation:** |
David Benjamin | d9d21aa1 | 2023-10-02 23:29:57 | [diff] [blame] | 617 | [`std::in_place_type`](https://en.cppreference.com/w/cpp/utility/in_place) |
Avi Drissman | bc6545f4 | 2022-05-03 17:47:38 | [diff] [blame] | 618 | |
| 619 | **Notes:** |
| 620 | *** promo |
David Benjamin | d9d21aa1 | 2023-10-02 23:29:57 | [diff] [blame] | 621 | Banned for now because `std::variant` and `std::any` are banned. Because |
| 622 | `absl::variant` is used instead, and it requires `absl::in_place_type`, use |
| 623 | `absl::in_place_type` for non-Abseil Chromium |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 624 | code. |
| 625 | |
| 626 | [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ZspmuJPpv6s) |
| 627 | *** |
| 628 | |
| 629 | ### std::{pmr::memory_resource,polymorphic_allocator} <sup>[banned]</sup> |
| 630 | |
| 631 | ```c++ |
| 632 | #include <memory_resource> |
| 633 | ``` |
| 634 | |
| 635 | **Description:** Manages memory allocations using runtime polymorphism. |
| 636 | |
| 637 | **Documentation:** |
| 638 | [`std::pmr::memory_resource`](https://en.cppreference.com/w/cpp/memory/memory_resource), |
| 639 | [`std::pmr::polymorphic_allocator`](https://en.cppreference.com/w/cpp/memory/polymorphic_allocator) |
| 640 | |
| 641 | **Notes:** |
| 642 | *** promo |
| 643 | Banned because Chromium does not customize allocators |
| 644 | ([PartitionAlloc](https://chromium.googlesource.com/chromium/src/+/main/base/allocator/partition_allocator/PartitionAlloc.md) |
| 645 | is used globally). |
| 646 | *** |
| 647 | |
| 648 | ### std::timespec_get <sup>[banned]</sup> |
| 649 | |
| 650 | ```c++ |
| 651 | std::timespec ts; |
| 652 | std::timespec_get(&ts, TIME_UTC); |
| 653 | ``` |
| 654 | |
| 655 | **Description:** Gets the current calendar time in the given time base. |
| 656 | |
| 657 | **Documentation:** |
| 658 | [`std::timespec_get`](https://en.cppreference.com/w/cpp/chrono/c/timespec_get) |
| 659 | |
| 660 | **Notes:** |
| 661 | *** promo |
| 662 | Banned due to unclear, implementation-defined behavior. On POSIX, use |
| 663 | `base::TimeDelta::ToTimeSpec()`; this could be supported on other platforms if |
| 664 | desirable. |
Avi Drissman | bc6545f4 | 2022-05-03 17:47:38 | [diff] [blame] | 665 | *** |
| 666 | |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 667 | ### std::uncaught_exceptions <sup>[banned]</sup> |
| 668 | |
| 669 | ```c++ |
| 670 | int count = std::uncaught_exceptions(); |
| 671 | ``` |
| 672 | |
| 673 | **Description:** Determines whether there are live exception objects. |
| 674 | |
| 675 | **Documentation:** |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 676 | [`std::uncaught_exceptions`](https://en.cppreference.com/w/cpp/error/uncaught_exception) |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 677 | |
| 678 | **Notes:** |
| 679 | *** promo |
| 680 | Banned because exceptions are banned. |
| 681 | *** |
| 682 | |
| 683 | ### std::variant <sup>[banned]</sup> |
| 684 | |
| 685 | ```c++ |
| 686 | std::variant<int, double> v = 12; |
| 687 | ``` |
| 688 | |
| 689 | **Description:** The class template `std::variant` represents a type-safe |
| 690 | `union`. An instance of `std::variant` at any given time holds a value of one of |
| 691 | its alternative types (it's also possible for it to be valueless). |
| 692 | |
| 693 | **Documentation:** |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 694 | [`std::variant`](https://en.cppreference.com/w/cpp/utility/variant) |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 695 | |
| 696 | **Notes:** |
| 697 | *** promo |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 698 | [Will be allowed soon](https://crbug.com/1373620); for now, use `absl::variant`. |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 699 | *** |
| 700 | |
| 701 | ### Transparent std::owner_less <sup>[banned]</sup> |
| 702 | |
| 703 | ```c++ |
| 704 | std::map<std::weak_ptr<T>, U, std::owner_less<>> |
| 705 | ``` |
| 706 | |
| 707 | **Description:** Function object providing mixed-type owner-based ordering of |
| 708 | shared and weak pointers, regardless of the type of the pointee. |
| 709 | |
| 710 | **Documentation:** |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 711 | [`std::owner_less`](https://en.cppreference.com/w/cpp/memory/owner_less) |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 712 | |
| 713 | **Notes:** |
| 714 | *** promo |
| 715 | Banned since `std::shared_ptr` and `std::weak_ptr` are banned. |
| 716 | *** |
| 717 | |
| 718 | ### weak_from_this <sup>[banned]</sup> |
| 719 | |
| 720 | ```c++ |
| 721 | auto weak_ptr = weak_from_this(); |
| 722 | ``` |
| 723 | |
| 724 | **Description:** Returns a `std::weak_ptr<T>` that tracks ownership of `*this` |
| 725 | by all existing `std::shared_ptr`s that refer to `*this`. |
| 726 | |
| 727 | **Documentation:** |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 728 | [`std::enable_shared_from_this<T>::weak_from_this`](https://en.cppreference.com/w/cpp/memory/enable_shared_from_this/weak_from_this) |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 729 | |
| 730 | **Notes:** |
| 731 | *** promo |
| 732 | Banned since `std::shared_ptr` and `std::weak_ptr` are banned. |
Avi Drissman | 37b7d51 | 2022-04-01 22:01:40 | [diff] [blame] | 733 | *** |
| 734 | |
Arthur Sonzogni | d8517c9 | 2023-03-03 09:41:45 | [diff] [blame] | 735 | ## C++20 Allowed Language Features {#core-allowlist-20} |
| 736 | |
Peter Kasting | f86817c | 2023-11-13 18:00:11 | [diff] [blame] | 737 | The following C++20 language features are allowed in the Chromium codebase. |
| 738 | |
| 739 | ### Abbreviated function templates <sup>[allowed]</sup> |
| 740 | |
| 741 | ```c++ |
| 742 | // template <typename T> |
| 743 | // void f1(T x); |
| 744 | void f1(auto x); |
| 745 | |
| 746 | // template <C T> // `C` is a concept |
| 747 | // void f2(T x); |
| 748 | void f2(C auto x); |
| 749 | |
| 750 | // template <typename T, C U> // `C` is a concept |
| 751 | // void f3(T x, U y); |
| 752 | template <typename T> |
| 753 | void f3(T x, C auto y); |
| 754 | |
| 755 | // template<typename... Ts> |
| 756 | // void f4(Ts... xs); |
| 757 | void f4(auto... xs); |
| 758 | ``` |
| 759 | |
| 760 | **Description:** Function params of type `auto` become syntactic sugar for |
| 761 | declaring a template type for each such parameter. |
| 762 | |
| 763 | **Documentation:** |
| 764 | [Abbreviated function template](https://en.cppreference.com/w/cpp/language/function_template#Abbreviated_function_template) |
| 765 | |
| 766 | **Notes:** |
| 767 | *** promo |
| 768 | [Migration bug](https://crbug.com/1414526) |
| 769 | *** |
| 770 | |
| 771 | ### consteval <sup>[allowed]</sup> |
| 772 | |
| 773 | ```c++ |
| 774 | consteval int sqr(int n) { return n * n; } |
| 775 | constexpr int kHundred = sqr(10); // OK |
| 776 | constexpr int quad(int n) { return sqr(sqr(n)); } // ERROR, might be runtime |
| 777 | ``` |
| 778 | |
| 779 | **Description:** Specified that a function may only be used in a compile-time |
| 780 | context. |
| 781 | |
| 782 | **Documentation:** |
| 783 | [`consteval` specifier](https://en.cppreference.com/w/cpp/language/consteval) |
| 784 | |
| 785 | **Notes:** |
| 786 | *** promo |
| 787 | None |
| 788 | *** |
| 789 | |
| 790 | ### Constraints and concepts <sup>[allowed]</sup> |
| 791 | |
| 792 | ```c++ |
| 793 | // `Hashable` is a concept satisfied by any type `T` for which the expression |
| 794 | // `std::hash<T>{}(a)` compiles and produces a value convertible to `size_t`. |
| 795 | template<typename T> |
| 796 | concept Hashable = requires(T a) |
| 797 | { |
| 798 | { std::hash<T>{}(a) } -> std::convertible_to<size_t>; |
| 799 | }; |
| 800 | template <Hashable T> // Only instantiable for `T`s that satisfy `Hashable`. |
| 801 | void f(T) { ... } |
| 802 | ``` |
| 803 | |
| 804 | **Description:** Allows bundling sets of requirements together as named |
| 805 | concepts, then enforcing them on template arguments. |
| 806 | |
| 807 | **Documentation:** |
| 808 | [Constraints and concepts](https://en.cppreference.com/w/cpp/language/constraints) |
| 809 | |
| 810 | **Notes:** |
| 811 | *** promo |
| 812 | [Migration bug](https://crbug.com/1414528) |
| 813 | *** |
| 814 | |
| 815 | ### Default comparisons <sup>[allowed]</sup> |
| 816 | |
| 817 | ```c++ |
Roland Bock | 5dbdff6 | 2024-01-20 09:33:53 | [diff] [blame] | 818 | class S : public T { |
| 819 | // Non-member equality operator with access to private members. |
danakj | 4b9193e | 2024-02-02 17:31:33 | [diff] [blame] | 820 | // Compares `T` bases, then `x`, then `y`, short-circuiting when |
| 821 | // it finds inequality. |
Roland Bock | 5dbdff6 | 2024-01-20 09:33:53 | [diff] [blame] | 822 | friend bool operator==(const S&, const S&) = default; |
| 823 | |
danakj | 4b9193e | 2024-02-02 17:31:33 | [diff] [blame] | 824 | // Non-member ordering operator with access to private members. |
| 825 | // Compares `T` bases, then `x`, then `y`, short-circuiting when |
| 826 | // it finds an ordering difference. |
| 827 | friend auto operator<=>(const S&, const S&) = default; |
| 828 | |
Peter Kasting | f86817c | 2023-11-13 18:00:11 | [diff] [blame] | 829 | int x; |
| 830 | bool y; |
| 831 | }; |
| 832 | ``` |
| 833 | |
Roland Bock | 5dbdff6 | 2024-01-20 09:33:53 | [diff] [blame] | 834 | **Description:** Requests that the compiler generate the implementation of |
| 835 | any comparison operator, including `<=>`. Prefer non-member comparison |
| 836 | operators. When defaulting `<=>`, also explicitly default `==`. Together these |
| 837 | are sufficient to allow any comparison as long as callers do not need to take |
| 838 | the address of any non-declared operator. |
Peter Kasting | f86817c | 2023-11-13 18:00:11 | [diff] [blame] | 839 | |
| 840 | **Documentation:** |
| 841 | [Default comparisons](https://en.cppreference.com/w/cpp/language/default_comparisons) |
| 842 | |
| 843 | **Notes:** |
| 844 | *** promo |
danakj | 4b9193e | 2024-02-02 17:31:33 | [diff] [blame] | 845 | Unlike constructors/destructors, our compiler extensions do not require these |
| 846 | to be written out-of-line in the .cc file. Feel free to write `= default` |
| 847 | directly in the header, as this is much simpler to write. |
| 848 | |
| 849 | - [Migration bug](https://crbug.com/1414530) |
David Bokan | c246fc9 | 2024-02-28 17:55:47 | [diff] [blame] | 850 | - [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/h4lVi2jHU-0/m/X0q_Bh2IAAAJ) |
danakj | 4b9193e | 2024-02-02 17:31:33 | [diff] [blame] | 851 | |
Peter Kasting | f86817c | 2023-11-13 18:00:11 | [diff] [blame] | 852 | *** |
| 853 | |
Arthur Sonzogni | d8517c9 | 2023-03-03 09:41:45 | [diff] [blame] | 854 | ### Designated initializers <sup>[allowed]</sup> |
| 855 | |
| 856 | ```c++ |
| 857 | struct S { int x = 1; int y = 2; } |
| 858 | S s{ .y = 3 }; // OK, s.x == 1, s.y == 3 |
| 859 | ``` |
| 860 | |
| 861 | **Description:** Allows explicit initialization of subsets of aggregate members |
| 862 | at construction. |
| 863 | |
| 864 | **Documentation:** |
| 865 | [Designated initializers](https://en.cppreference.com/w/cpp/language/aggregate_initialization#Designated_initializers) |
| 866 | |
| 867 | **Notes:** |
| 868 | *** promo |
| 869 | None |
| 870 | *** |
| 871 | |
Peter Kasting | f86817c | 2023-11-13 18:00:11 | [diff] [blame] | 872 | ### __has_cpp_attribute <sup>[allowed]</sup> |
| 873 | |
| 874 | ```c++ |
| 875 | #if __has_cpp_attribute(assume) // Toolchain supports C++23 `[[assume]]`. |
| 876 | ... |
| 877 | #endif |
| 878 | ``` |
| 879 | |
| 880 | **Description:** Checks whether the toolchain supports a particular standard |
| 881 | attribute. |
| 882 | |
| 883 | **Documentation:** |
| 884 | [Feature testing](https://en.cppreference.com/w/cpp/feature_test) |
| 885 | |
| 886 | **Notes:** |
| 887 | *** promo |
| 888 | None |
| 889 | *** |
| 890 | |
| 891 | ### constinit <sup>[allowed]</sup> |
| 892 | |
| 893 | ```c++ |
| 894 | constinit int x = 3; |
| 895 | void foo() { |
| 896 | ++x; |
| 897 | } |
| 898 | ``` |
| 899 | |
| 900 | **Description:** Ensures that a variable can be compile-time initialized. This |
| 901 | is like a milder form of `constexpr` that does not force variables to be const |
| 902 | or have constant destruction. |
| 903 | |
| 904 | **Documentation:** |
| 905 | [`constinit` specifier](https://en.cppreference.com/w/cpp/language/constinit) |
| 906 | |
| 907 | **Notes:** |
| 908 | *** promo |
| 909 | [Migration bug](https://crbug.com/1414612) |
| 910 | *** |
| 911 | |
| 912 | ### Initializers for bit-field members <sup>[allowed]</sup> |
| 913 | |
| 914 | ```c++ |
| 915 | struct S { |
| 916 | uint32_t x : 27 = 2; |
| 917 | }; |
| 918 | ``` |
| 919 | |
| 920 | **Description:** Allows specifying the default initial value of a bit-field |
| 921 | member, as can already be done for other member types. |
| 922 | |
| 923 | **Documentation:** |
| 924 | [Bit-field](https://en.cppreference.com/w/cpp/language/bit_field) |
| 925 | |
| 926 | **Notes:** |
| 927 | *** promo |
| 928 | None |
| 929 | *** |
| 930 | |
| 931 | ### Lambda captures with initializers that are pack expansions <sup>[allowed]</sup> |
| 932 | |
| 933 | ```c++ |
| 934 | template <typename... Args> |
| 935 | void foo(Args... args) { |
| 936 | const auto l = [...n = args] { (x(n), ...); }; |
| 937 | } |
| 938 | ``` |
| 939 | |
| 940 | **Description:** Allows initializing a capture with a pack expansion. |
| 941 | |
| 942 | **Documentation:** |
| 943 | [Lambda capture](https://en.cppreference.com/w/cpp/language/lambda#Lambda_capture) |
| 944 | |
| 945 | **Notes:** |
| 946 | *** promo |
| 947 | None |
| 948 | *** |
| 949 | |
| 950 | ### Language feature-test macros <sup>[allowed]</sup> |
| 951 | |
| 952 | ```c++ |
| 953 | #if !defined(__cpp_modules) || (__cpp_modules < 201907L) |
| 954 | ... // Toolchain does not support modules |
| 955 | #endif |
| 956 | ``` |
| 957 | |
| 958 | **Description:** Provides a standardized way to test the toolchain's |
| 959 | implementation of a particular language feature. |
| 960 | |
| 961 | **Documentation:** |
| 962 | [Feature testing](https://en.cppreference.com/w/cpp/feature_test) |
| 963 | |
| 964 | **Notes:** |
| 965 | *** promo |
| 966 | None |
| 967 | *** |
| 968 | |
Peter Kasting | f86817c | 2023-11-13 18:00:11 | [diff] [blame] | 969 | ### Range-for statements with initializer <sup>[allowed]</sup> |
| 970 | |
| 971 | ```c++ |
| 972 | T foo(); |
| 973 | ... |
| 974 | for (auto& x : foo().items()) { ... } // UAF before C++23! |
| 975 | for (T thing = foo(); auto& x : thing.items()) { ... } // OK |
| 976 | ``` |
| 977 | |
| 978 | **Description:** Like C++17's selection statements with initializer. |
| 979 | Particularly useful before C++23, since temporaries inside range-expressions are |
| 980 | not lifetime-extended until the end of the loop before C++23. |
| 981 | |
| 982 | **Documentation:** |
| 983 | [Range-based `for` loop](https://en.cppreference.com/w/cpp/language/range-for) |
| 984 | |
| 985 | **Notes:** |
| 986 | *** promo |
| 987 | [Migration bug](https://crbug.com/1414531) |
| 988 | *** |
| 989 | |
| 990 | ### Three-way comparison ("spaceship") operator <sup>[allowed]</sup> |
| 991 | |
| 992 | ```c++ |
| 993 | // `ordering` is an instance of `std::strong_odering` or `std::partial_ordering` |
| 994 | // that describes how `a` and `b` are related. |
| 995 | const auto ordering = a <=> b; |
| 996 | if (ordering < 0) { ... } // `a` < `b` |
| 997 | else if (ordering > 0) { ... } // `a` > `b` |
| 998 | else { ... } // `a` == `b` |
| 999 | ``` |
| 1000 | |
| 1001 | **Description:** Compares two objects in a fashion similar to `strcmp`. Perhaps |
| 1002 | most useful when defined as an overload in a class, in which case it can replace |
| 1003 | definitions of other inequalities. See also "Default comparisons". |
| 1004 | |
| 1005 | **Documentation:** |
| 1006 | [Three-way comparison](https://en.cppreference.com/w/cpp/language/operator_comparison#Three-way_comparison) |
| 1007 | |
| 1008 | **Notes:** |
| 1009 | *** promo |
| 1010 | [Migration bug](https://crbug.com/1414530) |
| 1011 | *** |
| 1012 | |
| 1013 | ## C++20 Allowed Library Features {#library-allowlist-20} |
| 1014 | |
| 1015 | The following C++20 library features are allowed in the Chromium codebase. |
| 1016 | |
| 1017 | ### <bit> <sup>[allowed]</sup> |
| 1018 | |
| 1019 | ```c++ |
| 1020 | #include <bit> |
| 1021 | ``` |
| 1022 | |
| 1023 | **Description:** Provides various byte- and bit-twiddling functions, e.g. |
| 1024 | counting leading zeros. |
| 1025 | |
| 1026 | **Documentation:** |
| 1027 | [Standard library header `<bit>`](https://en.cppreference.com/w/cpp/header/bit) |
| 1028 | |
| 1029 | **Notes:** |
| 1030 | *** promo |
| 1031 | [Migration bug](https://crbug.com/1414634) |
| 1032 | *** |
| 1033 | |
| 1034 | ### <compare> <sup>[allowed]</sup> |
| 1035 | |
| 1036 | ```c++ |
| 1037 | #include <compare> |
| 1038 | ``` |
| 1039 | |
| 1040 | **Description:** Concepts and classes used to implement three-way comparison |
| 1041 | ("spaceship", `<=>`) support. |
| 1042 | |
| 1043 | **Documentation:** |
| 1044 | [Standard library header `<compare>`](https://en.cppreference.com/w/cpp/header/compare) |
| 1045 | |
| 1046 | **Notes:** |
| 1047 | *** promo |
| 1048 | None |
| 1049 | *** |
| 1050 | |
| 1051 | ### <concepts> <sup>[allowed]</sup> |
| 1052 | |
| 1053 | ```c++ |
| 1054 | #include <concepts> |
| 1055 | ``` |
| 1056 | |
| 1057 | **Description:** Various useful concepts, many of which replace pre-concept |
| 1058 | machinery in `<type_traits>`. |
| 1059 | |
| 1060 | **Documentation:** |
| 1061 | [Standard library header `<concepts>`](https://en.cppreference.com/w/cpp/header/concepts) |
| 1062 | |
| 1063 | **Notes:** |
| 1064 | *** promo |
| 1065 | None |
| 1066 | *** |
| 1067 | |
| 1068 | ### Library feature-test macros and <version> <sup>[allowed]</sup> |
| 1069 | |
| 1070 | ```c++ |
| 1071 | #if !defined(__cpp_lib_atomic_value_initialization) || \ |
| 1072 | (__cpp_lib_atomic_value_initialization < 201911L) |
| 1073 | ... // `std::atomic` is not value-initialized by default. |
| 1074 | #endif |
| 1075 | ``` |
| 1076 | |
| 1077 | **Description:** Provides a standardized way to test the toolchain's |
| 1078 | implementation of a particular library feature. |
| 1079 | |
| 1080 | **Documentation:** |
| 1081 | [Feature testing](https://en.cppreference.com/w/cpp/feature_test) |
| 1082 | |
| 1083 | **Notes:** |
| 1084 | *** promo |
| 1085 | None |
| 1086 | *** |
| 1087 | |
| 1088 | ### <numbers> <sup>[allowed]</sup> |
| 1089 | |
| 1090 | ```c++ |
| 1091 | #include <numbers> |
| 1092 | ``` |
| 1093 | |
| 1094 | **Description:** Provides compile-time constants for many common mathematical |
| 1095 | values, e.g. pi and e. |
| 1096 | |
| 1097 | **Documentation:** |
| 1098 | [Mathematical constants](https://en.cppreference.com/w/cpp/numeric/constants) |
| 1099 | |
| 1100 | **Notes:** |
| 1101 | *** promo |
| 1102 | [Migration bug](https://crbug.com/1414635) |
| 1103 | *** |
| 1104 | |
| 1105 | ### std::assume_aligned <sup>[allowed]</sup> |
| 1106 | |
| 1107 | ```c++ |
| 1108 | void f(int* p) { |
| 1109 | int* aligned = std::assume_aligned<256>(p); |
| 1110 | ... |
| 1111 | ``` |
| 1112 | |
| 1113 | **Description:** Informs the compiler that a pointer points to an address |
| 1114 | aligned to at least some particular power of 2. |
| 1115 | |
| 1116 | **Documentation:** |
| 1117 | [`std::assume_aligned`](https://en.cppreference.com/w/cpp/memory/assume_aligned) |
| 1118 | |
| 1119 | **Notes:** |
| 1120 | *** promo |
Peter Kasting | b2887c3 | 2024-01-08 21:41:04 | [diff] [blame] | 1121 | None |
Peter Kasting | f86817c | 2023-11-13 18:00:11 | [diff] [blame] | 1122 | *** |
| 1123 | |
| 1124 | ### std::erase[_if] for containers <sup>[allowed]</sup> |
| 1125 | |
| 1126 | ```c++ |
| 1127 | std::vector<int> numbers = ...; |
| 1128 | std::erase_if(numbers, [](int x) { return x % 2 == 0; }); |
| 1129 | ``` |
| 1130 | |
| 1131 | **Description:** Erases from a container by value comparison or predicate, |
| 1132 | avoiding the need to use the `erase(remove(...` paradigm. |
| 1133 | |
| 1134 | **Documentation:** |
| 1135 | [`std::erase`, `std::erase_if` (`std::vector`)](https://en.cppreference.com/w/cpp/container/vector/erase2) |
| 1136 | |
| 1137 | **Notes:** |
| 1138 | *** promo |
| 1139 | [Migration bug](https://crbug.com/1414639) |
| 1140 | *** |
| 1141 | |
| 1142 | ### std::is_[un]bounded_array <sup>[allowed]</sup> |
| 1143 | |
| 1144 | ```c++ |
| 1145 | template <typename T> |
| 1146 | static constexpr bool kBoundedArray = std::is_bounded_array_v<T>; |
| 1147 | ``` |
| 1148 | |
| 1149 | **Description:** Checks if a type is an array type with a known or unknown |
| 1150 | bound. |
| 1151 | |
| 1152 | **Documentation:** |
| 1153 | [`std::is_bounded_array`](https://en.cppreference.com/w/cpp/types/is_bounded_array), |
| 1154 | [`std::is_unbounded_array`](https://en.cppreference.com/w/cpp/types/is_unbounded_array) |
| 1155 | |
| 1156 | **Notes:** |
| 1157 | *** promo |
| 1158 | None |
| 1159 | *** |
| 1160 | |
| 1161 | ### std::lerp <sup>[allowed]</sup> |
| 1162 | |
| 1163 | ```c++ |
| 1164 | double val = std::lerp(start, end, t); |
| 1165 | ``` |
| 1166 | |
| 1167 | **Description:** Linearly interpolates (or extrapolates) between two values. |
| 1168 | |
| 1169 | **Documentation:** |
| 1170 | [`std::lerp`](https://en.cppreference.com/w/cpp/numeric/lerp) |
| 1171 | |
| 1172 | **Notes:** |
| 1173 | *** promo |
| 1174 | [Migration bug](https://crbug.com/1414537) |
| 1175 | *** |
| 1176 | |
| 1177 | ### std::make_obj_using_allocator etc. <sup>[allowed]</sup> |
| 1178 | |
| 1179 | ```c++ |
| 1180 | auto obj = std::make_obj_using_allocator<Obj>(alloc, ...); |
| 1181 | ``` |
| 1182 | |
| 1183 | **Description:** Constructs an object using |
| 1184 | [uses-allocator construction](https://en.cppreference.com/w/cpp/memory/uses_allocator). |
| 1185 | |
| 1186 | **Documentation:** |
| 1187 | [`std::make_obj_using_allocator`](https://en.cppreference.com/w/cpp/memory/make_obj_using_allocator) |
| 1188 | |
| 1189 | **Notes:** |
| 1190 | *** promo |
| 1191 | None |
| 1192 | *** |
| 1193 | |
| 1194 | ### std::make_unique_for_overwrite <sup>[allowed]</sup> |
| 1195 | |
| 1196 | ```c++ |
| 1197 | auto ptr = std::make_unique_for_overwrite<int>(); // `*ptr` is uninitialized |
| 1198 | ``` |
| 1199 | |
| 1200 | **Description:** Like calling `std::unique_ptr<T>(new T)` instead of the more |
| 1201 | typical `std::unique_ptr<T>(new T(...))`. |
| 1202 | |
| 1203 | **Documentation:** |
| 1204 | [`std::make_unique`, `std::make_unique_for_overwrite`](https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique) |
| 1205 | |
| 1206 | **Notes:** |
| 1207 | *** promo |
| 1208 | None |
| 1209 | *** |
| 1210 | |
| 1211 | ### std::midpoint <sup>[allowed]</sup> |
| 1212 | |
| 1213 | ```c++ |
| 1214 | int center = std::midpoint(top, bottom); |
| 1215 | ``` |
| 1216 | |
| 1217 | **Description:** Finds the midpoint between its two arguments, avoiding any |
| 1218 | possible overflow. For integral inputs, rounds towards the first argument. |
| 1219 | |
| 1220 | **Documentation:** |
| 1221 | [`std::midpoint`](https://en.cppreference.com/w/cpp/numeric/midpoint) |
| 1222 | |
| 1223 | **Notes:** |
| 1224 | *** promo |
| 1225 | [Migration bug](https://crbug.com/1414539) |
| 1226 | *** |
| 1227 | |
| 1228 | ### std::remove_cvref[_t] <sup>[allowed]</sup> |
| 1229 | |
| 1230 | ```c++ |
| 1231 | template <typename T, |
| 1232 | typename = std::enable_if_t<std::is_same_v<std::remove_cvref_t<T>, |
| 1233 | int>>> |
| 1234 | void foo(T t); |
| 1235 | ``` |
| 1236 | |
| 1237 | **Description:** Provides a way to remove const, volatile, and reference |
| 1238 | qualifiers from a type. |
| 1239 | |
| 1240 | **Documentation:** |
| 1241 | [`std::remove_cvref`](https://en.cppreference.com/w/cpp/types/remove_cvref) |
| 1242 | |
| 1243 | **Notes:** |
| 1244 | *** promo |
Peter Kasting | b2887c3 | 2024-01-08 21:41:04 | [diff] [blame] | 1245 | None |
Peter Kasting | f86817c | 2023-11-13 18:00:11 | [diff] [blame] | 1246 | *** |
| 1247 | |
| 1248 | ### std::ssize <sup>[allowed]</sup> |
| 1249 | |
| 1250 | ```c++ |
| 1251 | str.replace(it, it + std::ssize(substr), 1, 'x'); |
| 1252 | ``` |
| 1253 | |
| 1254 | **Description:** Returns the size of an object as a signed type. |
| 1255 | |
| 1256 | **Documentation:** |
| 1257 | [`std::size`, `std::ssize`](https://en.cppreference.com/w/cpp/iterator/size) |
| 1258 | |
| 1259 | **Notes:** |
| 1260 | *** promo |
| 1261 | [Migration bug](https://crbug.com/1414543) |
| 1262 | *** |
| 1263 | |
| 1264 | ### std::string::(starts,ends)_with <sup>[allowed]</sup> |
| 1265 | |
| 1266 | ```c++ |
| 1267 | const std::string str = "Foo bar"; |
| 1268 | const bool is_true = str.ends_with("bar"); |
| 1269 | ``` |
| 1270 | |
| 1271 | **Description:** Tests whether a string starts or ends with a particular |
| 1272 | character or string. |
| 1273 | |
| 1274 | **Documentation:** |
| 1275 | [`std::basic_string<CharT,Traits,Allocator>::starts_with`](https://en.cppreference.com/w/cpp/string/basic_string/starts_with), |
| 1276 | [`std::basic_string<CharT,Traits,Allocator>::ends_with`](https://en.cppreference.com/w/cpp/string/basic_string/ends_with) |
| 1277 | |
| 1278 | **Notes:** |
| 1279 | *** promo |
| 1280 | [Migration bug](https://crbug.com/1414647) |
| 1281 | *** |
| 1282 | |
Peter Kasting | f86817c | 2023-11-13 18:00:11 | [diff] [blame] | 1283 | ## C++20 Banned Language Features {#core-blocklist-20} |
| 1284 | |
| 1285 | The following C++20 language features are not allowed in the Chromium codebase. |
| 1286 | |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 1287 | ### char8_t <sup>[banned]</sup> |
| 1288 | |
| 1289 | ```c++ |
| 1290 | char8_t c = u8'x'; |
| 1291 | ``` |
| 1292 | |
| 1293 | **Description:** A single UTF-8 code unit. Similar to `unsigned char`, but |
| 1294 | considered a distinct type. |
| 1295 | |
| 1296 | **Documentation:** |
| 1297 | [Fundamental types](https://en.cppreference.com/w/cpp/language/types#char8_t) |
| 1298 | |
| 1299 | **Notes:** |
| 1300 | *** promo |
| 1301 | Use `char` and unprefixed character literals. Non-UTF-8 encodings are rare |
| 1302 | enough in Chromium that the value of distinguishing them at the type level is |
| 1303 | low, and `char8_t*` is not interconvertible with `char*` (what ~all Chromium, |
| 1304 | STL, and platform-specific APIs use), so using `u8` prefixes would obligate us |
| 1305 | to insert casts everywhere. If you want to declare at a type level that a block |
| 1306 | of data is string-like and not an arbitrary binary blob, prefer |
| 1307 | `std::string[_view]` over `char*`. |
| 1308 | *** |
| 1309 | |
Peter Kasting | f86817c | 2023-11-13 18:00:11 | [diff] [blame] | 1310 | ### Modules <sup>[banned]</sup> |
| 1311 | |
| 1312 | ```c++ |
| 1313 | export module helloworld; // module declaration |
| 1314 | |
| 1315 | import <iostream>; // import declaration |
| 1316 | |
| 1317 | export void hello() { // export declaration |
| 1318 | std::cout << "Hello world!\n"; |
| 1319 | } |
| 1320 | ``` |
| 1321 | |
| 1322 | **Description:** Modules provide an alternative to many uses of headers which |
| 1323 | allows for faster compilation, better tooling support, and reduction of problems |
| 1324 | like "include what you use". |
| 1325 | |
| 1326 | **Documentation:** |
| 1327 | [Modules](https://en.cppreference.com/w/cpp/language/modules) |
| 1328 | |
| 1329 | **Notes:** |
| 1330 | *** promo |
| 1331 | Not yet sufficiently supported in Clang and GN. Re-evaluate when support |
| 1332 | improves. |
| 1333 | *** |
| 1334 | |
Peter Kasting | 8bc046d2 | 2023-11-14 00:38:03 | [diff] [blame] | 1335 | ### [[no_unique_address]] <sup>[banned]</sup> |
| 1336 | |
| 1337 | ```c++ |
| 1338 | struct Empty {}; |
| 1339 | struct X { |
| 1340 | int i; |
| 1341 | [[no_unique_address]] Empty e; |
| 1342 | }; |
| 1343 | ``` |
| 1344 | |
| 1345 | **Description:** Allows a data member to be overlapped with other members. |
| 1346 | |
| 1347 | **Documentation:** |
| 1348 | [C++ attribute: `no_unique_address`](https://en.cppreference.com/w/cpp/language/attributes/no_unique_address) |
| 1349 | |
| 1350 | **Notes:** |
| 1351 | *** promo |
| 1352 | Has no effect on Windows, for compatibility with Microsoft's ABI. Use |
| 1353 | `NO_UNIQUE_ADDRESS` from `base/compiler_specific.h` instead. Do not use (either |
| 1354 | form) on members of unions due to |
| 1355 | [potential memory safety problems](https://github.com/llvm/llvm-project/issues/60711). |
Peter Kasting | 8bc046d2 | 2023-11-14 00:38:03 | [diff] [blame] | 1356 | *** |
| 1357 | |
Peter Kasting | f86817c | 2023-11-13 18:00:11 | [diff] [blame] | 1358 | ## C++20 Banned Library Features {#library-blocklist-20} |
| 1359 | |
| 1360 | The following C++20 library features are not allowed in the Chromium codebase. |
| 1361 | |
| 1362 | ### std::atomic_ref <sup>[banned]</sup> |
| 1363 | |
| 1364 | ```c++ |
| 1365 | struct S { int a; int b; }; |
| 1366 | S not_atomic; |
| 1367 | std::atomic_ref<S> is_atomic(not_atomic); |
| 1368 | ``` |
| 1369 | |
| 1370 | **Description:** Allows atomic access to objects that might not themselves be |
| 1371 | atomic types. While any atomic_ref to an object exists, the object must be |
| 1372 | accessed exclusively through atomic_ref instances. |
| 1373 | |
| 1374 | **Documentation:** |
| 1375 | [`std::atomic_ref`](https://en.cppreference.com/w/cpp/atomic/atomic_ref) |
| 1376 | |
| 1377 | **Notes:** |
| 1378 | *** promo |
| 1379 | Banned due to being [unimplemented in libc++](https://reviews.llvm.org/D72240). |
| 1380 | |
| 1381 | [Migration bug](https://crbug.com/1422701) (once this is allowed) |
| 1382 | *** |
| 1383 | |
| 1384 | ### std::bind_front <sup>[banned]</sup> |
| 1385 | |
| 1386 | ```c++ |
| 1387 | int minus(int a, int b); |
| 1388 | auto fifty_minus_x = std::bind_front(minus, 50); |
| 1389 | int forty = fifty_minus_x(10); |
| 1390 | ``` |
| 1391 | |
| 1392 | **Description:** An updated version of `std::bind` with fewer gotchas, similar |
| 1393 | to `absl::bind_front`. |
| 1394 | |
| 1395 | **Documentation:** |
| 1396 | [`std::bind_front`, `std::bind_back`](https://en.cppreference.com/w/cpp/utility/functional/bind_front) |
| 1397 | |
| 1398 | **Notes:** |
| 1399 | *** promo |
| 1400 | Overlaps with `base::Bind`. |
| 1401 | *** |
| 1402 | |
Avi Drissman | 70cb7f7 | 2023-12-12 17:44:37 | [diff] [blame] | 1403 | ### std::bit_cast <sup>[banned]</sup> |
| 1404 | |
| 1405 | ```c++ |
| 1406 | float quake_rsqrt(float number) { |
| 1407 | long i = std::bit_cast<long>(number); |
| 1408 | i = 0x5f3759df - (i >> 1); // wtf? |
| 1409 | float y = std::bit_cast<float>(i); |
| 1410 | return y * (1.5f - (0.5f * number * y * y)); |
| 1411 | } |
| 1412 | ``` |
| 1413 | |
| 1414 | **Description:** Returns an value constructed with the same bits as an value of |
| 1415 | a different type. |
| 1416 | |
| 1417 | **Documentation:** |
| 1418 | [`std::bit_cast`](https://en.cppreference.com/w/cpp/numeric/bit_cast) |
| 1419 | |
| 1420 | **Notes:** |
| 1421 | *** promo |
| 1422 | The `std::` version of `bit_cast` allows casting of pointer and reference types, |
| 1423 | which is both useless in that it doesn't avoid UB, and dangerous in that it |
| 1424 | allows arbitrary casting away of modifiers like `const`. Instead of using |
| 1425 | `bit_cast` on pointers, use standard C++ casts. For use on values, use |
| 1426 | `base::bit_cast` which does not allow this unwanted usage. |
| 1427 | *** |
| 1428 | |
Peter Kasting | f86817c | 2023-11-13 18:00:11 | [diff] [blame] | 1429 | ### std::{c8rtomb,mbrtoc8} <sup>[banned]</sup> |
| 1430 | |
| 1431 | ```c++ |
| 1432 | std::u8string_view strv = u8"zß水🍌"; |
| 1433 | std::mbstate_t state; |
| 1434 | char out[MB_LEN_MAX] = {0}; |
| 1435 | for (char8_t c : strv) { |
| 1436 | size_t rc = std::c8rtomb(out, c, &state); |
| 1437 | ... |
| 1438 | ``` |
| 1439 | |
| 1440 | **Description:** Converts a code point between UTF-8 and a multibyte character |
| 1441 | encoded using the current C locale. |
| 1442 | |
| 1443 | **Documentation:** |
| 1444 | [`std::c8rtomb`](https://en.cppreference.com/w/cpp/string/multibyte/c8rtomb), |
| 1445 | [`std::mbrtoc8`](https://en.cppreference.com/w/cpp/string/multibyte/mbrtoc8) |
| 1446 | |
| 1447 | **Notes:** |
| 1448 | *** promo |
| 1449 | Chromium functionality should not vary with the C locale. |
| 1450 | *** |
| 1451 | |
Nick Diego Yamane | e522ae8 | 2024-02-27 04:23:22 | [diff] [blame] | 1452 | ### std::to_address <sup>[banned]</sup> |
| 1453 | |
| 1454 | ```c++ |
| 1455 | std::vector<int> numbers; |
| 1456 | int* i = std::to_address(numbers.begin()); |
| 1457 | ``` |
| 1458 | |
| 1459 | **Description:** Converts a pointer-like object to a pointer, even if the |
| 1460 | pointer does not refer to a constructed object (in which case an expression like |
| 1461 | `&*p` is UB). |
| 1462 | |
| 1463 | **Documentation:** |
| 1464 | [`std::to_address`](https://en.cppreference.com/w/cpp/memory/to_address) |
| 1465 | |
| 1466 | **Notes:** |
| 1467 | *** promo |
| 1468 | Banned because it is not guaranteed to be SFINAE-compatible. Use |
| 1469 | base::to_address, which does guarantee this. |
| 1470 | *** |
| 1471 | |
Peter Kasting | f86817c | 2023-11-13 18:00:11 | [diff] [blame] | 1472 | ### <syncstream> <sup>[banned]</sup> |
| 1473 | |
| 1474 | ```c++ |
| 1475 | #include <syncstream> |
| 1476 | ``` |
| 1477 | |
| 1478 | **Description:** Facilities for multithreaded access to streams. |
| 1479 | |
| 1480 | **Documentation:** |
| 1481 | [Standard library header `<syncstream>`](https://en.cppreference.com/w/cpp/header/syncstream) |
| 1482 | |
| 1483 | **Notes:** |
| 1484 | *** promo |
| 1485 | Banned due to being unimplemented per |
| 1486 | [the libc++ C++20 status page](https://libcxx.llvm.org/Status/Cxx20.html). |
| 1487 | Reevaluate usefulness once implemented. |
| 1488 | *** |
| 1489 | |
| 1490 | ## C++20 TBD Language Features {#core-review-20} |
| 1491 | |
| 1492 | The following C++20 language features are not allowed in the Chromium codebase. |
| 1493 | See the top of this page on how to propose moving a feature from this list into |
| 1494 | the allowed or banned sections. |
| 1495 | |
| 1496 | ### Aggregate initialization using parentheses <sup>[tbd]</sup> |
| 1497 | |
| 1498 | ```c++ |
| 1499 | struct B { |
| 1500 | int a; |
| 1501 | int&& r; |
| 1502 | } b2(1, 1); // Warning: dangling reference |
| 1503 | ``` |
| 1504 | |
| 1505 | **Description:** Allows initialization of aggregates using parentheses, not just |
| 1506 | braces. |
| 1507 | |
| 1508 | **Documentation:** |
| 1509 | [Aggregate initialization](https://en.cppreference.com/w/cpp/language/aggregate_initialization), |
| 1510 | [Direct initialization](https://en.cppreference.com/w/cpp/language/direct_initialization) |
| 1511 | |
| 1512 | **Notes:** |
| 1513 | *** promo |
| 1514 | There are subtle but important differences between brace- and paren-init of |
| 1515 | aggregates. The parenthesis style appears to have more pitfalls (allowing |
| 1516 | narrowing conversions, not extending lifetimes of temporaries bound to |
| 1517 | references). |
| 1518 | *** |
| 1519 | |
Peter Kasting | f86817c | 2023-11-13 18:00:11 | [diff] [blame] | 1520 | ### Coroutines <sup>[tbd]</sup> |
| 1521 | |
| 1522 | ```c++ |
| 1523 | co_return 1; |
| 1524 | ``` |
| 1525 | |
| 1526 | **Description:** Allows writing functions that logically block while physically |
| 1527 | returning control to a caller. This enables writing some kinds of async code in |
| 1528 | simple, straight-line ways without storing state in members or binding |
| 1529 | callbacks. |
| 1530 | |
| 1531 | **Documentation:** |
| 1532 | [Coroutines](https://en.cppreference.com/w/cpp/language/coroutines) |
| 1533 | |
| 1534 | **Notes:** |
| 1535 | *** promo |
| 1536 | Requires significant support code and planning around API and migration. |
| 1537 | |
| 1538 | [Prototyping bug](https://crbug.com/1403840) |
| 1539 | *** |
| 1540 | |
| 1541 | ### [[likely]], [[unlikely]] <sup>[tbd]</sup> |
| 1542 | |
| 1543 | ```c++ |
| 1544 | if (n > 0) [[likely]] { |
| 1545 | return 1; |
| 1546 | } |
| 1547 | ``` |
| 1548 | |
| 1549 | **Description:** Tells the optimizer that a particular codepath is more or less |
| 1550 | likely than an alternative. |
| 1551 | |
| 1552 | **Documentation:** |
| 1553 | [C++ attribute: `likely`, `unlikely`](https://en.cppreference.com/w/cpp/language/attributes/likely) |
| 1554 | |
| 1555 | **Notes:** |
| 1556 | *** promo |
| 1557 | [Will be allowed soon](https://crbug.com/1414620); for now, use `[UN]LIKELY`. |
| 1558 | *** |
| 1559 | |
| 1560 | ## C++20 TBD Library Features {#library-review-20} |
| 1561 | |
| 1562 | The following C++20 library features are not allowed in the Chromium codebase. |
| 1563 | See the top of this page on how to propose moving a feature from this list into |
| 1564 | the allowed or banned sections. |
| 1565 | |
| 1566 | ### <coroutine> <sup>[tbd]</sup> |
| 1567 | |
| 1568 | ```c++ |
| 1569 | #include <coroutine> |
| 1570 | ``` |
| 1571 | |
| 1572 | **Description:** Header which defines various core coroutine types. |
| 1573 | |
| 1574 | **Documentation:** |
| 1575 | [Coroutine support](https://en.cppreference.com/w/cpp/coroutine) |
| 1576 | |
| 1577 | **Notes:** |
| 1578 | *** promo |
| 1579 | See notes on "Coroutines" above. |
| 1580 | *** |
| 1581 | |
| 1582 | ### <format> <sup>[tbd]</sup> |
| 1583 | |
| 1584 | ```c++ |
| 1585 | std::cout << std::format("Hello {}!\n", "world"); |
| 1586 | ``` |
| 1587 | |
| 1588 | **Description:** Utilities for producing formatted strings. |
| 1589 | |
| 1590 | **Documentation:** |
| 1591 | [Formatting library](https://en.cppreference.com/w/cpp/utility/format) |
| 1592 | |
| 1593 | **Notes:** |
| 1594 | *** promo |
| 1595 | Has both pros and cons compared to `absl::StrFormat` (which we don't yet use). |
| 1596 | Migration would be nontrivial. |
| 1597 | *** |
| 1598 | |
| 1599 | ### <ranges> <sup>[tbd]</sup> |
| 1600 | |
| 1601 | ```c++ |
| 1602 | constexpr int arr[] = {6, 2, 8, 4, 4, 2}; |
| 1603 | constexpr auto plus_one = std::views::transform([](int n){ return n + 1; }); |
| 1604 | static_assert(std::ranges::equal(arr | plus_one, {7, 3, 9, 5, 5, 3})); |
| 1605 | ``` |
| 1606 | |
| 1607 | **Description:** Generalizes algorithms using range views, which are lightweight |
| 1608 | objects that represent iterable sequences. Provides facilities for eager and |
| 1609 | lazy operations on ranges, along with composition into pipelines. |
| 1610 | |
| 1611 | **Documentation:** |
| 1612 | [Ranges library](https://en.cppreference.com/w/cpp/ranges) |
| 1613 | |
| 1614 | **Notes:** |
| 1615 | *** promo |
| 1616 | Significant concerns expressed internally. We should consider whether there are |
| 1617 | clearly-safe pieces to allow (e.g. to replace `base/ranges/algorithm.h`) and |
| 1618 | engage with the internal library team. |
| 1619 | *** |
| 1620 | |
| 1621 | ### <source_location> <sup>[tbd]</sup> |
| 1622 | |
| 1623 | ```c++ |
| 1624 | #include <source_location> |
| 1625 | ``` |
| 1626 | |
| 1627 | **Description:** Provides a class that can hold source code details such as |
| 1628 | filenames, function names, and line numbers. |
| 1629 | |
| 1630 | **Documentation:** |
| 1631 | [Standard library header `<source_location>`](https://en.cppreference.com/w/cpp/header/source_location) |
| 1632 | |
| 1633 | **Notes:** |
| 1634 | *** promo |
| 1635 | Seems to regress code size vs. `base::Location`. |
| 1636 | *** |
| 1637 | |
| 1638 | ### <span> <sup>[tbd]</sup> |
| 1639 | |
| 1640 | ```c++ |
| 1641 | #include <span> |
| 1642 | ``` |
| 1643 | |
| 1644 | **Description:** Utilities for non-owning views over a sequence of objects. |
| 1645 | |
| 1646 | **Documentation:** |
| 1647 | [](https://en.cppreference.com/w/cpp/header/span) |
| 1648 | |
| 1649 | **Notes:** |
| 1650 | *** promo |
| 1651 | Use `base::span` for now. |
| 1652 | |
| 1653 | [Migration bug](https://crbug.com/1414652) |
| 1654 | *** |
| 1655 | |
| 1656 | ### std::u8string <sup>[tbd]</sup> |
| 1657 | |
| 1658 | ```c++ |
| 1659 | std::u8string str = u8"Foo"; |
| 1660 | ``` |
| 1661 | |
| 1662 | **Description:** A string whose character type is `char8_t`, intended to hold |
| 1663 | UTF-8-encoded text. |
| 1664 | |
| 1665 | **Documentation:** |
| 1666 | [`std::basic_string`](https://en.cppreference.com/w/cpp/string/basic_string) |
| 1667 | |
| 1668 | **Notes:** |
| 1669 | *** promo |
| 1670 | See notes on `char8_t` above. |
| 1671 | *** |
| 1672 | |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1673 | ## Abseil Banned Library Features {#absl-blocklist} |
| 1674 | |
| 1675 | The following Abseil library features are not allowed in the Chromium codebase. |
| 1676 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 1677 | ### Any <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1678 | |
| 1679 | ```c++ |
| 1680 | absl::any a = int{5}; |
| 1681 | EXPECT_THAT(absl::any_cast<int>(&a), Pointee(5)); |
| 1682 | EXPECT_EQ(absl::any_cast<size_t>(&a), nullptr); |
| 1683 | ``` |
| 1684 | |
| 1685 | **Description:** Early adaptation of C++17 `std::any`. |
| 1686 | |
| 1687 | **Documentation:** [std::any](https://en.cppreference.com/w/cpp/utility/any) |
| 1688 | |
| 1689 | **Notes:** |
| 1690 | *** promo |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 1691 | Banned since workaround for lack of RTTI |
| 1692 | [isn't compatible with the component build](https://crbug.com/1096380). See also |
| 1693 | `std::any`. |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1694 | *** |
| 1695 | |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 1696 | ### bind_front <sup>[banned]</sup> |
| 1697 | |
| 1698 | ```c++ |
| 1699 | absl::bind_front |
| 1700 | ``` |
| 1701 | |
| 1702 | **Description:** Binds the first N arguments of an invocable object and stores them by value. |
| 1703 | |
| 1704 | **Documentation:** |
| 1705 | * [bind_front.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/bind_front.h) |
| 1706 | * [Avoid std::bind](https://abseil.io/tips/108) |
| 1707 | |
| 1708 | **Notes:** |
| 1709 | *** promo |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 1710 | Overlaps with `base::Bind`. |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 1711 | *** |
| 1712 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 1713 | ### Command line flags <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1714 | |
| 1715 | ```c++ |
| 1716 | ABSL_FLAG(bool, logs, false, "print logs to stderr"); |
| 1717 | app --logs=true; |
| 1718 | ``` |
| 1719 | |
| 1720 | **Description:** Allows programmatic access to flag values passed on the |
| 1721 | command-line to binaries. |
| 1722 | |
| 1723 | **Documentation:** [Flags Library](https://abseil.io/docs/cpp/guides/flags) |
| 1724 | |
| 1725 | **Notes:** |
| 1726 | *** promo |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 1727 | Banned since workaround for lack of RTTI |
| 1728 | [isn't compatible with the component build](https://crbug.com/1096380). Use |
| 1729 | `base::CommandLine` instead. |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1730 | *** |
| 1731 | |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 1732 | ### Container utilities <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1733 | |
| 1734 | ```c++ |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 1735 | auto it = absl::c_find(container, value); |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1736 | ``` |
| 1737 | |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 1738 | **Description:** Container-based versions of algorithmic functions within C++ |
| 1739 | standard library. |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1740 | |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 1741 | **Documentation:** |
| 1742 | [container.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/algorithm/container.h) |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1743 | |
| 1744 | **Notes:** |
| 1745 | *** promo |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 1746 | Overlaps with `base/ranges/algorithm.h`. |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1747 | *** |
| 1748 | |
Peter Kasting | 431239a | 2023-09-29 03:11:44 | [diff] [blame] | 1749 | ### FixedArray <sup>[banned]</sup> |
| 1750 | |
| 1751 | ```c++ |
| 1752 | absl::FixedArray<MyObj> objs_; |
| 1753 | ``` |
| 1754 | |
| 1755 | **Description:** A fixed size array like `std::array`, but with size determined |
| 1756 | at runtime instead of compile time. |
| 1757 | |
| 1758 | **Documentation:** |
| 1759 | [fixed_array.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/container/fixed_array.h) |
| 1760 | |
| 1761 | **Notes:** |
| 1762 | *** promo |
| 1763 | Direct construction is banned due to the risk of UB with uninitialized |
| 1764 | trivially-default-constructible types. Instead use `base/types/fixed_array.h`, |
| 1765 | which is a light-weight wrapper that deletes the problematic constructor. |
Joe Mason | 6a6f258 | 2024-01-30 20:26:43 | [diff] [blame] | 1766 | *** |
Peter Kasting | 431239a | 2023-09-29 03:11:44 | [diff] [blame] | 1767 | |
Daniel Cheng | 2248b33 | 2022-07-27 06:16:59 | [diff] [blame] | 1768 | ### FunctionRef <sup>[banned]</sup> |
| 1769 | |
| 1770 | ```c++ |
| 1771 | absl::FunctionRef |
| 1772 | ``` |
| 1773 | |
| 1774 | **Description:** Type for holding a non-owning reference to an object of any |
| 1775 | invocable type. |
| 1776 | |
| 1777 | **Documentation:** |
| 1778 | [function_ref.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/function_ref.h) |
| 1779 | |
| 1780 | **Notes:** |
| 1781 | *** promo |
| 1782 | - `absl::FunctionRef` is banned due to allowing implicit conversions between |
| 1783 | function signatures in potentially surprising ways. For example, a callable |
| 1784 | with the signature `int()` will bind to `absl::FunctionRef<void()>`: the |
| 1785 | return value from the callable will be silently discarded. |
| 1786 | - In Chromium, use `base::FunctionRef` instead. |
| 1787 | - Unlike `base::OnceCallback` and `base::RepeatingCallback`, `base::FunctionRef` |
| 1788 | supports capturing lambdas. |
| 1789 | - Useful when passing an invocable object to a function that synchronously calls |
| 1790 | the invocable object, e.g. `ForEachFrame(base::FunctionRef<void(Frame&)>)`. |
| 1791 | This can often result in clearer code than code that is templated to accept |
| 1792 | lambdas, e.g. with `template <typename Invocable> void |
| 1793 | ForEachFrame(Invocable invocable)`, it is much less obvious what arguments |
| 1794 | will be passed to `invocable`. |
| 1795 | - For now, `base::OnceCallback` and `base::RepeatingCallback` intentionally |
| 1796 | disallow conversions to `base::FunctionRef`, under the theory that the |
| 1797 | callback should be a capturing lambda instead. Attempting to use this |
| 1798 | conversion will trigger a `static_assert` requesting additional feedback for |
| 1799 | use cases where this conversion would be valuable. |
| 1800 | - *Important:* `base::FunctionRef` must not outlive the function call. Like |
Helmut Januschka | 1dce9dc | 2024-06-11 13:05:35 | [diff] [blame^] | 1801 | `std::string_view`, `base::FunctionRef` is a *non-owning* reference. Using a |
Daniel Cheng | 2248b33 | 2022-07-27 06:16:59 | [diff] [blame] | 1802 | `base::FunctionRef` as a return value or class field is dangerous and likely |
| 1803 | to result in lifetime bugs. |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 1804 | |
| 1805 | [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/JVN4E4IIYA0) |
Daniel Cheng | 2248b33 | 2022-07-27 06:16:59 | [diff] [blame] | 1806 | *** |
| 1807 | |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 1808 | ### Random <sup>[banned]</sup> |
| 1809 | |
| 1810 | ```c++ |
| 1811 | absl::BitGen bitgen; |
| 1812 | size_t index = absl::Uniform(bitgen, 0u, elems.size()); |
| 1813 | ``` |
| 1814 | |
| 1815 | **Description:** Functions and utilities for generating pseudorandom data. |
| 1816 | |
| 1817 | **Documentation:** [Random library](https://abseil.io/docs/cpp/guides/random) |
| 1818 | |
| 1819 | **Notes:** |
| 1820 | *** promo |
| 1821 | Banned because most uses of random values in Chromium should be using a |
| 1822 | cryptographically secure generator. Use `base/rand_util.h` instead. |
| 1823 | *** |
| 1824 | |
| 1825 | ### Span <sup>[banned]</sup> |
| 1826 | |
| 1827 | ```c++ |
| 1828 | absl::Span |
| 1829 | ``` |
| 1830 | |
| 1831 | **Description:** Early adaptation of C++20 `std::span`. |
| 1832 | |
| 1833 | **Documentation:** [Using absl::Span](https://abseil.io/tips/93) |
| 1834 | |
| 1835 | **Notes:** |
| 1836 | *** promo |
| 1837 | Banned due to being less std::-compliant than `base::span`. Keep using |
| 1838 | `base::span`. |
| 1839 | *** |
| 1840 | |
| 1841 | ### StatusOr <sup>[banned]</sup> |
| 1842 | |
| 1843 | ```c++ |
| 1844 | absl::StatusOr<T> |
| 1845 | ``` |
| 1846 | |
| 1847 | **Description:** An object that is either a usable value, or an error Status |
| 1848 | explaining why such a value is not present. |
| 1849 | |
| 1850 | **Documentation:** |
| 1851 | [statusor.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/status/statusor.h) |
| 1852 | |
| 1853 | **Notes:** |
| 1854 | *** promo |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 1855 | Overlaps with `base::expected`. |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 1856 | *** |
| 1857 | |
| 1858 | ### String Formatting <sup>[banned]</sup> |
| 1859 | |
| 1860 | ```c++ |
| 1861 | absl::StrFormat |
| 1862 | ``` |
| 1863 | |
| 1864 | **Description:** A typesafe replacement for the family of printf() string |
| 1865 | formatting routines. |
| 1866 | |
| 1867 | **Documentation:** |
| 1868 | [String Formatting](https://abseil.io/docs/cpp/guides/format) |
| 1869 | |
| 1870 | **Notes:** |
| 1871 | *** promo |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 1872 | Overlaps with `base::StringPrintf()`. See |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 1873 | [migration bug](https://bugs.chromium.org/p/chromium/issues/detail?id=1371963). |
| 1874 | *** |
| 1875 | |
| 1876 | ### string_view <sup>[banned]</sup> |
| 1877 | |
| 1878 | ```c++ |
| 1879 | absl::string_view |
| 1880 | ``` |
| 1881 | |
| 1882 | **Description:** Early adaptation of C++17 `std::string_view`. |
| 1883 | |
| 1884 | **Documentation:** [absl::string_view](https://abseil.io/tips/1) |
| 1885 | |
| 1886 | **Notes:** |
| 1887 | *** promo |
David Benjamin | ea985a2 | 2023-04-18 22:05:01 | [diff] [blame] | 1888 | Originally banned due to only working with 8-bit characters. Now it is |
| 1889 | unnecessary because, in Chromium, it is the same type as `std::string_view`. |
Hong Xu | 5e492d3 | 2023-07-27 21:38:46 | [diff] [blame] | 1890 | Please use `std::string_view` instead. |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 1891 | *** |
| 1892 | |
| 1893 | ### Strings Library <sup>[banned]</sup> |
| 1894 | |
| 1895 | ```c++ |
| 1896 | absl::StrSplit |
| 1897 | absl::StrJoin |
| 1898 | absl::StrCat |
| 1899 | absl::StrAppend |
| 1900 | absl::Substitute |
| 1901 | absl::StrContains |
| 1902 | ``` |
| 1903 | |
| 1904 | **Description:** Classes and utility functions for manipulating and comparing |
| 1905 | strings. |
| 1906 | |
| 1907 | **Documentation:** |
| 1908 | [String Utilities](https://abseil.io/docs/cpp/guides/strings) |
| 1909 | |
| 1910 | **Notes:** |
| 1911 | *** promo |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 1912 | Overlaps with `base/strings`. We |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 1913 | [should re-evalute](https://bugs.chromium.org/p/chromium/issues/detail?id=1371966) |
| 1914 | when we've |
| 1915 | [migrated](https://bugs.chromium.org/p/chromium/issues/detail?id=691162) from |
| 1916 | `base::StringPiece` to `std::string_view`. |
| 1917 | *** |
| 1918 | |
| 1919 | ### Synchronization <sup>[banned]</sup> |
| 1920 | |
| 1921 | ```c++ |
| 1922 | absl::Mutex |
| 1923 | ``` |
| 1924 | |
| 1925 | **Description:** Primitives for managing tasks across different threads. |
| 1926 | |
| 1927 | **Documentation:** |
| 1928 | [Synchronization](https://abseil.io/docs/cpp/guides/synchronization) |
| 1929 | |
| 1930 | **Notes:** |
| 1931 | *** promo |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 1932 | Overlaps with `base/synchronization/`. We would love |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 1933 | [more testing](https://bugs.chromium.org/p/chromium/issues/detail?id=1371969) on |
| 1934 | whether there are compelling reasons to prefer base, absl, or std |
| 1935 | synchronization primitives; for now, use `base/synchronization/`. |
| 1936 | *** |
| 1937 | |
| 1938 | ### Time library <sup>[banned]</sup> |
| 1939 | |
| 1940 | ```c++ |
| 1941 | absl::Duration |
| 1942 | absl::Time |
| 1943 | absl::TimeZone |
| 1944 | absl::CivilDay |
| 1945 | ``` |
| 1946 | |
| 1947 | **Description:** Abstractions for holding time values, both in terms of |
| 1948 | absolute time and civil time. |
| 1949 | |
| 1950 | **Documentation:** [Time](https://abseil.io/docs/cpp/guides/time) |
| 1951 | |
| 1952 | **Notes:** |
| 1953 | *** promo |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 1954 | Overlaps with `base/time/`. |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 1955 | *** |
| 1956 | |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1957 | ## Abseil TBD Features {#absl-review} |
| 1958 | |
| 1959 | The following Abseil library features are not allowed in the Chromium codebase. |
| 1960 | See the top of this page on how to propose moving a feature from this list into |
| 1961 | the allowed or banned sections. |
| 1962 | |
Danil Chapovalov | a9f2703 | 2022-06-20 16:56:14 | [diff] [blame] | 1963 | ### AnyInvocable <sup>[tbd]</sup> |
| 1964 | |
| 1965 | ```c++ |
| 1966 | absl::AnyInvocable |
| 1967 | ``` |
| 1968 | |
| 1969 | **Description:** An equivalent of the C++23 std::move_only_function. |
| 1970 | |
| 1971 | **Documentation:** |
| 1972 | * [any_invocable.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/any_invocable.h) |
| 1973 | * [std::move_only_function](https://en.cppreference.com/w/cpp/utility/functional/move_only_function/move_only_function) |
| 1974 | |
| 1975 | **Notes:** |
| 1976 | *** promo |
| 1977 | Overlaps with `base::RepeatingCallback`, `base::OnceCallback`. |
| 1978 | *** |
| 1979 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 1980 | ### Containers <sup>[tbd]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1981 | |
| 1982 | ```c++ |
| 1983 | absl::flat_hash_map |
| 1984 | absl::flat_hash_set |
| 1985 | absl::node_hash_map |
| 1986 | absl::node_hash_set |
| 1987 | absl::btree_map |
| 1988 | absl::btree_set |
| 1989 | absl::btree_multimap |
| 1990 | absl::btree_multiset |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1991 | ``` |
| 1992 | |
| 1993 | **Description:** Alternatives to STL containers designed to be more efficient |
| 1994 | in the general case. |
| 1995 | |
| 1996 | **Documentation:** |
| 1997 | * [Containers](https://abseil.io/docs/cpp/guides/container) |
| 1998 | * [Hash](https://abseil.io/docs/cpp/guides/hash) |
| 1999 | |
| 2000 | **Notes:** |
| 2001 | *** promo |
| 2002 | Supplements `base/containers/`. |
Adam Rice | af2708e | 2023-07-20 14:53:16 | [diff] [blame] | 2003 | |
| 2004 | absl::InlinedVector is explicitly allowed, see the [discussion |
| 2005 | thread](https://groups.google.com/a/chromium.org/g/cxx/c/jTfqVfU-Ka0/m/caaal90NCgAJ). |
| 2006 | |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 2007 | *** |
| 2008 | |
Mirko Bonadei | de812cd | 2022-12-07 22:38:32 | [diff] [blame] | 2009 | ### CRC32C library <sup>[tbd]</sup> |
| 2010 | |
| 2011 | **Description:** API for computing CRC32C values as checksums for arbitrary |
| 2012 | sequences of bytes provided as a string buffer. |
| 2013 | |
| 2014 | **Documentation:** |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 2015 | [crc32.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/crc/crc32c.h) |
Mirko Bonadei | de812cd | 2022-12-07 22:38:32 | [diff] [blame] | 2016 | |
| 2017 | **Notes:** |
| 2018 | *** promo |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 2019 | Overlaps with `third_party/crc32c`. |
Mirko Bonadei | de812cd | 2022-12-07 22:38:32 | [diff] [blame] | 2020 | *** |
| 2021 | |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 2022 | ### Log macros and related classes <sup>[tbd]</sup> |
Danil Chapovalov | 6719fb1 | 2022-08-31 13:52:49 | [diff] [blame] | 2023 | |
| 2024 | ```c++ |
| 2025 | LOG(INFO) << message; |
| 2026 | CHECK(condition); |
| 2027 | absl::AddLogSink(&custom_sink_to_capture_absl_logs); |
| 2028 | ``` |
| 2029 | |
| 2030 | **Description:** Macros and related classes to perform debug loggings |
| 2031 | |
| 2032 | **Documentation:** |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 2033 | [log.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/log/log.h) |
| 2034 | [check.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/log/check.h) |
Danil Chapovalov | 6719fb1 | 2022-08-31 13:52:49 | [diff] [blame] | 2035 | |
| 2036 | **Notes:** |
| 2037 | *** promo |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 2038 | Overlaps with `base/logging.h`. |
Danil Chapovalov | 6719fb1 | 2022-08-31 13:52:49 | [diff] [blame] | 2039 | *** |
Danil Chapovalov | 79a4094 | 2023-06-21 18:35:30 | [diff] [blame] | 2040 | |
Mirko Bonadei | dc928a16 | 2023-11-20 08:24:23 | [diff] [blame] | 2041 | ### NoDestructor <sup>[tbd]</sup> |
| 2042 | |
| 2043 | ```c++ |
| 2044 | // Global or namespace scope. |
| 2045 | ABSL_CONST_INIT absl::NoDestructor<MyRegistry> reg{"foo", "bar", 8008}; |
| 2046 | |
| 2047 | // Function scope. |
| 2048 | const std::string& MyString() { |
| 2049 | static const absl::NoDestructor<std::string> x("foo"); |
| 2050 | return *x; |
| 2051 | } |
| 2052 | ``` |
| 2053 | |
| 2054 | **Description:** `absl::NoDestructor<T>` is a wrapper around an object of |
| 2055 | type T that behaves as an object of type T but never calls T's destructor. |
| 2056 | |
| 2057 | **Documentation:** |
| 2058 | [no_destructor.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/base/no_desctructor.h) |
| 2059 | |
| 2060 | **Notes:** |
| 2061 | *** promo |
| 2062 | Overlaps with `base::NoDestructor`. |
| 2063 | *** |
| 2064 | |
Danil Chapovalov | 79a4094 | 2023-06-21 18:35:30 | [diff] [blame] | 2065 | ### Nullability annotations <sup>[tbd]</sup> |
| 2066 | |
| 2067 | ```c++ |
| 2068 | void PaySalary(absl::NotNull<Employee *> employee) { |
| 2069 | pay(*employee); // OK to dereference |
| 2070 | } |
| 2071 | ``` |
| 2072 | |
| 2073 | **Description:** Annotations to more clearly specify contracts |
| 2074 | |
| 2075 | **Documentation:** |
| 2076 | [nullability.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/base/nullability.h) |
| 2077 | |
| 2078 | **Notes:** |
| 2079 | *** promo |
| 2080 | These nullability annotations are primarily a human readable signal about the |
| 2081 | intended contract of the pointer. They are not *types* and do not currently |
| 2082 | provide any correctness guarantees. |
| 2083 | *** |
Danil Chapovalov | 0e8ed13 | 2023-09-28 08:05:39 | [diff] [blame] | 2084 | |
| 2085 | ### Overload <sup>[tbd]</sup> |
| 2086 | |
| 2087 | ```c++ |
| 2088 | std::variant<int, std::string, double> v(int{1}); |
| 2089 | assert(std::visit(absl::Overload( |
| 2090 | [](int) -> absl::string_view { return "int"; }, |
| 2091 | [](const std::string&) -> absl::string_view { |
| 2092 | return "string"; |
| 2093 | }, |
| 2094 | [](double) -> absl::string_view { return "double"; }), |
| 2095 | v) == "int"); |
| 2096 | ``` |
| 2097 | |
| 2098 | **Description:** Returns a functor that provides overloads based on the functors passed to it |
| 2099 | |
| 2100 | **Documentation:** |
Adam Rice | bf82f34 | 2023-09-29 15:07:31 | [diff] [blame] | 2101 | [overload.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/overload.h) |
Danil Chapovalov | 0e8ed13 | 2023-09-28 08:05:39 | [diff] [blame] | 2102 | |
| 2103 | **Notes:** |
| 2104 | *** promo |
Peter Kasting | 72d110f1 | 2024-02-06 19:45:26 | [diff] [blame] | 2105 | Overlaps with `base::Overloaded`. |
Danil Chapovalov | 0e8ed13 | 2023-09-28 08:05:39 | [diff] [blame] | 2106 | *** |