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