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