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