Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1 | # Modern C++ use in Chromium |
| 2 | |
| 3 | _This document is part of the more general |
| 4 | [Chromium C++ style guide](https://chromium.googlesource.com/chromium/src/+/main/styleguide/c++/c++.md). |
| 5 | It summarizes the supported state of new and updated language and library |
| 6 | features in recent C++ standards and the [Abseil](https://abseil.io/about/) |
| 7 | library. This guide applies to both Chromium and its subprojects, though |
| 8 | subprojects can choose to be more restrictive if necessary for toolchain |
| 9 | support._ |
| 10 | |
| 11 | The C++ language has in recent years received an updated standard every three |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 12 | years (C++11, C++14, etc.). For various reasons, Chromium does not immediately |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 13 | allow new features on the publication of such a standard. Instead, once |
Hong Xu | 68ba51a0 | 2022-04-27 22:21:35 | [diff] [blame] | 14 | Chromium supports the toolchain to a certain extent (e.g., build support is |
| 15 | ready), a standard is declared "_initially supported_", with new |
| 16 | language/library features banned pending discussion but not yet allowed. |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 17 | |
| 18 | You can propose changing the status of a feature by sending an email to |
| 19 | [[email protected]](https://groups.google.com/a/chromium.org/forum/#!forum/cxx). |
| 20 | Include a short blurb on what the feature is and why you think it should or |
| 21 | should not be allowed, along with links to any relevant previous discussion. If |
| 22 | the list arrives at some consensus, send a codereview to change this file |
| 23 | accordingly, linking to your discussion thread. |
| 24 | |
| 25 | If an item remains on the TBD list two years after initial support is added, |
| 26 | style arbiters should explicitly move it to an appropriate allowlist or |
| 27 | blocklist, allowing it if there are no obvious reasons to ban. |
| 28 | |
| 29 | The current status of existing standards and Abseil features is: |
| 30 | |
| 31 | * **C++11:** _Default allowed; see banned features below_ |
| 32 | * **C++14:** _Default allowed; see banned features below_ |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 33 | * **C++17:** Initially supported December 23, 2021; see allowed/banned/TBD |
| 34 | features below |
| 35 | * **C++20:** _Not yet supported in Chromium_ |
| 36 | * **C++23:** _Not yet standardized_ |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame^] | 37 | * **Abseil:** _Default allowed; see banned/TBD |
| 38 | features below_ |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 39 | * absl::Cleanup: Initially supported February 4, 2021 |
Danil Chapovalov | a9f2703 | 2022-06-20 16:56:14 | [diff] [blame] | 40 | * absl::AnyInvocable: Initially supported June 20, 2022 |
Danil Chapovalov | 6719fb1 | 2022-08-31 13:52:49 | [diff] [blame] | 41 | * Log library: Initially supported Aug 31, 2022 |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 42 | |
| 43 | [TOC] |
| 44 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 45 | ## C++11 Banned Language Features {#core-blocklist-11} |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 46 | |
| 47 | The following C++11 language features are not allowed in the Chromium codebase. |
| 48 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 49 | ### Inline Namespaces <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 50 | |
| 51 | ```c++ |
| 52 | inline namespace foo { ... } |
| 53 | ``` |
| 54 | |
| 55 | **Description:** Allows better versioning of namespaces. |
| 56 | |
| 57 | **Documentation:** |
| 58 | [Inline namespaces](https://en.cppreference.com/w/cpp/language/namespace#Inline_namespaces) |
| 59 | |
| 60 | **Notes:** |
| 61 | *** promo |
| 62 | Banned in the |
| 63 | [Google Style Guide](https://google.github.io/styleguide/cppguide.html#Namespaces). |
| 64 | Unclear how it will work with components. |
| 65 | *** |
| 66 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 67 | ### long long Type <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 68 | |
| 69 | ```c++ |
| 70 | long long var = value; |
| 71 | ``` |
| 72 | |
| 73 | **Description:** An integer of at least 64 bits. |
| 74 | |
| 75 | **Documentation:** |
| 76 | [Fundamental types](https://en.cppreference.com/w/cpp/language/types) |
| 77 | |
| 78 | **Notes:** |
| 79 | *** promo |
| 80 | Use a stdint.h type if you need a 64-bit number. |
| 81 | [Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/chromium-dev/RxugZ-pIDxk) |
| 82 | *** |
| 83 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 84 | ### User-Defined Literals <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 85 | |
| 86 | ```c++ |
| 87 | type var = literal_value_type; |
| 88 | ``` |
| 89 | |
| 90 | **Description:** Allows user-defined literal expressions. |
| 91 | |
| 92 | **Documentation:** |
| 93 | [User-defined literals](https://en.cppreference.com/w/cpp/language/user_literal) |
| 94 | |
| 95 | **Notes:** |
| 96 | *** promo |
| 97 | Banned in the |
| 98 | [Google Style Guide](https://google.github.io/styleguide/cppguide.html#Operator_Overloading). |
| 99 | *** |
| 100 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 101 | ### thread_local Storage Class <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 102 | |
| 103 | ```c++ |
| 104 | thread_local int foo = 1; |
| 105 | ``` |
| 106 | |
| 107 | **Description:** Puts variables into thread local storage. |
| 108 | |
| 109 | **Documentation:** |
| 110 | [Storage duration](https://en.cppreference.com/w/cpp/language/storage_duration) |
| 111 | |
| 112 | **Notes:** |
| 113 | *** promo |
| 114 | Some surprising effects on Mac |
| 115 | ([discussion](https://groups.google.com/a/chromium.org/forum/#!topic/chromium-dev/2msN8k3Xzgs), |
| 116 | [fork](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/h7O5BdtWCZw)). |
| 117 | Use `base::SequenceLocalStorageSlot` for sequence support, and |
| 118 | `base::ThreadLocal`/`base::ThreadLocalStorage` otherwise. |
| 119 | *** |
| 120 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 121 | ## C++11 Banned Library Features {#library-blocklist-11} |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 122 | |
| 123 | The following C++11 library features are not allowed in the Chromium codebase. |
| 124 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 125 | ### Bind Operations <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 126 | |
| 127 | ```c++ |
| 128 | std::bind(function, args, ...) |
| 129 | ``` |
| 130 | |
| 131 | **Description:** Declares a function object bound to certain arguments |
| 132 | |
| 133 | **Documentation:** |
| 134 | [std::bind](https://en.cppreference.com/w/cpp/utility/functional/bind) |
| 135 | |
| 136 | **Notes:** |
| 137 | *** promo |
| 138 | Use `base::Bind` instead. Compared to `std::bind`, `base::Bind` helps prevent |
| 139 | lifetime issues by preventing binding of capturing lambdas and by forcing |
| 140 | callers to declare raw pointers as `Unretained`. |
| 141 | [Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/SoEj7oIDNuA) |
| 142 | *** |
| 143 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 144 | ### C Floating-Point Environment <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 145 | |
| 146 | ```c++ |
| 147 | #include <cfenv> |
| 148 | #include <fenv.h> |
| 149 | ``` |
| 150 | |
| 151 | **Description:** Provides floating point status flags and control modes for |
| 152 | C-compatible code |
| 153 | |
| 154 | **Documentation:** |
| 155 | [Standard library header "cfenv"](https://en.cppreference.com/w/cpp/header/cfenv) |
| 156 | |
| 157 | **Notes:** |
| 158 | *** promo |
| 159 | Banned by the |
| 160 | [Google Style Guide](https://google.github.io/styleguide/cppguide.html#C++11) |
| 161 | due to concerns about compiler support. |
| 162 | *** |
| 163 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 164 | ### Date and time utilities <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 165 | |
| 166 | ```c++ |
| 167 | #include <chrono> |
| 168 | ``` |
| 169 | |
| 170 | **Description:** A standard date and time library |
| 171 | |
| 172 | **Documentation:** |
| 173 | [Date and time utilities](https://en.cppreference.com/w/cpp/chrono) |
| 174 | |
| 175 | **Notes:** |
| 176 | *** promo |
| 177 | Overlaps with `Time` APIs in `base/`. Keep using the `base/` classes. |
| 178 | *** |
| 179 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 180 | ### Exceptions <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 181 | |
| 182 | ```c++ |
| 183 | #include <exception> |
| 184 | ``` |
| 185 | |
| 186 | **Description:** Enhancements to exception throwing and handling |
| 187 | |
| 188 | **Documentation:** |
| 189 | [Standard library header "exception"](https://en.cppreference.com/w/cpp/header/exception) |
| 190 | |
| 191 | **Notes:** |
| 192 | *** promo |
| 193 | Exceptions are banned by the |
| 194 | [Google Style Guide](https://google.github.io/styleguide/cppguide.html#Exceptions) |
| 195 | and disabled in Chromium compiles. However, the `noexcept` specifier is |
| 196 | explicitly allowed. |
| 197 | [Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/chromium-dev/8i4tMqNpHhg) |
| 198 | *** |
| 199 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 200 | ### Function Objects <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 201 | |
| 202 | ```c++ |
| 203 | std::function |
| 204 | ``` |
| 205 | |
| 206 | **Description:** Wraps a standard polymorphic function |
| 207 | |
| 208 | **Documentation:** |
| 209 | [std::function](https://en.cppreference.com/w/cpp/utility/functional/function) |
| 210 | |
| 211 | **Notes:** |
| 212 | *** promo |
| 213 | Use `base::{Once,Repeating}Callback` instead. Compared to `std::function`, |
| 214 | `base::{Once,Repeating}Callback` directly supports Chromium's refcounting |
| 215 | classes and weak pointers and deals with additional thread safety concerns. |
| 216 | [Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/SoEj7oIDNuA) |
| 217 | *** |
| 218 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 219 | ### Random Number Engines <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 220 | |
| 221 | *** aside |
| 222 | The random number engines defined in `<random>` (see separate item for random |
| 223 | number distributions), e.g.: `linear_congruential_engine`, |
| 224 | `mersenne_twister_engine`, `minstd_rand0`, `mt19937`, `ranlinux48`, |
| 225 | `random_device` |
| 226 | *** |
| 227 | |
| 228 | **Description:** Random number generation algorithms and utilities. |
| 229 | |
| 230 | **Documentation:** |
| 231 | [Pseudo-random number generation](https://en.cppreference.com/w/cpp/numeric/random) |
| 232 | |
| 233 | **Notes:** |
| 234 | *** promo |
| 235 | Do not use any random number engines from `<random>`. Instead, use |
| 236 | `base::RandomBitGenerator`. |
| 237 | [Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/16Xmw05C-Y0) |
| 238 | *** |
| 239 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 240 | ### Ratio Template Class <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 241 | |
| 242 | ```c++ |
| 243 | std::ratio<numerator, denominator> |
| 244 | ``` |
| 245 | |
| 246 | **Description:** Provides compile-time rational numbers |
| 247 | |
| 248 | **Documentation:** |
| 249 | [std::ratio](https://en.cppreference.com/w/cpp/numeric/ratio/ratio) |
| 250 | |
| 251 | **Notes:** |
| 252 | *** promo |
| 253 | Banned by the |
| 254 | [Google Style Guide](https://google.github.io/styleguide/cppguide.html#C++11) |
| 255 | due to concerns that this is tied to a more template-heavy interface style. |
| 256 | *** |
| 257 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 258 | ### Regular Expressions <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 259 | |
| 260 | ```c++ |
| 261 | #include <regex> |
| 262 | ``` |
| 263 | |
| 264 | **Description:** A standard regular expressions library |
| 265 | |
| 266 | **Documentation:** |
| 267 | [Regular expressions library](https://en.cppreference.com/w/cpp/regex) |
| 268 | |
| 269 | **Notes:** |
| 270 | *** promo |
| 271 | Overlaps with many regular expression libraries in Chromium. When in doubt, use |
| 272 | `re2`. |
| 273 | *** |
| 274 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 275 | ### Shared Pointers <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 276 | |
| 277 | ```c++ |
| 278 | std::shared_ptr |
| 279 | ``` |
| 280 | |
| 281 | **Description:** Allows shared ownership of a pointer through reference counts |
| 282 | |
| 283 | **Documentation:** |
| 284 | [std::shared_ptr](https://en.cppreference.com/w/cpp/memory/shared_ptr) |
| 285 | |
| 286 | **Notes:** |
| 287 | *** promo |
| 288 | Needs a lot more evaluation for Chromium, and there isn't enough of a push for |
| 289 | this feature. |
| 290 | |
| 291 | * [Google Style Guide](https://google.github.io/styleguide/cppguide.html#Ownership_and_Smart_Pointers). |
| 292 | * [Discussion Thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/aT2wsBLKvzI) |
| 293 | *** |
| 294 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 295 | ### String-Number Conversion Functions <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 296 | |
| 297 | ```c++ |
| 298 | std::stoi() |
| 299 | std::stol() |
| 300 | std::stoul() |
| 301 | std::stoll |
| 302 | std::stoull() |
| 303 | std::stof() |
| 304 | std::stod() |
| 305 | std::stold() |
| 306 | std::to_string() |
| 307 | ``` |
| 308 | |
| 309 | **Description:** Converts strings to/from numbers |
| 310 | |
| 311 | **Documentation:** |
| 312 | * [std::stoi, std::stol, std::stoll](https://en.cppreference.com/w/cpp/string/basic_string/stol), |
| 313 | * [std::stoul, std::stoull](https://en.cppreference.com/w/cpp/string/basic_string/stoul), |
| 314 | * [std::stof, std::stod, std::stold](https://en.cppreference.com/w/cpp/string/basic_string/stof), |
| 315 | * [std::to_string](https://en.cppreference.com/w/cpp/string/basic_string/to_string) |
| 316 | |
| 317 | **Notes:** |
| 318 | *** promo |
| 319 | The string-to-number conversions rely on exceptions to communicate failure, |
| 320 | while the number-to-string conversions have performance concerns and depend on |
| 321 | the locale. Use the routines in `base/strings/string_number_conversions.h` |
| 322 | instead. |
| 323 | *** |
| 324 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 325 | ### Thread Library <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 326 | |
| 327 | *** aside |
| 328 | `<thread>` and related headers, including `<future>`, `<mutex>`, |
| 329 | `<condition_variable>` |
| 330 | *** |
| 331 | |
| 332 | **Description:** Provides a standard multithreading library using `std::thread` |
| 333 | and associates |
| 334 | |
| 335 | **Documentation:** |
| 336 | [Thread support library](https://en.cppreference.com/w/cpp/thread) |
| 337 | |
| 338 | **Notes:** |
| 339 | *** promo |
| 340 | Overlaps with many classes in `base/`. Keep using the `base/` classes for now. |
| 341 | `base::Thread` is tightly coupled to `MessageLoop` which would make it hard to |
| 342 | replace. We should investigate using standard mutexes, or unique_lock, etc. to |
| 343 | replace our locking/synchronization classes. |
| 344 | *** |
| 345 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 346 | ### Weak Pointers <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 347 | |
| 348 | ```c++ |
| 349 | std::weak_ptr |
| 350 | ``` |
| 351 | |
| 352 | **Description:** Allows a weak reference to a `std::shared_ptr` |
| 353 | |
| 354 | **Documentation:** |
| 355 | [std::weak_ptr](https://en.cppreference.com/w/cpp/memory/weak_ptr) |
| 356 | |
| 357 | **Notes:** |
| 358 | *** promo |
| 359 | Banned because `std::shared_ptr` is banned. Use `base::WeakPtr` instead. |
| 360 | *** |
| 361 | |
| 362 | ## C++14 Banned Library Features {#library-blocklist-14} |
| 363 | |
| 364 | The following C++14 library features are not allowed in the Chromium codebase. |
| 365 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 366 | ### std::chrono literals <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 367 | |
| 368 | ```c++ |
| 369 | using namespace std::chrono_literals; |
| 370 | auto timeout = 30s; |
| 371 | ``` |
| 372 | |
| 373 | **Description:** Allows `std::chrono` types to be more easily constructed. |
| 374 | |
| 375 | **Documentation:** |
| 376 | [std::literals::chrono_literals::operator""s](https://en.cppreference.com/w/cpp/chrono/operator%22%22s) |
| 377 | |
| 378 | **Notes:** |
| 379 | *** promo |
| 380 | Banned because `<chrono>` is banned. |
| 381 | *** |
| 382 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 383 | ## C++17 Allowed Language Features {#core-allowlist-17} |
| 384 | |
| 385 | The following C++17 language features are allowed in the Chromium codebase. |
| 386 | |
Avi Drissman | efca412 | 2022-01-05 23:59:36 | [diff] [blame] | 387 | ### Nested namespaces <sup>[allowed]</sup> |
| 388 | |
| 389 | ```c++ |
| 390 | namespace A::B::C { ... |
| 391 | ``` |
| 392 | |
| 393 | **Description:** Using the namespace resolution operator to create nested |
| 394 | namespace definitions. |
| 395 | |
| 396 | **Documentation:** |
| 397 | [Namespaces](https://en.cppreference.com/w/cpp/language/namespace) |
| 398 | |
| 399 | **Notes:** |
| 400 | *** promo |
| 401 | [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/gLdR3apDSmg/) |
| 402 | *** |
| 403 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 404 | ### Template argument deduction for class templates <sup>[allowed]</sup> |
| 405 | |
| 406 | ```c++ |
| 407 | template <typename T> |
| 408 | struct MyContainer { |
| 409 | MyContainer(T val) : val{val} {} |
| 410 | // ... |
| 411 | }; |
| 412 | MyContainer c1(1); // Type deduced to be `int`. |
| 413 | ``` |
| 414 | |
| 415 | **Description:** Automatic template argument deduction much like how it's done |
| 416 | for functions, but now including class constructors. |
| 417 | |
| 418 | **Documentation:** |
| 419 | [Class template argument deduction](https://en.cppreference.com/w/cpp/language/class_template_argument_deduction) |
| 420 | |
| 421 | **Notes:** |
| 422 | *** promo |
| 423 | Usage is governed by the |
| 424 | [Google Style Guide](https://google.github.io/styleguide/cppguide.html#CTAD). |
| 425 | *** |
| 426 | |
Daniel Cheng | 7919c8d | 2022-07-08 01:36:22 | [diff] [blame] | 427 | ### Fold expressions <sup>[allowed]</sup> |
| 428 | |
| 429 | ```c++ |
| 430 | template <typename... Args> |
| 431 | auto sum(Args... args) { |
| 432 | return (... + args); |
| 433 | } |
| 434 | ``` |
| 435 | |
| 436 | **Description:** A fold expression performs a fold of a template parameter pack |
| 437 | over a binary operator. |
| 438 | |
| 439 | **Documentation:** |
| 440 | [Fold expression](https://en.cppreference.com/w/cpp/language/fold) |
| 441 | |
| 442 | **Notes:** |
| 443 | *** promo |
| 444 | [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/4DTm3idXz0w/m/g_JjOh0wAgAJ) |
| 445 | *** |
| 446 | |
Andrew Rayskiy | 6ce944d | 2022-01-04 18:13:33 | [diff] [blame] | 447 | ### Selection statements with initializer <sup>[allowed]</sup> |
| 448 | |
| 449 | ```c++ |
| 450 | if (int a = Func(); a < 3) { ... |
| 451 | switch (int a = Func(); a) { ... |
| 452 | ``` |
| 453 | |
| 454 | **Description:** New versions of the if and switch statements which simplify |
| 455 | common code patterns and help users keep scopes tight. |
| 456 | |
| 457 | **Documentation:** |
| 458 | [if statement](https://en.cppreference.com/w/cpp/language/if), |
| 459 | [switch statement](https://en.cppreference.com/w/cpp/language/switch) |
| 460 | |
| 461 | **Notes:** |
| 462 | *** promo |
| 463 | [@cxx discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/4GP43nftePE) |
| 464 | *** |
| 465 | |
Roland Bock | f5242fb | 2022-01-05 17:54:38 | [diff] [blame] | 466 | ### fallthrough attribute <sup>[allowed]</sup> |
Roland Bock | 767858e | 2022-01-04 20:25:21 | [diff] [blame] | 467 | |
| 468 | ```c++ |
| 469 | case 1: |
| 470 | DoSomething(); |
| 471 | [[fallthrough]]; |
| 472 | case 2: |
| 473 | break; |
| 474 | ``` |
| 475 | |
| 476 | **Description:** |
| 477 | The `[[fallthrough]]` attribute can be used in switch statements to indicate |
| 478 | when intentionally falling through to the next case. |
| 479 | |
| 480 | **Documentation:** |
| 481 | [C++ attribute: fallthrough](https://en.cppreference.com/w/cpp/language/attributes/fallthrough) |
| 482 | |
| 483 | **Notes:** |
| 484 | *** promo |
| 485 | See [discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/JrvyFd243QI). |
| 486 | |
| 487 | See [migration task](https://bugs.chromium.org/p/chromium/issues/detail?id=1283907). |
| 488 | *** |
| 489 | |
Jayson Adams | 62f16c0 | 2022-01-11 15:03:35 | [diff] [blame] | 490 | ### constexpr if <sup>[allowed]</sup> |
| 491 | |
| 492 | ```c++ |
| 493 | if constexpr (cond) { ... |
| 494 | ``` |
| 495 | |
| 496 | **Description:** Write code that is instantiated depending on a compile-time |
| 497 | condition. |
| 498 | |
| 499 | **Documentation:** |
| 500 | [if statement](https://en.cppreference.com/w/cpp/language/if) |
| 501 | |
| 502 | **Notes:** |
| 503 | *** promo |
| 504 | See [discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/op2ePZnjP0w). |
Avi Drissman | c2558ba | 2022-01-13 19:49:01 | [diff] [blame] | 505 | *** |
Victor Costan | dcddd26 | 2022-01-12 19:20:39 | [diff] [blame] | 506 | |
Daniel Cheng | 4455c984 | 2022-01-13 23:26:37 | [diff] [blame] | 507 | ### nodiscard attribute <sup>[allowed]</sup> |
| 508 | |
| 509 | ```c++ |
| 510 | struct [[nodiscard]] ErrorOrValue; |
| 511 | [[nodiscard]] bool DoSomething(); |
| 512 | ``` |
| 513 | |
| 514 | **Description:** |
| 515 | The `[[nodiscard]]` attribute can be used to indicate that |
| 516 | |
| 517 | - the return value of a function should not be ignored |
| 518 | - values of annotated classes/structs/enums returned from functions should not |
| 519 | be ignored |
| 520 | |
| 521 | **Documentation:** |
| 522 | [C++ attribute: nodiscard](https://en.cppreference.com/w/cpp/language/attributes/nodiscard) |
| 523 | |
| 524 | **Notes:** |
| 525 | *** promo |
Daniel Cheng | 0bef487 | 2022-02-15 22:21:33 | [diff] [blame] | 526 | This replaces the previous `WARN_UNUSED_RESULT` macro, which was a wrapper |
| 527 | around the compiler-specific `__attribute__((warn_unused_result))`. |
| 528 | |
Daniel Cheng | 4455c984 | 2022-01-13 23:26:37 | [diff] [blame] | 529 | [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/nH7Ar8pZ1Dw/m/c90vGChvAAAJ) |
| 530 | *** |
| 531 | |
Avi Drissman | 7ba88aa6 | 2022-01-13 14:15:48 | [diff] [blame] | 532 | ### maybe_unused attribute <sup>[allowed]</sup> |
| 533 | |
| 534 | ```c++ |
| 535 | struct [[maybe_unused]] MyUnusedThing; |
| 536 | [[maybe_unused]] int x; |
| 537 | ``` |
| 538 | |
| 539 | **Description:** |
| 540 | The `[[maybe_unused]]` attribute can be used to indicate that individual |
| 541 | variables, functions, or fields of a class/struct/enum can be left unused. |
| 542 | |
| 543 | **Documentation:** |
| 544 | [C++ attribute: maybe_unused](https://en.cppreference.com/w/cpp/language/attributes/maybe_unused) |
| 545 | |
| 546 | **Notes:** |
| 547 | *** promo |
| 548 | See [discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/jPLfU5eRg8M/). |
| 549 | *** |
| 550 | |
Victor Costan | dcddd26 | 2022-01-12 19:20:39 | [diff] [blame] | 551 | ### Structured bindings <sup>[allowed]</sup> |
| 552 | |
| 553 | ```c++ |
| 554 | const auto [x, y] = FuncReturningStdPair(); |
| 555 | ``` |
| 556 | |
| 557 | **Description:** Allows writing `auto [x, y, z] = expr;` where the type of |
| 558 | `expr` is a tuple-like object, whose elements are bound to the variables `x`, |
| 559 | `y`, and `z` (which this construct declares). Tuple-like objects include |
| 560 | `std::tuple`, `std::pair`, `std::array`, and aggregate structures. |
| 561 | |
| 562 | **Documentation:** |
| 563 | [Structured binding declaration](https://en.cppreference.com/w/cpp/language/structured_binding) |
| 564 | [Explanation of structured binding types](https://jguegant.github.io/blogs/tech/structured-bindings.html) |
| 565 | |
| 566 | **Notes:** |
| 567 | *** promo |
| 568 | In C++17, structured bindings don't work with lambda captures. |
| 569 | [C++20 will allow capturing structured bindings by value](https://wg21.link/p1091r3). |
| 570 | |
| 571 | This feature forces omitting type names. Its use should follow |
| 572 | [the guidance around `auto` in Google C++ Style guide](https://google.github.io/styleguide/cppguide.html#Type_deduction). |
| 573 | |
| 574 | See [discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ExfSorNLNf4). |
Jayson Adams | 62f16c0 | 2022-01-11 15:03:35 | [diff] [blame] | 575 | *** |
| 576 | |
Avi Drissman | 37b7d51 | 2022-04-01 22:01:40 | [diff] [blame] | 577 | ### Inline variables <sup>[allowed]</sup> |
Bruce Dawson | bb51b003 | 2022-01-14 19:24:22 | [diff] [blame] | 578 | |
| 579 | ```c++ |
| 580 | struct S { |
| 581 | static constexpr int kZero = 0; // constexpr implies inline here. |
| 582 | }; |
| 583 | |
Victor Vianna | 7f2773f | 2022-02-08 14:44:56 | [diff] [blame] | 584 | inline constexpr int kOne = 1; // Explicit inline needed here. |
Bruce Dawson | bb51b003 | 2022-01-14 19:24:22 | [diff] [blame] | 585 | ``` |
| 586 | |
| 587 | **Description:** The `inline` specifier can be applied to variables as well as |
| 588 | to functions. A variable declared inline has the same semantics as a function |
| 589 | declared inline. It can also be used to declare and define a static member |
| 590 | variable, such that it does not need to be initialized in the source file. |
| 591 | |
| 592 | **Documentation:** |
| 593 | [inline specifier](https://en.cppreference.com/w/cpp/language/inline) |
| 594 | |
| 595 | **Notes:** |
| 596 | *** promo |
| 597 | Inline variables in anonymous namespaces in header files will still get one copy |
| 598 | per translation unit, so they must be outside of an anonymous namespace to be |
| 599 | effective. |
| 600 | |
| 601 | Mutable inline variables and taking the address of inline variables are banned |
| 602 | since these will break the component build. |
| 603 | |
| 604 | See [discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/hmyGFD80ocE/m/O4AXC93vAQAJ). |
| 605 | *** |
| 606 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 607 | ## C++17 Allowed Library Features {#library-allowlist-17} |
| 608 | |
| 609 | The following C++17 language features are allowed in the Chromium codebase. |
| 610 | |
| 611 | ### Allocation functions with explicit alignment <sup>[allowed]</sup> |
| 612 | |
| 613 | ```c++ |
| 614 | class alignas(32) Vec3d { |
| 615 | double x, y, z; |
| 616 | }; |
| 617 | auto p_vec = new Vec3d[10]; // 32-byte aligned in C++17, maybe not previously |
| 618 | ``` |
| 619 | |
| 620 | **Description:** Performs heap allocation of objects whose alignment |
| 621 | requirements exceed `__STDCPP_DEFAULT_NEW_ALIGNMENT__`. |
| 622 | |
| 623 | **Documentation:** |
| 624 | [operator new](https://en.cppreference.com/w/cpp/memory/new/operator_new) |
| 625 | |
| 626 | **Notes:** |
| 627 | *** promo |
| 628 | None |
| 629 | *** |
| 630 | |
Daniel Cheng | 6da95c7 | 2022-02-24 19:54:33 | [diff] [blame] | 631 | ### Type trait variable templates <sup>[allowed]</sup> |
Daniel Cheng | 6f510fa | 2022-01-12 19:36:03 | [diff] [blame] | 632 | |
| 633 | ```c++ |
| 634 | bool b = std::is_same_v<int, std::int32_t>; |
| 635 | ``` |
| 636 | |
| 637 | **Description:** Syntactic sugar to provide convenient access to `::value` |
| 638 | members by simply adding `_v`. |
| 639 | |
| 640 | **Documentation:** |
| 641 | [Type support](https://en.cppreference.com/w/cpp/types) |
| 642 | |
| 643 | **Notes:** |
| 644 | *** promo |
| 645 | [Discussion thread](Non://groups.google.com/a/chromium.org/g/cxx/c/KEa-0AOGRNY/m/IV_S3_pvAAAJ) |
| 646 | *** |
| 647 | |
Daniel Cheng | a371b4c | 2022-01-14 18:24:27 | [diff] [blame] | 648 | ### std::map::try_emplace <sup>[allowed]</sup> |
| 649 | |
| 650 | ```c++ |
| 651 | std::map<std::string, std::string> m; |
| 652 | m.try_emplace("c", 10, 'c'); |
| 653 | m.try_emplace("c", "Won't be inserted"); |
| 654 | ``` |
| 655 | |
| 656 | **Description:** Like `emplace`, but does not move from rvalue arguments if the |
| 657 | insertion does not happen. |
| 658 | |
| 659 | **Documentation:** |
| 660 | [std::map::try_emplace](https://en.cppreference.com/w/cpp/container/map/try_emplace), |
| 661 | |
| 662 | **Notes:** |
| 663 | *** promo |
| 664 | [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/Uv2tUfIwUfQ/m/ffMxCk9uAAAJ) |
| 665 | *** |
| 666 | |
| 667 | ### std::map::insert_or_assign <sup>[allowed]</sup> |
| 668 | |
| 669 | ```c++ |
| 670 | std::map<std::string, std::string> m; |
| 671 | m.insert_or_assign("c", "cherry"); |
| 672 | m.insert_or_assign("c", "clementine"); |
| 673 | ``` |
| 674 | |
| 675 | **Description:** Like `operator[]`, but returns more information and does not |
| 676 | require default-constructibility of the mapped type. |
| 677 | |
| 678 | **Documentation:** |
| 679 | [std::map::insert_or_assign](https://en.cppreference.com/w/cpp/container/map/insert_or_assign) |
| 680 | |
| 681 | **Notes:** |
| 682 | *** promo |
| 683 | [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/Uv2tUfIwUfQ/m/ffMxCk9uAAAJ) |
| 684 | *** |
| 685 | |
Peter Kasting | d51bed7 | 2022-09-09 19:40:00 | [diff] [blame] | 686 | ### std::apply <sup>[allowed]</sup> |
| 687 | |
| 688 | ```c++ |
| 689 | static_assert(std::apply(std::plus<>(), std::make_tuple(1, 2)) == 3); |
| 690 | ``` |
| 691 | |
| 692 | **Description:** Invokes a `Callable` object with a tuple of arguments. |
| 693 | |
| 694 | **Documentation:** |
| 695 | [std::apply](https://en.cppreference.com/w/cpp/utility/apply) |
| 696 | |
| 697 | **Notes:** |
| 698 | *** promo |
| 699 | [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/cNZm_g39fyM) |
| 700 | *** |
| 701 | |
Lei Zhang | aaf6fc2 | 2022-10-06 23:12:42 | [diff] [blame] | 702 | ### std::as_const <sup>[allowed]</sup> |
| 703 | |
| 704 | ```c++ |
| 705 | auto&& const_ref = std::as_const(mutable_obj); |
| 706 | ``` |
| 707 | |
| 708 | **Description:** Forms reference to const T. |
| 709 | |
| 710 | **Documentation:** |
| 711 | [std::as_const](https://en.cppreference.com/w/cpp/utility/as_const) |
| 712 | |
| 713 | **Notes:** |
| 714 | *** promo |
| 715 | [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/5Uo4iJK6Mf4) |
| 716 | *** |
| 717 | |
Daniel Cheng | 4ddc920 | 2022-02-24 20:19:26 | [diff] [blame] | 718 | ### Non-member std::size/std::empty/std::data <sup>[allowed]</sup> |
| 719 | |
| 720 | ```c++ |
| 721 | char buffer[260]; |
| 722 | memcpy(std::data(buffer), source_str.data(), std::size(buffer)); |
| 723 | |
| 724 | if (!std::empty(container)) { ... } |
| 725 | ``` |
| 726 | |
| 727 | **Description:** Non-member versions of what are often member functions on STL |
| 728 | containers. Primarily useful when: |
| 729 | - using `std::size()` as a replacement for the old `arraysize()` macro. |
| 730 | - writing code that needs to generically operate across things like |
| 731 | `std::vector` and `std::list` (which provide `size()`, `empty()`, and `data() |
| 732 | member functions), `std::array` and `std::initialize_list` (which only provide |
| 733 | a subset of the aforementioned member functions), and regular arrays (which |
| 734 | have no member functions at all). |
| 735 | |
| 736 | **Documentation:** |
| 737 | [std::size](https://en.cppreference.com/w/cpp/iterator/size), |
| 738 | [std::empty](https://en.cppreference.com/w/cpp/iterator/empty), |
| 739 | [std::data](https://en.cppreference.com/w/cpp/iterator/data) |
| 740 | |
| 741 | **Notes:** |
| 742 | *** promo |
| 743 | [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/58qlA3zk5ZI/m/7kKok65xAAAJ) |
| 744 | |
| 745 | Prefer range-based for loops over `std::size()`: range-based for loops work even |
| 746 | for regular arrays. |
| 747 | *** |
| 748 | |
Avi Drissman | 67c3b1e | 2022-04-27 20:42:49 | [diff] [blame] | 749 | ### std::is_invocable <sup>[allowed]</sup> |
| 750 | |
| 751 | ```c++ |
| 752 | std::is_invocable_v<Fn, 1, "Hello"> |
| 753 | ``` |
| 754 | |
| 755 | **Description:** Checks whether a function may be invoked with the given |
| 756 | argument types. The `_r` variant also evaluates whether the result is |
| 757 | convertible to a given type. |
| 758 | |
| 759 | **Documentation:** |
| 760 | [std::is_invocable](https://en.cppreference.com/w/cpp/types/is_invocable) |
| 761 | |
| 762 | **Notes:** |
| 763 | *** promo |
| 764 | [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/YhlF_sTDSc0/m/QMzf42BtAAAJ) |
| 765 | *** |
| 766 | |
Avi Drissman | c109efd | 2022-04-27 22:03:35 | [diff] [blame] | 767 | ### std::conjunction/std::disjunction/std::negation <sup>[allowed]</sup> |
| 768 | |
| 769 | ```c++ |
| 770 | template<typename T, typename... Ts> |
| 771 | std::enable_if_t<std::conjunction_v<std::is_same<T, Ts>...>> |
| 772 | func(T, Ts...) { ... |
| 773 | ``` |
| 774 | |
| 775 | **Description:** Performs logical operations on type traits. |
| 776 | |
| 777 | **Documentation:** |
| 778 | [std::conjunction](https://en.cppreference.com/w/cpp/types/conjunction), |
| 779 | [std::disjunction](https://en.cppreference.com/w/cpp/types/disjunction), |
| 780 | [std::negation](https://en.cppreference.com/w/cpp/types/negation) |
| 781 | |
| 782 | **Notes:** |
| 783 | *** promo |
| 784 | [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/YhlF_sTDSc0/m/QMzf42BtAAAJ) |
| 785 | *** |
| 786 | |
Anton Bikineev | c6a02258 | 2022-10-10 19:08:58 | [diff] [blame] | 787 | ### std::hardware_{constructive|destructive}_interference_size <sup>[allowed]</sup> |
| 788 | |
| 789 | ```c++ |
| 790 | struct SharedData { |
| 791 | ReadOnlyFrequentlyUsed data; |
| 792 | alignas(std::hardware_destructive_interference_size) std::atomic<size_t> counter; |
| 793 | }; |
| 794 | ``` |
| 795 | |
| 796 | **Description:** The `std::hardware_destructive_interference_size` constant is |
| 797 | useful to avoid false sharing (destructive interference) between variables that |
| 798 | would otherwise occupy the same cacheline. In contrast, |
| 799 | `std::hardware_constructive_interference_size` is helpful to promote true |
| 800 | sharing (constructive interference), e.g. to support better locality for |
| 801 | non-contended data. |
| 802 | |
| 803 | **Documentation:** |
| 804 | [std::hardware_destructive_interference_size](https://en.cppreference.com/w/cpp/thread/hardware_destructive_interference_size), |
| 805 | [std::hardware_constructive_interference_size](https://en.cppreference.com/w/cpp/thread/hardware_destructive_interference_size) |
| 806 | |
| 807 | **Notes:** |
| 808 | *** promo |
| 809 | [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/cwktrFxxUY4/m/sP-J-s61AQAJ) |
| 810 | *** |
| 811 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 812 | ## C++17 Banned Library Features {#library-blocklist-17} |
| 813 | |
| 814 | The following C++17 library features are not allowed in the Chromium codebase. |
| 815 | |
Daniel Cheng | c05fcc6 | 2022-01-12 16:54:29 | [diff] [blame] | 816 | ### std::any <sup>[banned]</sup> |
| 817 | |
| 818 | ```c++ |
| 819 | std::any x = 5; |
| 820 | ``` |
| 821 | |
| 822 | **Description:** A type-safe container for single values of any type. |
| 823 | |
| 824 | **Documentation:** |
| 825 | [std::any](https://en.cppreference.com/w/cpp/utility/any) |
| 826 | |
| 827 | **Notes:** |
| 828 | *** promo |
| 829 | [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/KEa-0AOGRNY/m/IV_S3_pvAAAJ) |
| 830 | |
| 831 | Banned since workaround for lack of RTTI isn't compatible with the component |
| 832 | build ([Bug](https://crbug.com/1096380)). Also see `absl::any`. |
| 833 | *** |
| 834 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 835 | ### std::filesystem <sup>[banned]</sup> |
| 836 | |
| 837 | ```c++ |
| 838 | #include <filesystem> |
| 839 | ``` |
| 840 | |
| 841 | **Description:** A standard way to manipulate files, directories, and paths in a |
| 842 | filesystem. |
| 843 | |
| 844 | **Documentation:** |
| 845 | [Filesystem library](https://en.cppreference.com/w/cpp/filesystem) |
| 846 | |
| 847 | **Notes:** |
| 848 | *** promo |
| 849 | Banned by the [Google Style Guide](https://google.github.io/styleguide/cppguide.html#Other_Features). |
| 850 | *** |
| 851 | |
| 852 | ### weak_from_this <sup>[banned]</sup> |
| 853 | |
| 854 | ```c++ |
| 855 | auto weak_ptr = weak_from_this(); |
| 856 | ``` |
| 857 | |
| 858 | **Description:** Returns a `std::weak_ptr<T>` that tracks ownership of `*this` |
| 859 | by all existing `std::shared_ptr`s that refer to `*this`. |
| 860 | |
| 861 | **Documentation:** |
| 862 | [std::enable_shared_from_this<T>::weak_from_this](https://en.cppreference.com/w/cpp/memory/enable_shared_from_this/weak_from_this) |
| 863 | |
| 864 | **Notes:** |
| 865 | *** promo |
| 866 | Banned since `std::shared_ptr` and `std::weak_ptr` are banned. |
| 867 | *** |
| 868 | |
| 869 | ### Transparent std::owner_less <sup>[banned]</sup> |
| 870 | |
| 871 | ```c++ |
| 872 | std::map<std::weak_ptr<T>, U, std::owner_less<>> |
| 873 | ``` |
| 874 | |
| 875 | **Description:** Function object providing mixed-type owner-based ordering of |
| 876 | shared and weak pointers, regardless of the type of the pointee. |
| 877 | |
| 878 | **Documentation:** |
| 879 | [std::owner_less](https://en.cppreference.com/w/cpp/memory/owner_less) |
| 880 | |
| 881 | **Notes:** |
| 882 | *** promo |
| 883 | Banned since `std::shared_ptr` and `std::weak_ptr` are banned. |
| 884 | *** |
| 885 | |
| 886 | ### Array support for std::shared_ptr <sup>[banned]</sup> |
| 887 | |
| 888 | ```c++ |
| 889 | std::shared_ptr<int[]> p(new int[10]{0,1,2,3,4,5,6,7,8,9}); |
| 890 | std::cout << p[3]; // "3" |
| 891 | ``` |
| 892 | |
| 893 | **Description:** Supports memory management of arrays via `std::shared_ptr`. |
| 894 | |
| 895 | **Documentation:** |
| 896 | [std::shared_ptr](https://en.cppreference.com/w/cpp/memory/shared_ptr) |
| 897 | |
| 898 | **Notes:** |
| 899 | *** promo |
| 900 | Banned since `std::shared_ptr` is banned. |
| 901 | *** |
| 902 | |
| 903 | ### std::uncaught_exceptions <sup>[banned]</sup> |
| 904 | |
| 905 | ```c++ |
| 906 | int count = std::uncaught_exceptions(); |
| 907 | ``` |
| 908 | |
| 909 | **Description:** Determines whether there are live exception objects. |
| 910 | |
| 911 | **Documentation:** |
| 912 | [std::uncaught_exceptions](https://en.cppreference.com/w/cpp/error/uncaught_exception) |
| 913 | |
| 914 | **Notes:** |
| 915 | *** promo |
| 916 | Banned because exceptions are banned. |
| 917 | *** |
| 918 | |
| 919 | ### Rounding functions for duration and time_point <sup>[banned]</sup> |
| 920 | |
| 921 | ```c++ |
| 922 | std::chrono::ceil<std::chrono::seconds>(dur); |
| 923 | std::chrono::ceil<std::chrono::seconds>(time_pt); |
| 924 | std::chrono::floor<std::chrono::seconds>(dur); |
| 925 | std::chrono::floor<std::chrono::seconds>(time_pt); |
| 926 | std::chrono::round<std::chrono::seconds>(dur); |
| 927 | std::chrono::round<std::chrono::seconds>(time_pt); |
| 928 | ``` |
| 929 | |
| 930 | **Description:** Converts durations and time_points by rounding. |
| 931 | |
| 932 | **Documentation:** |
| 933 | [std::chrono::duration](https://en.cppreference.com/w/cpp/chrono/duration), |
| 934 | [std::chrono::time_point](https://en.cppreference.com/w/cpp/chrono/time_point) |
| 935 | |
| 936 | **Notes:** |
| 937 | *** promo |
| 938 | Banned since `std::chrono` is banned. |
| 939 | *** |
| 940 | |
Avi Drissman | 37b7d51 | 2022-04-01 22:01:40 | [diff] [blame] | 941 | ### std::variant <sup>[banned]</sup> |
| 942 | |
| 943 | ```c++ |
| 944 | std::variant<int, double> v = 12; |
| 945 | ``` |
| 946 | |
| 947 | **Description:** The class template `std::variant` represents a type-safe |
| 948 | `union`. An instance of `std::variant` at any given time holds a value of one of |
| 949 | its alternative types (it's also possible for it to be valueless). |
| 950 | |
| 951 | **Documentation:** |
| 952 | [std::variant](https://en.cppreference.com/w/cpp/utility/variant) |
| 953 | |
| 954 | **Notes:** |
| 955 | *** promo |
| 956 | Banned for now because it does not provide safety guarantees in the case of |
| 957 | misuse. The Chromium C++ team is investigating the possibility of hardening the |
| 958 | C++ library so that the standard version can be used. In the meanwhile, use |
| 959 | `absl::variant` instead. |
| 960 | *** |
| 961 | |
| 962 | ### std::optional <sup>[banned]</sup> |
| 963 | |
| 964 | ```c++ |
| 965 | std::optional<std::string> s; |
| 966 | ``` |
| 967 | |
| 968 | **Description:** The class template `std::optional` manages an optional |
| 969 | contained value, i.e. a value that may or may not be present. A common use case |
| 970 | for optional is the return value of a function that may fail. |
| 971 | |
| 972 | **Documentation:** |
| 973 | [std::optional](https://en.cppreference.com/w/cpp/utility/optional) |
| 974 | |
| 975 | **Notes:** |
| 976 | *** promo |
| 977 | Banned for now because it does not provide safety guarantees in the case of |
| 978 | misuse. The Chromium C++ team is investigating the possibility of hardening the |
| 979 | C++ library so that the standard version can be used. In the meanwhile, use |
| 980 | `absl::optional` instead. |
| 981 | *** |
| 982 | |
Avi Drissman | bc6545f4 | 2022-05-03 17:47:38 | [diff] [blame] | 983 | ### std::in_place/in_place_type/in_place_index/in_place_t/in_place_type_t/in_place_index_t <sup>[banned]</sup> |
| 984 | |
| 985 | ```c++ |
| 986 | std::optional<std::complex<double>> opt{std::in_place, 0, 1}; |
| 987 | std::variant<int, float> v{std::in_place_type<int>, 1.4}; |
| 988 | ``` |
| 989 | |
| 990 | **Description:** The `std::in_place` are disambiguation tags for |
| 991 | `std::optional`, `std::variant`, and `std::any` to indicate that the object |
| 992 | should be constructed in-place. |
| 993 | |
| 994 | **Documentation:** |
| 995 | [std::in_place](https://en.cppreference.com/w/cpp/utility/in_place) |
| 996 | |
| 997 | **Notes:** |
| 998 | *** promo |
| 999 | Banned for now because `std::optional`, `std::variant`, and `std::any` are all |
| 1000 | banned for now. Because `absl::optional` and `absl::variant` are used instead, |
| 1001 | and they require `absl::in_place`, use `absl::in_place` for non-Abseil Chromium |
| 1002 | code. See the |
| 1003 | [discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ZspmuJPpv6s/m/wYYTCiRwAAAJ). |
| 1004 | *** |
| 1005 | |
Avi Drissman | 37b7d51 | 2022-04-01 22:01:40 | [diff] [blame] | 1006 | ### std::clamp <sup>[banned]</sup> |
| 1007 | |
| 1008 | ```c++ |
| 1009 | int x = std::clamp(inp, 0, 100); |
| 1010 | ``` |
| 1011 | |
| 1012 | **Description:** Clamps a value between a minimum and a maximum. |
| 1013 | |
| 1014 | **Documentation:** |
| 1015 | [std::clamp](https://en.cppreference.com/w/cpp/algorithm/clamp) |
| 1016 | |
| 1017 | **Notes:** |
| 1018 | *** promo |
| 1019 | Banned for now because it does not provide safety guarantees in the case of |
| 1020 | misuse. The Chromium C++ team is investigating the possibility of hardening the |
| 1021 | C++ library so that the standard version can be used. In the meanwhile, use |
| 1022 | `base::clamp` instead. |
| 1023 | *** |
| 1024 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 1025 | ## C++17 TBD Language Features {#core-review-17} |
| 1026 | |
| 1027 | The following C++17 language features are not allowed in the Chromium codebase. |
| 1028 | See the top of this page on how to propose moving a feature from this list into |
| 1029 | the allowed or banned sections. |
| 1030 | |
| 1031 | ### Declaring non-type template parameters with auto <sup>[tbd]</sup> |
| 1032 | |
| 1033 | ```c++ |
| 1034 | template <auto... seq> |
| 1035 | struct my_integer_sequence { |
| 1036 | // ... |
| 1037 | }; |
| 1038 | auto seq = my_integer_sequence<0, 1, 2>(); // Type deduced to be `int`. |
| 1039 | ``` |
| 1040 | |
| 1041 | **Description:** Following the deduction rules of `auto`, while respecting the |
| 1042 | non-type template parameter list of allowable types, template arguments can be |
| 1043 | deduced from the types of its arguments. |
| 1044 | |
| 1045 | **Documentation:** |
| 1046 | [Template parameters](https://en.cppreference.com/w/cpp/language/template_parameters) |
| 1047 | |
| 1048 | **Notes:** |
| 1049 | *** promo |
| 1050 | None |
| 1051 | *** |
| 1052 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 1053 | ### constexpr lambda <sup>[tbd]</sup> |
| 1054 | |
| 1055 | ```c++ |
| 1056 | auto identity = [](int n) constexpr { return n; }; |
| 1057 | static_assert(identity(123) == 123); |
| 1058 | ``` |
| 1059 | |
| 1060 | **Description:** Compile-time lambdas using constexpr. |
| 1061 | |
| 1062 | **Documentation:** |
| 1063 | [Lambda expressions](https://en.cppreference.com/w/cpp/language/lambda) |
| 1064 | |
| 1065 | **Notes:** |
| 1066 | *** promo |
| 1067 | None |
| 1068 | *** |
| 1069 | |
| 1070 | ### Lambda capture this by value <sup>[tbd]</sup> |
| 1071 | |
| 1072 | ```c++ |
| 1073 | const auto l = [*this] { return member_; } |
| 1074 | ``` |
| 1075 | |
| 1076 | **Description:** `*this` captures the current object by copy, while `this` |
| 1077 | continues to capture by reference. |
| 1078 | |
| 1079 | **Documentation:** |
| 1080 | [Lambda capture](https://en.cppreference.com/w/cpp/language/lambda#Lambda_capture) |
| 1081 | |
| 1082 | **Notes:** |
| 1083 | *** promo |
| 1084 | None |
| 1085 | *** |
| 1086 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 1087 | ### UTF-8 character literals <sup>[tbd]</sup> |
| 1088 | |
| 1089 | ```c++ |
| 1090 | char x = u8'x'; |
| 1091 | ``` |
| 1092 | |
| 1093 | **Description:** A character literal that begins with `u8` is a character |
| 1094 | literal of type `char`. The value of a UTF-8 character literal is equal to its |
| 1095 | ISO 10646 code point value. |
| 1096 | |
| 1097 | **Documentation:** |
| 1098 | [Character literal](https://en.cppreference.com/w/cpp/language/character_literal) |
| 1099 | |
| 1100 | **Notes:** |
| 1101 | *** promo |
| 1102 | C++20 changes the type to `char8_t`, causing migration hazards for code using |
| 1103 | this. |
| 1104 | *** |
| 1105 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 1106 | ### using declaration for attributes <sup>[tbd]</sup> |
| 1107 | |
| 1108 | ```c++ |
| 1109 | [[using CC: opt(1), debug]] // same as [[CC:opt(1), CC::debug]] |
| 1110 | ``` |
| 1111 | |
| 1112 | **Description:** Specifies a common namespace for a list of attributes. |
| 1113 | |
| 1114 | **Documentation:** |
| 1115 | [Attribute specifier sequence](https://en.cppreference.com/w/cpp/language/attributes) |
| 1116 | |
| 1117 | **Notes:** |
| 1118 | *** promo |
| 1119 | See similar attribute macros in base/compiler_specific.h. |
| 1120 | *** |
| 1121 | |
| 1122 | ### __has_include <sup>[tbd]</sup> |
| 1123 | |
| 1124 | ```c++ |
| 1125 | #if __has_include(<optional>) ... |
| 1126 | ``` |
| 1127 | |
| 1128 | **Description:** Checks whether a file is available for inclusion, i.e. the file |
| 1129 | exists. |
| 1130 | |
| 1131 | **Documentation:** |
| 1132 | [Source file inclusion](https://en.cppreference.com/w/cpp/preprocessor/include) |
| 1133 | |
| 1134 | **Notes:** |
| 1135 | *** promo |
| 1136 | None |
| 1137 | *** |
| 1138 | |
| 1139 | ## C++17 TBD Library Features {#library-review-17} |
| 1140 | |
| 1141 | The following C++17 library features are not allowed in the Chromium codebase. |
| 1142 | See the top of this page on how to propose moving a feature from this list into |
| 1143 | the allowed or banned sections. |
| 1144 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 1145 | ### std::string_view <sup>[tbd]</sup> |
| 1146 | |
| 1147 | ```c++ |
| 1148 | std::string_view str = "foo"; |
| 1149 | ``` |
| 1150 | |
| 1151 | **Description:** A non-owning reference to a string. Useful for providing an |
| 1152 | abstraction on top of strings (e.g. for parsing). |
| 1153 | |
| 1154 | **Documentation:** |
| 1155 | [std::basic_string_view](https://en.cppreference.com/w/cpp/string/basic_string_view) |
| 1156 | |
| 1157 | **Notes:** |
| 1158 | *** promo |
| 1159 | See also `absl::string_view` and `base::StringPiece`. |
| 1160 | *** |
| 1161 | |
| 1162 | ### std::invoke <sup>[tbd]</sup> |
| 1163 | |
| 1164 | ```c++ |
| 1165 | static_assert(std::invoke(std::plus<>(), 1, 2) == 3); |
| 1166 | ``` |
| 1167 | |
| 1168 | **Description:** Invokes a `Callable` object with parameters. An example of a |
| 1169 | `Callable` object is `base::Callback` where an object can be called similarly to |
| 1170 | a regular function. |
| 1171 | |
| 1172 | **Documentation:** |
| 1173 | [std::invoke](https://en.cppreference.com/w/cpp/utility/functional/invoke) |
| 1174 | |
| 1175 | **Notes:** |
| 1176 | *** promo |
| 1177 | See also `base::invoke`. |
| 1178 | *** |
| 1179 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 1180 | ### std::byte <sup>[tbd]</sup> |
| 1181 | |
| 1182 | ```c++ |
| 1183 | std::byte b = 0xFF; |
| 1184 | int i = std::to_integer<int>(b); // 0xFF |
| 1185 | ``` |
| 1186 | |
| 1187 | **Description:** A standard way of representing data as a byte. `std::byte` is |
| 1188 | neither a character type nor an arithmetic type, and the only operator overloads |
| 1189 | available are bitwise operations. |
| 1190 | |
| 1191 | **Documentation:** |
| 1192 | [std::byte](https://en.cppreference.com/w/cpp/types/byte) |
| 1193 | |
| 1194 | **Notes:** |
| 1195 | *** promo |
| 1196 | None |
| 1197 | *** |
| 1198 | |
| 1199 | ### Splicing for maps and sets <sup>[tbd]</sup> |
| 1200 | |
| 1201 | ```c++ |
| 1202 | std::map<...>::extract |
| 1203 | std::map<...>::merge |
| 1204 | std::set<...>::extract |
| 1205 | std::set<...>::merge |
| 1206 | ``` |
| 1207 | |
| 1208 | **Description:** Moving nodes and merging containers without the overhead of |
| 1209 | expensive copies, moves, or heap allocations/deallocations. |
| 1210 | |
| 1211 | **Documentation:** |
| 1212 | [std::map::extract](https://en.cppreference.com/w/cpp/container/map/extract), |
| 1213 | [std::map::merge](https://en.cppreference.com/w/cpp/container/map/merge) |
| 1214 | |
| 1215 | **Notes:** |
| 1216 | *** promo |
| 1217 | None |
| 1218 | *** |
| 1219 | |
| 1220 | ### Parallel algorithms <sup>[tbd]</sup> |
| 1221 | |
| 1222 | ```c++ |
| 1223 | auto it = std::find(std::execution::par, std::begin(vec), std::end(vec), 2); |
| 1224 | ``` |
| 1225 | |
| 1226 | **Description:** Many of the STL algorithms, such as the `copy`, `find` and |
| 1227 | `sort` methods, now support the parallel execution policies: `seq`, `par`, and |
| 1228 | `par_unseq` which translate to "sequentially", "parallel" and |
| 1229 | "parallel unsequenced". |
| 1230 | |
| 1231 | **Documentation:** |
| 1232 | [execution_policy_tag_t](https://en.cppreference.com/w/cpp/algorithm/execution_policy_tag_t) |
| 1233 | |
| 1234 | **Notes:** |
| 1235 | *** promo |
| 1236 | None |
| 1237 | *** |
| 1238 | |
| 1239 | ### std::make_from_tuple <sup>[tbd]</sup> |
| 1240 | |
| 1241 | ```c++ |
| 1242 | // Calls Foo(int, double): |
| 1243 | auto foo = std::make_from_tuple<Foo>(std::make_tuple(1, 3.5)); |
| 1244 | ``` |
| 1245 | |
| 1246 | **Description:** Constructs an object from a tuple of arguments. |
| 1247 | |
| 1248 | **Documentation:** |
| 1249 | [std::make_from_tuple](https://en.cppreference.com/w/cpp/utility/make_from_tuple) |
| 1250 | |
| 1251 | **Notes:** |
| 1252 | *** promo |
| 1253 | See also `absl::make_from_tuple`. |
| 1254 | *** |
| 1255 | |
| 1256 | ### Searchers <sup>[tbd]</sup> |
| 1257 | |
| 1258 | ```c++ |
| 1259 | auto it = std::search(haystack.begin(), haystack.end(), |
| 1260 | std::boyer_moore_searcher(needle.begin(), needle.end())); |
| 1261 | ``` |
| 1262 | |
| 1263 | **Description:** Alternate string searching algorithms. |
| 1264 | |
| 1265 | **Documentation:** |
| 1266 | [Searchers](https://en.cppreference.com/w/cpp/utility/functional#Searchers) |
| 1267 | |
| 1268 | **Notes:** |
| 1269 | *** promo |
| 1270 | None |
| 1271 | *** |
| 1272 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 1273 | ### std::not_fn <sup>[tbd]</sup> |
| 1274 | |
| 1275 | ```c++ |
| 1276 | auto nonwhite = std::find_if(str.begin(), str.end(), std::not_fn(IsWhitespace)); |
| 1277 | ``` |
| 1278 | |
| 1279 | **Description:** Creates a forwarding call wrapper that returns the negation of |
| 1280 | the callable object it holds. |
| 1281 | |
| 1282 | **Documentation:** |
| 1283 | [std::not_fn](https://en.cppreference.com/w/cpp/utility/functional/not_fn) |
| 1284 | |
| 1285 | **Notes:** |
| 1286 | *** promo |
| 1287 | See also `base::not_fn`. |
| 1288 | *** |
| 1289 | |
| 1290 | ### Uninitialized memory algorithms <sup>[tbd]</sup> |
| 1291 | |
| 1292 | ```c++ |
| 1293 | std::destroy_at(ptr); |
| 1294 | std::destroy(ptr, ptr + 8); |
| 1295 | std::destroy_n(ptr, 8); |
| 1296 | std::uninitialized_move(src.begin(), src.end(), dest.begin()); |
| 1297 | std::uninitialized_value_construct(std::begin(storage), std::end(storage)); |
| 1298 | ``` |
| 1299 | |
| 1300 | **Description:** Replaces direct constructor and destructor calls when manually |
| 1301 | managing memory. |
| 1302 | |
| 1303 | **Documentation:** |
| 1304 | [std::destroy_at](https://en.cppreference.com/w/cpp/memory/destroy_at), |
| 1305 | [std::destroy](https://en.cppreference.com/w/cpp/memory/destroy), |
| 1306 | [std::destroy_n](https://en.cppreference.com/w/cpp/memory/destroy_n), |
| 1307 | [std::uninitialized_move](https://en.cppreference.com/w/cpp/memory/uninitialized_move), |
| 1308 | [std::uninitialized_value_construct](https://en.cppreference.com/w/cpp/memory/uninitialized_value_construct) |
| 1309 | |
| 1310 | **Notes:** |
| 1311 | *** promo |
| 1312 | None |
| 1313 | *** |
| 1314 | |
| 1315 | ### std::pmr::memory_resource and std::polymorphic_allocator <sup>[tbd]</sup> |
| 1316 | |
| 1317 | ```c++ |
| 1318 | #include <memory_resource> |
| 1319 | ``` |
| 1320 | |
| 1321 | **Description:** Manages memory allocations using runtime polymorphism. |
| 1322 | |
| 1323 | **Documentation:** |
| 1324 | [std::pmr::memory_resource](https://en.cppreference.com/w/cpp/memory/memory_resource), |
| 1325 | [std::pmr::polymorphic_allocator](https://en.cppreference.com/w/cpp/memory/polymorphic_allocator), |
| 1326 | |
| 1327 | **Notes:** |
| 1328 | *** promo |
| 1329 | May not be supported in libc++, according to the |
| 1330 | [library features table](https://en.cppreference.com/w/cpp/17) |
| 1331 | *** |
| 1332 | |
| 1333 | ### std::aligned_alloc <sup>[tbd]</sup> |
| 1334 | |
| 1335 | ```c++ |
| 1336 | int* p2 = static_cast<int*>(std::aligned_alloc(1024, 1024)); |
| 1337 | ``` |
| 1338 | |
| 1339 | **Description:** Allocates uninitialized storage with the specified alignment. |
| 1340 | |
| 1341 | **Documentation:** |
| 1342 | [std::aligned_alloc](https://en.cppreference.com/w/cpp/memory/c/aligned_alloc) |
| 1343 | |
| 1344 | **Notes:** |
| 1345 | *** promo |
| 1346 | None |
| 1347 | *** |
| 1348 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 1349 | ### std::is_swappable <sup>[tbd]</sup> |
| 1350 | |
| 1351 | ```c++ |
| 1352 | std::is_swappable<T> |
| 1353 | std::is_swappable_with_v<T, U> |
| 1354 | ``` |
| 1355 | |
| 1356 | **Description:** Checks whether classes may be swapped. |
| 1357 | |
| 1358 | **Documentation:** |
| 1359 | [std::is_swappable](https://en.cppreference.com/w/cpp/types/is_swappable) |
| 1360 | |
| 1361 | **Notes:** |
| 1362 | *** promo |
| 1363 | None |
| 1364 | *** |
| 1365 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 1366 | ### std::is_aggregate <sup>[tbd]</sup> |
| 1367 | |
| 1368 | ```c++ |
| 1369 | if constexpr(std::is_aggregate_v<T>) { ... |
| 1370 | ``` |
| 1371 | |
| 1372 | **Description:** Checks wither the given type is an aggregate type. |
| 1373 | |
| 1374 | **Documentation:** |
| 1375 | [std::is_aggregate](https://en.cppreference.com/w/cpp/types/is_aggregate) |
| 1376 | |
| 1377 | **Notes:** |
| 1378 | *** promo |
| 1379 | None |
| 1380 | *** |
| 1381 | |
| 1382 | ### std::has_unique_object_representations <sup>[tbd]</sup> |
| 1383 | |
| 1384 | ```c++ |
| 1385 | std::has_unique_object_representations_v<foo> |
| 1386 | ``` |
| 1387 | |
| 1388 | **Description:** Checks wither the given type is trivially copyable and any two |
| 1389 | objects with the same value have the same object representation. |
| 1390 | |
| 1391 | **Documentation:** |
| 1392 | [std::has_unique_object_representations](https://en.cppreference.com/w/cpp/types/has_unique_object_representations) |
| 1393 | |
| 1394 | **Notes:** |
| 1395 | *** promo |
| 1396 | None |
| 1397 | *** |
| 1398 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 1399 | ### std::reduce <sup>[tbd]</sup> |
| 1400 | |
| 1401 | ```c++ |
| 1402 | std::reduce(std::execution::par, v.cbegin(), v.cend()); |
| 1403 | ``` |
| 1404 | |
| 1405 | **Description:** Like `std::accumulate` except the elements of the range may be |
| 1406 | grouped and rearranged in arbitrary order. |
| 1407 | |
| 1408 | **Documentation:** |
| 1409 | [std::reduce](https://en.cppreference.com/w/cpp/algorithm/reduce) |
| 1410 | |
| 1411 | **Notes:** |
| 1412 | *** promo |
| 1413 | Makes the most sense in conjunction with `std::execution::par`. |
| 1414 | *** |
| 1415 | |
| 1416 | ### std::inclusive_scan <sup>[tbd]</sup> |
| 1417 | |
| 1418 | ```c++ |
| 1419 | std::inclusive_scan(data.begin(), data.end(), output.begin()); |
| 1420 | ``` |
| 1421 | |
| 1422 | **Description:** Like `std::accumulate` but writes the result at each step into |
| 1423 | the output range. |
| 1424 | |
| 1425 | **Documentation:** |
| 1426 | [std::inclusive_scan](https://en.cppreference.com/w/cpp/algorithm/inclusive_scan) |
| 1427 | |
| 1428 | **Notes:** |
| 1429 | *** promo |
| 1430 | None |
| 1431 | *** |
| 1432 | |
| 1433 | ### std::exclusive_scan <sup>[tbd]</sup> |
| 1434 | |
| 1435 | ```c++ |
| 1436 | std::exclusive_scan(data.begin(), data.end(), output.begin()); |
| 1437 | ``` |
| 1438 | |
| 1439 | **Description:** Like `std::inclusive_scan` but omits the current element from |
| 1440 | the written output at each step; that is, results are "one value behind" those |
| 1441 | of `std::inclusive_scan`. |
| 1442 | |
| 1443 | **Documentation:** |
| 1444 | [std::exclusive_scan](https://en.cppreference.com/w/cpp/algorithm/exclusive_scan) |
| 1445 | |
| 1446 | **Notes:** |
| 1447 | *** promo |
| 1448 | None |
| 1449 | *** |
| 1450 | |
| 1451 | ### std::gcd <sup>[tbd]</sup> |
| 1452 | |
| 1453 | ```c++ |
| 1454 | static_assert(std::gcd(12, 18) == 6); |
| 1455 | ``` |
| 1456 | |
| 1457 | **Description:** Computes the greatest common divisor of its arguments. |
| 1458 | |
| 1459 | **Documentation:** |
| 1460 | [std::gcd](https://en.cppreference.com/w/cpp/numeric/gcd) |
| 1461 | |
| 1462 | **Notes:** |
| 1463 | *** promo |
| 1464 | None |
| 1465 | *** |
| 1466 | |
| 1467 | ### std::lcm <sup>[tbd]</sup> |
| 1468 | |
| 1469 | ```c++ |
| 1470 | static_assert(std::lcm(12, 18) == 36); |
| 1471 | ``` |
| 1472 | |
| 1473 | **Description:** Computes the least common multiple of its arguments. |
| 1474 | |
| 1475 | **Documentation:** |
| 1476 | [std::lcm](https://en.cppreference.com/w/cpp/numeric/lcm) |
| 1477 | |
| 1478 | **Notes:** |
| 1479 | *** promo |
| 1480 | None |
| 1481 | *** |
| 1482 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 1483 | ### Mathematical special functions <sup>[tbd]</sup> |
| 1484 | |
| 1485 | ```c++ |
| 1486 | std::assoc_laguerre() |
| 1487 | std::assoc_legendre() |
| 1488 | std::beta() |
| 1489 | std::comp_ellint_1() |
| 1490 | std::comp_ellint_2() |
| 1491 | std::comp_ellint_3() |
| 1492 | std::cyl_bessel_i() |
| 1493 | std::cyl_bessel_j() |
| 1494 | std::cyl_bessel_k() |
| 1495 | std::cyl_neumann() |
| 1496 | std::ellint_1() |
| 1497 | std::ellint_2() |
| 1498 | std::ellint_3() |
| 1499 | std::expint() |
| 1500 | std::hermite() |
| 1501 | std::legendre() |
| 1502 | std::laguerre() |
| 1503 | std::riemann_zeta() |
| 1504 | std::sph_bessel() |
| 1505 | std::sph_legendre() |
| 1506 | std::sph_neumann() |
| 1507 | ``` |
| 1508 | |
| 1509 | **Description:** A variety of mathematical functions. |
| 1510 | |
| 1511 | **Documentation:** |
| 1512 | [Mathematical special functions](https://en.cppreference.com/w/cpp/numeric/special_functions) |
| 1513 | |
| 1514 | **Notes:** |
| 1515 | *** promo |
| 1516 | May not be supported in libc++, according to the |
| 1517 | [library features table](https://en.cppreference.com/w/cpp/17) |
| 1518 | *** |
| 1519 | |
| 1520 | ### 3D std::hypot <sup>[tbd]</sup> |
| 1521 | |
| 1522 | ```c++ |
| 1523 | double dist = std::hypot(1.0, 2.5, 3.7); |
| 1524 | ``` |
| 1525 | |
| 1526 | **Description:** Computes the distance from the origin in 3D space. |
| 1527 | |
| 1528 | **Documentation:** |
| 1529 | [std::hypot](https://en.cppreference.com/w/cpp/numeric/math/hypot) |
| 1530 | |
| 1531 | **Notes:** |
| 1532 | *** promo |
| 1533 | None |
| 1534 | *** |
| 1535 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 1536 | ### std::launder <sup>[tbd]</sup> |
| 1537 | |
| 1538 | ```c++ |
| 1539 | struct Y { int z; }; |
| 1540 | alignas(Y) std::byte s[sizeof(Y)]; |
| 1541 | Y* q = new(&s) Y{2}; |
| 1542 | const int h = std::launder(reinterpret_cast<Y*>(&s))->z; |
| 1543 | ``` |
| 1544 | |
| 1545 | **Description:** When used to wrap a pointer, makes it valid to access the |
| 1546 | resulting object in cases it otherwise wouldn't have been, in a very limited set |
| 1547 | of circumstances. |
| 1548 | |
| 1549 | **Documentation:** |
| 1550 | [std::launder](https://en.cppreference.com/w/cpp/utility/launder) |
| 1551 | |
| 1552 | **Notes:** |
| 1553 | *** promo |
| 1554 | None |
| 1555 | *** |
| 1556 | |
| 1557 | ### std::to_chars/std::from_chars <sup>[tbd]</sup> |
| 1558 | |
| 1559 | ```c++ |
| 1560 | std::to_chars(str.data(), str.data() + str.size(), 42); |
| 1561 | std::from_chars(str.data(), str.data() + str.size(), result); |
| 1562 | ``` |
| 1563 | |
| 1564 | **Description:** Locale-independent, non-allocating, non-throwing functions to |
| 1565 | convert values to/from character strings, designed for use in high-throughput |
| 1566 | contexts. |
| 1567 | |
| 1568 | **Documentation:** |
| 1569 | [std::to_chars](https://en.cppreference.com/w/cpp/utility/to_chars), |
| 1570 | [std::from_chars](https://en.cppreference.com/w/cpp/utility/from_chars) |
| 1571 | |
| 1572 | **Notes:** |
| 1573 | *** promo |
| 1574 | None |
| 1575 | *** |
| 1576 | |
| 1577 | ### std::atomic<T>::is_always_lock_free <sup>[tbd]</sup> |
| 1578 | |
| 1579 | ```c++ |
| 1580 | template <typename T> |
| 1581 | struct is_lock_free_impl |
| 1582 | : std::integral_constant<bool, std::atomic<T>::is_always_lock_free> {}; |
| 1583 | ``` |
| 1584 | |
| 1585 | **Description:** True when the given atomic type is always lock-free. |
| 1586 | |
| 1587 | **Documentation:** |
| 1588 | [std::atomic<T>::is_always_lock_free](https://en.cppreference.com/w/cpp/atomic/atomic/is_always_lock_free) |
| 1589 | |
| 1590 | **Notes:** |
| 1591 | *** promo |
| 1592 | None |
| 1593 | *** |
| 1594 | |
| 1595 | ### std::scoped_lock <sup>[tbd]</sup> |
| 1596 | |
| 1597 | ```c++ |
| 1598 | std::scoped_lock lock(e1.m, e2.m); |
| 1599 | ``` |
| 1600 | |
| 1601 | **Description:** Provides an RAII-style mechanism for owning one or more mutexes |
| 1602 | for the duration of a scoped block. |
| 1603 | |
| 1604 | **Documentation:** |
| 1605 | [std::scoped_lock](https://en.cppreference.com/w/cpp/thread/scoped_lock) |
| 1606 | |
| 1607 | **Notes:** |
| 1608 | *** promo |
| 1609 | See also `base::AutoLock`. |
| 1610 | *** |
| 1611 | |
| 1612 | ### std::timespec_get <sup>[tbd]</sup> |
| 1613 | |
| 1614 | ```c++ |
| 1615 | std::timespec ts; |
| 1616 | std::timespec_get(&ts, TIME_UTC); |
| 1617 | ``` |
| 1618 | |
| 1619 | **Description:** Gets the current calendar time in the given time base. |
| 1620 | |
| 1621 | **Documentation:** |
| 1622 | [std::timespec_get](https://en.cppreference.com/w/cpp/chrono/c/timespec_get) |
| 1623 | |
| 1624 | **Notes:** |
| 1625 | *** promo |
| 1626 | None |
| 1627 | *** |
| 1628 | |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1629 | ## Abseil Banned Library Features {#absl-blocklist} |
| 1630 | |
| 1631 | The following Abseil library features are not allowed in the Chromium codebase. |
| 1632 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 1633 | ### Any <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1634 | |
| 1635 | ```c++ |
| 1636 | absl::any a = int{5}; |
| 1637 | EXPECT_THAT(absl::any_cast<int>(&a), Pointee(5)); |
| 1638 | EXPECT_EQ(absl::any_cast<size_t>(&a), nullptr); |
| 1639 | ``` |
| 1640 | |
| 1641 | **Description:** Early adaptation of C++17 `std::any`. |
| 1642 | |
| 1643 | **Documentation:** [std::any](https://en.cppreference.com/w/cpp/utility/any) |
| 1644 | |
| 1645 | **Notes:** |
| 1646 | *** promo |
| 1647 | Banned since workaround for lack of RTTI isn't compatible with the component |
Daniel Cheng | c05fcc6 | 2022-01-12 16:54:29 | [diff] [blame] | 1648 | build ([Bug](https://crbug.com/1096380)). Also see `std::any`. |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1649 | *** |
| 1650 | |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame^] | 1651 | ### bind_front <sup>[banned]</sup> |
| 1652 | |
| 1653 | ```c++ |
| 1654 | absl::bind_front |
| 1655 | ``` |
| 1656 | |
| 1657 | **Description:** Binds the first N arguments of an invocable object and stores them by value. |
| 1658 | |
| 1659 | **Documentation:** |
| 1660 | * [bind_front.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/bind_front.h) |
| 1661 | * [Avoid std::bind](https://abseil.io/tips/108) |
| 1662 | |
| 1663 | **Notes:** |
| 1664 | *** promo |
| 1665 | Banned due to overlap with `base::Bind`. Use `base::Bind` instead. |
| 1666 | *** |
| 1667 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 1668 | ### Command line flags <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1669 | |
| 1670 | ```c++ |
| 1671 | ABSL_FLAG(bool, logs, false, "print logs to stderr"); |
| 1672 | app --logs=true; |
| 1673 | ``` |
| 1674 | |
| 1675 | **Description:** Allows programmatic access to flag values passed on the |
| 1676 | command-line to binaries. |
| 1677 | |
| 1678 | **Documentation:** [Flags Library](https://abseil.io/docs/cpp/guides/flags) |
| 1679 | |
| 1680 | **Notes:** |
| 1681 | *** promo |
| 1682 | Banned since workaround for lack of RTTI isn't compatible with the component |
| 1683 | build. ([Bug](https://crbug.com/1096380)) Use `base::CommandLine` instead. |
| 1684 | *** |
| 1685 | |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame^] | 1686 | ### Container utilities <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1687 | |
| 1688 | ```c++ |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame^] | 1689 | auto it = absl::c_find(container, value); |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1690 | ``` |
| 1691 | |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame^] | 1692 | **Description:** Container-based versions of algorithmic functions within C++ |
| 1693 | standard library. |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1694 | |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame^] | 1695 | **Documentation:** |
| 1696 | [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] | 1697 | |
| 1698 | **Notes:** |
| 1699 | *** promo |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame^] | 1700 | Banned due to overlap with `base/ranges/algorithm.h`. Use the `base/ranges/` |
| 1701 | facilities instead. |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1702 | *** |
| 1703 | |
Daniel Cheng | 2248b33 | 2022-07-27 06:16:59 | [diff] [blame] | 1704 | ### FunctionRef <sup>[banned]</sup> |
| 1705 | |
| 1706 | ```c++ |
| 1707 | absl::FunctionRef |
| 1708 | ``` |
| 1709 | |
| 1710 | **Description:** Type for holding a non-owning reference to an object of any |
| 1711 | invocable type. |
| 1712 | |
| 1713 | **Documentation:** |
| 1714 | [function_ref.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/function_ref.h) |
| 1715 | |
| 1716 | **Notes:** |
| 1717 | *** promo |
| 1718 | - `absl::FunctionRef` is banned due to allowing implicit conversions between |
| 1719 | function signatures in potentially surprising ways. For example, a callable |
| 1720 | with the signature `int()` will bind to `absl::FunctionRef<void()>`: the |
| 1721 | return value from the callable will be silently discarded. |
| 1722 | - In Chromium, use `base::FunctionRef` instead. |
| 1723 | - Unlike `base::OnceCallback` and `base::RepeatingCallback`, `base::FunctionRef` |
| 1724 | supports capturing lambdas. |
| 1725 | - Useful when passing an invocable object to a function that synchronously calls |
| 1726 | the invocable object, e.g. `ForEachFrame(base::FunctionRef<void(Frame&)>)`. |
| 1727 | This can often result in clearer code than code that is templated to accept |
| 1728 | lambdas, e.g. with `template <typename Invocable> void |
| 1729 | ForEachFrame(Invocable invocable)`, it is much less obvious what arguments |
| 1730 | will be passed to `invocable`. |
| 1731 | - For now, `base::OnceCallback` and `base::RepeatingCallback` intentionally |
| 1732 | disallow conversions to `base::FunctionRef`, under the theory that the |
| 1733 | callback should be a capturing lambda instead. Attempting to use this |
| 1734 | conversion will trigger a `static_assert` requesting additional feedback for |
| 1735 | use cases where this conversion would be valuable. |
| 1736 | - *Important:* `base::FunctionRef` must not outlive the function call. Like |
| 1737 | `base::StringPiece`, `base::FunctionRef` is a *non-owning* reference. Using a |
| 1738 | `base::FunctionRef` as a return value or class field is dangerous and likely |
| 1739 | to result in lifetime bugs. |
| 1740 | - [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/JVN4E4IIYA0/m/V0EVUVLiBwAJ) |
| 1741 | *** |
| 1742 | |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame^] | 1743 | ### Random <sup>[banned]</sup> |
| 1744 | |
| 1745 | ```c++ |
| 1746 | absl::BitGen bitgen; |
| 1747 | size_t index = absl::Uniform(bitgen, 0u, elems.size()); |
| 1748 | ``` |
| 1749 | |
| 1750 | **Description:** Functions and utilities for generating pseudorandom data. |
| 1751 | |
| 1752 | **Documentation:** [Random library](https://abseil.io/docs/cpp/guides/random) |
| 1753 | |
| 1754 | **Notes:** |
| 1755 | *** promo |
| 1756 | Banned because most uses of random values in Chromium should be using a |
| 1757 | cryptographically secure generator. Use `base/rand_util.h` instead. |
| 1758 | *** |
| 1759 | |
| 1760 | ### Span <sup>[banned]</sup> |
| 1761 | |
| 1762 | ```c++ |
| 1763 | absl::Span |
| 1764 | ``` |
| 1765 | |
| 1766 | **Description:** Early adaptation of C++20 `std::span`. |
| 1767 | |
| 1768 | **Documentation:** [Using absl::Span](https://abseil.io/tips/93) |
| 1769 | |
| 1770 | **Notes:** |
| 1771 | *** promo |
| 1772 | Banned due to being less std::-compliant than `base::span`. Keep using |
| 1773 | `base::span`. |
| 1774 | *** |
| 1775 | |
| 1776 | ### StatusOr <sup>[banned]</sup> |
| 1777 | |
| 1778 | ```c++ |
| 1779 | absl::StatusOr<T> |
| 1780 | ``` |
| 1781 | |
| 1782 | **Description:** An object that is either a usable value, or an error Status |
| 1783 | explaining why such a value is not present. |
| 1784 | |
| 1785 | **Documentation:** |
| 1786 | [statusor.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/status/statusor.h) |
| 1787 | |
| 1788 | **Notes:** |
| 1789 | *** promo |
| 1790 | Banned due to overlap with `base::expected`. Use `base::expected` instead. |
| 1791 | *** |
| 1792 | |
| 1793 | ### String Formatting <sup>[banned]</sup> |
| 1794 | |
| 1795 | ```c++ |
| 1796 | absl::StrFormat |
| 1797 | ``` |
| 1798 | |
| 1799 | **Description:** A typesafe replacement for the family of printf() string |
| 1800 | formatting routines. |
| 1801 | |
| 1802 | **Documentation:** |
| 1803 | [String Formatting](https://abseil.io/docs/cpp/guides/format) |
| 1804 | |
| 1805 | **Notes:** |
| 1806 | *** promo |
| 1807 | Banned for now due to overlap with `base::StringPrintf()`. See |
| 1808 | [migration bug](https://bugs.chromium.org/p/chromium/issues/detail?id=1371963). |
| 1809 | *** |
| 1810 | |
| 1811 | ### string_view <sup>[banned]</sup> |
| 1812 | |
| 1813 | ```c++ |
| 1814 | absl::string_view |
| 1815 | ``` |
| 1816 | |
| 1817 | **Description:** Early adaptation of C++17 `std::string_view`. |
| 1818 | |
| 1819 | **Documentation:** [absl::string_view](https://abseil.io/tips/1) |
| 1820 | |
| 1821 | **Notes:** |
| 1822 | *** promo |
| 1823 | Banned due to only working with 8-bit characters. Keep using |
| 1824 | `base::StringPiece` from `base/strings/`. |
| 1825 | *** |
| 1826 | |
| 1827 | ### Strings Library <sup>[banned]</sup> |
| 1828 | |
| 1829 | ```c++ |
| 1830 | absl::StrSplit |
| 1831 | absl::StrJoin |
| 1832 | absl::StrCat |
| 1833 | absl::StrAppend |
| 1834 | absl::Substitute |
| 1835 | absl::StrContains |
| 1836 | ``` |
| 1837 | |
| 1838 | **Description:** Classes and utility functions for manipulating and comparing |
| 1839 | strings. |
| 1840 | |
| 1841 | **Documentation:** |
| 1842 | [String Utilities](https://abseil.io/docs/cpp/guides/strings) |
| 1843 | |
| 1844 | **Notes:** |
| 1845 | *** promo |
| 1846 | Banned for now due to overlap with `base/strings`. We |
| 1847 | [should re-evalute](https://bugs.chromium.org/p/chromium/issues/detail?id=1371966) |
| 1848 | when we've |
| 1849 | [migrated](https://bugs.chromium.org/p/chromium/issues/detail?id=691162) from |
| 1850 | `base::StringPiece` to `std::string_view`. |
| 1851 | *** |
| 1852 | |
| 1853 | ### Synchronization <sup>[banned]</sup> |
| 1854 | |
| 1855 | ```c++ |
| 1856 | absl::Mutex |
| 1857 | ``` |
| 1858 | |
| 1859 | **Description:** Primitives for managing tasks across different threads. |
| 1860 | |
| 1861 | **Documentation:** |
| 1862 | [Synchronization](https://abseil.io/docs/cpp/guides/synchronization) |
| 1863 | |
| 1864 | **Notes:** |
| 1865 | *** promo |
| 1866 | Banned due to overlap with `base/synchronization/`. We would love |
| 1867 | [more testing](https://bugs.chromium.org/p/chromium/issues/detail?id=1371969) on |
| 1868 | whether there are compelling reasons to prefer base, absl, or std |
| 1869 | synchronization primitives; for now, use `base/synchronization/`. |
| 1870 | *** |
| 1871 | |
| 1872 | ### Time library <sup>[banned]</sup> |
| 1873 | |
| 1874 | ```c++ |
| 1875 | absl::Duration |
| 1876 | absl::Time |
| 1877 | absl::TimeZone |
| 1878 | absl::CivilDay |
| 1879 | ``` |
| 1880 | |
| 1881 | **Description:** Abstractions for holding time values, both in terms of |
| 1882 | absolute time and civil time. |
| 1883 | |
| 1884 | **Documentation:** [Time](https://abseil.io/docs/cpp/guides/time) |
| 1885 | |
| 1886 | **Notes:** |
| 1887 | *** promo |
| 1888 | Banned due to overlap with `base/time/`. Use `base/time/` instead. |
| 1889 | *** |
| 1890 | |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1891 | ## Abseil TBD Features {#absl-review} |
| 1892 | |
| 1893 | The following Abseil library features are not allowed in the Chromium codebase. |
| 1894 | See the top of this page on how to propose moving a feature from this list into |
| 1895 | the allowed or banned sections. |
| 1896 | |
Danil Chapovalov | a9f2703 | 2022-06-20 16:56:14 | [diff] [blame] | 1897 | ### AnyInvocable <sup>[tbd]</sup> |
| 1898 | |
| 1899 | ```c++ |
| 1900 | absl::AnyInvocable |
| 1901 | ``` |
| 1902 | |
| 1903 | **Description:** An equivalent of the C++23 std::move_only_function. |
| 1904 | |
| 1905 | **Documentation:** |
| 1906 | * [any_invocable.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/any_invocable.h) |
| 1907 | * [std::move_only_function](https://en.cppreference.com/w/cpp/utility/functional/move_only_function/move_only_function) |
| 1908 | |
| 1909 | **Notes:** |
| 1910 | *** promo |
| 1911 | Overlaps with `base::RepeatingCallback`, `base::OnceCallback`. |
| 1912 | *** |
| 1913 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 1914 | ### Cleanup <sup>[tbd]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1915 | |
| 1916 | ```c++ |
| 1917 | FILE* sink_file = fopen(sink_path, "w"); |
| 1918 | auto sink_closer = absl::MakeCleanup([sink_file] { fclose(sink_file); }); |
| 1919 | ``` |
| 1920 | |
| 1921 | **Description:** Implements the scope guard idiom, invoking the contained |
| 1922 | callback's `operator()() &&` on scope exit. |
| 1923 | |
| 1924 | **Documentation:** |
| 1925 | [cleanup.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/cleanup/cleanup.h) |
| 1926 | |
| 1927 | **Notes:** |
| 1928 | *** promo |
| 1929 | Similar to `defer` in Golang. |
| 1930 | *** |
| 1931 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 1932 | ### Containers <sup>[tbd]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1933 | |
| 1934 | ```c++ |
| 1935 | absl::flat_hash_map |
| 1936 | absl::flat_hash_set |
| 1937 | absl::node_hash_map |
| 1938 | absl::node_hash_set |
| 1939 | absl::btree_map |
| 1940 | absl::btree_set |
| 1941 | absl::btree_multimap |
| 1942 | absl::btree_multiset |
| 1943 | absl::InlinedVector |
| 1944 | absl::FixedArray |
| 1945 | ``` |
| 1946 | |
| 1947 | **Description:** Alternatives to STL containers designed to be more efficient |
| 1948 | in the general case. |
| 1949 | |
| 1950 | **Documentation:** |
| 1951 | * [Containers](https://abseil.io/docs/cpp/guides/container) |
| 1952 | * [Hash](https://abseil.io/docs/cpp/guides/hash) |
| 1953 | |
| 1954 | **Notes:** |
| 1955 | *** promo |
| 1956 | Supplements `base/containers/`. |
| 1957 | *** |
| 1958 | |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame^] | 1959 | ### Log macros and related classes <sup>[tbd]</sup> |
Danil Chapovalov | 6719fb1 | 2022-08-31 13:52:49 | [diff] [blame] | 1960 | |
| 1961 | ```c++ |
| 1962 | LOG(INFO) << message; |
| 1963 | CHECK(condition); |
| 1964 | absl::AddLogSink(&custom_sink_to_capture_absl_logs); |
| 1965 | ``` |
| 1966 | |
| 1967 | **Description:** Macros and related classes to perform debug loggings |
| 1968 | |
| 1969 | **Documentation:** |
| 1970 | [log.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/log.h) |
| 1971 | [check.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/check.h) |
| 1972 | |
| 1973 | **Notes:** |
| 1974 | *** promo |
| 1975 | Overlaps and uses same macros names as `base/logging.h`. |
| 1976 | *** |