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_ |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 37 | * **Abseil:** Initially supported July 31, 2020; see allowed/banned/TBD |
| 38 | features below |
| 39 | * absl::StatusOr: Initially supported September 3, 2020 |
| 40 | * absl::Cleanup: Initially supported February 4, 2021 |
Danil Chapovalov | a9f2703 | 2022-06-20 16:56:14 | [diff] [blame] | 41 | * absl::AnyInvocable: Initially supported June 20, 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 | |
Daniel Cheng | 4ddc920 | 2022-02-24 20:19:26 | [diff] [blame] | 686 | ### Non-member std::size/std::empty/std::data <sup>[allowed]</sup> |
| 687 | |
| 688 | ```c++ |
| 689 | char buffer[260]; |
| 690 | memcpy(std::data(buffer), source_str.data(), std::size(buffer)); |
| 691 | |
| 692 | if (!std::empty(container)) { ... } |
| 693 | ``` |
| 694 | |
| 695 | **Description:** Non-member versions of what are often member functions on STL |
| 696 | containers. Primarily useful when: |
| 697 | - using `std::size()` as a replacement for the old `arraysize()` macro. |
| 698 | - writing code that needs to generically operate across things like |
| 699 | `std::vector` and `std::list` (which provide `size()`, `empty()`, and `data() |
| 700 | member functions), `std::array` and `std::initialize_list` (which only provide |
| 701 | a subset of the aforementioned member functions), and regular arrays (which |
| 702 | have no member functions at all). |
| 703 | |
| 704 | **Documentation:** |
| 705 | [std::size](https://en.cppreference.com/w/cpp/iterator/size), |
| 706 | [std::empty](https://en.cppreference.com/w/cpp/iterator/empty), |
| 707 | [std::data](https://en.cppreference.com/w/cpp/iterator/data) |
| 708 | |
| 709 | **Notes:** |
| 710 | *** promo |
| 711 | [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/58qlA3zk5ZI/m/7kKok65xAAAJ) |
| 712 | |
| 713 | Prefer range-based for loops over `std::size()`: range-based for loops work even |
| 714 | for regular arrays. |
| 715 | *** |
| 716 | |
Avi Drissman | 67c3b1e | 2022-04-27 20:42:49 | [diff] [blame] | 717 | ### std::is_invocable <sup>[allowed]</sup> |
| 718 | |
| 719 | ```c++ |
| 720 | std::is_invocable_v<Fn, 1, "Hello"> |
| 721 | ``` |
| 722 | |
| 723 | **Description:** Checks whether a function may be invoked with the given |
| 724 | argument types. The `_r` variant also evaluates whether the result is |
| 725 | convertible to a given type. |
| 726 | |
| 727 | **Documentation:** |
| 728 | [std::is_invocable](https://en.cppreference.com/w/cpp/types/is_invocable) |
| 729 | |
| 730 | **Notes:** |
| 731 | *** promo |
| 732 | [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/YhlF_sTDSc0/m/QMzf42BtAAAJ) |
| 733 | *** |
| 734 | |
Avi Drissman | c109efd | 2022-04-27 22:03:35 | [diff] [blame] | 735 | ### std::conjunction/std::disjunction/std::negation <sup>[allowed]</sup> |
| 736 | |
| 737 | ```c++ |
| 738 | template<typename T, typename... Ts> |
| 739 | std::enable_if_t<std::conjunction_v<std::is_same<T, Ts>...>> |
| 740 | func(T, Ts...) { ... |
| 741 | ``` |
| 742 | |
| 743 | **Description:** Performs logical operations on type traits. |
| 744 | |
| 745 | **Documentation:** |
| 746 | [std::conjunction](https://en.cppreference.com/w/cpp/types/conjunction), |
| 747 | [std::disjunction](https://en.cppreference.com/w/cpp/types/disjunction), |
| 748 | [std::negation](https://en.cppreference.com/w/cpp/types/negation) |
| 749 | |
| 750 | **Notes:** |
| 751 | *** promo |
| 752 | [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/YhlF_sTDSc0/m/QMzf42BtAAAJ) |
| 753 | *** |
| 754 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 755 | ## C++17 Banned Library Features {#library-blocklist-17} |
| 756 | |
| 757 | The following C++17 library features are not allowed in the Chromium codebase. |
| 758 | |
Daniel Cheng | c05fcc6 | 2022-01-12 16:54:29 | [diff] [blame] | 759 | ### std::any <sup>[banned]</sup> |
| 760 | |
| 761 | ```c++ |
| 762 | std::any x = 5; |
| 763 | ``` |
| 764 | |
| 765 | **Description:** A type-safe container for single values of any type. |
| 766 | |
| 767 | **Documentation:** |
| 768 | [std::any](https://en.cppreference.com/w/cpp/utility/any) |
| 769 | |
| 770 | **Notes:** |
| 771 | *** promo |
| 772 | [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/KEa-0AOGRNY/m/IV_S3_pvAAAJ) |
| 773 | |
| 774 | Banned since workaround for lack of RTTI isn't compatible with the component |
| 775 | build ([Bug](https://crbug.com/1096380)). Also see `absl::any`. |
| 776 | *** |
| 777 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 778 | ### std::filesystem <sup>[banned]</sup> |
| 779 | |
| 780 | ```c++ |
| 781 | #include <filesystem> |
| 782 | ``` |
| 783 | |
| 784 | **Description:** A standard way to manipulate files, directories, and paths in a |
| 785 | filesystem. |
| 786 | |
| 787 | **Documentation:** |
| 788 | [Filesystem library](https://en.cppreference.com/w/cpp/filesystem) |
| 789 | |
| 790 | **Notes:** |
| 791 | *** promo |
| 792 | Banned by the [Google Style Guide](https://google.github.io/styleguide/cppguide.html#Other_Features). |
| 793 | *** |
| 794 | |
| 795 | ### weak_from_this <sup>[banned]</sup> |
| 796 | |
| 797 | ```c++ |
| 798 | auto weak_ptr = weak_from_this(); |
| 799 | ``` |
| 800 | |
| 801 | **Description:** Returns a `std::weak_ptr<T>` that tracks ownership of `*this` |
| 802 | by all existing `std::shared_ptr`s that refer to `*this`. |
| 803 | |
| 804 | **Documentation:** |
| 805 | [std::enable_shared_from_this<T>::weak_from_this](https://en.cppreference.com/w/cpp/memory/enable_shared_from_this/weak_from_this) |
| 806 | |
| 807 | **Notes:** |
| 808 | *** promo |
| 809 | Banned since `std::shared_ptr` and `std::weak_ptr` are banned. |
| 810 | *** |
| 811 | |
| 812 | ### Transparent std::owner_less <sup>[banned]</sup> |
| 813 | |
| 814 | ```c++ |
| 815 | std::map<std::weak_ptr<T>, U, std::owner_less<>> |
| 816 | ``` |
| 817 | |
| 818 | **Description:** Function object providing mixed-type owner-based ordering of |
| 819 | shared and weak pointers, regardless of the type of the pointee. |
| 820 | |
| 821 | **Documentation:** |
| 822 | [std::owner_less](https://en.cppreference.com/w/cpp/memory/owner_less) |
| 823 | |
| 824 | **Notes:** |
| 825 | *** promo |
| 826 | Banned since `std::shared_ptr` and `std::weak_ptr` are banned. |
| 827 | *** |
| 828 | |
| 829 | ### Array support for std::shared_ptr <sup>[banned]</sup> |
| 830 | |
| 831 | ```c++ |
| 832 | std::shared_ptr<int[]> p(new int[10]{0,1,2,3,4,5,6,7,8,9}); |
| 833 | std::cout << p[3]; // "3" |
| 834 | ``` |
| 835 | |
| 836 | **Description:** Supports memory management of arrays via `std::shared_ptr`. |
| 837 | |
| 838 | **Documentation:** |
| 839 | [std::shared_ptr](https://en.cppreference.com/w/cpp/memory/shared_ptr) |
| 840 | |
| 841 | **Notes:** |
| 842 | *** promo |
| 843 | Banned since `std::shared_ptr` is banned. |
| 844 | *** |
| 845 | |
| 846 | ### std::uncaught_exceptions <sup>[banned]</sup> |
| 847 | |
| 848 | ```c++ |
| 849 | int count = std::uncaught_exceptions(); |
| 850 | ``` |
| 851 | |
| 852 | **Description:** Determines whether there are live exception objects. |
| 853 | |
| 854 | **Documentation:** |
| 855 | [std::uncaught_exceptions](https://en.cppreference.com/w/cpp/error/uncaught_exception) |
| 856 | |
| 857 | **Notes:** |
| 858 | *** promo |
| 859 | Banned because exceptions are banned. |
| 860 | *** |
| 861 | |
| 862 | ### Rounding functions for duration and time_point <sup>[banned]</sup> |
| 863 | |
| 864 | ```c++ |
| 865 | std::chrono::ceil<std::chrono::seconds>(dur); |
| 866 | std::chrono::ceil<std::chrono::seconds>(time_pt); |
| 867 | std::chrono::floor<std::chrono::seconds>(dur); |
| 868 | std::chrono::floor<std::chrono::seconds>(time_pt); |
| 869 | std::chrono::round<std::chrono::seconds>(dur); |
| 870 | std::chrono::round<std::chrono::seconds>(time_pt); |
| 871 | ``` |
| 872 | |
| 873 | **Description:** Converts durations and time_points by rounding. |
| 874 | |
| 875 | **Documentation:** |
| 876 | [std::chrono::duration](https://en.cppreference.com/w/cpp/chrono/duration), |
| 877 | [std::chrono::time_point](https://en.cppreference.com/w/cpp/chrono/time_point) |
| 878 | |
| 879 | **Notes:** |
| 880 | *** promo |
| 881 | Banned since `std::chrono` is banned. |
| 882 | *** |
| 883 | |
Avi Drissman | 37b7d51 | 2022-04-01 22:01:40 | [diff] [blame] | 884 | ### std::variant <sup>[banned]</sup> |
| 885 | |
| 886 | ```c++ |
| 887 | std::variant<int, double> v = 12; |
| 888 | ``` |
| 889 | |
| 890 | **Description:** The class template `std::variant` represents a type-safe |
| 891 | `union`. An instance of `std::variant` at any given time holds a value of one of |
| 892 | its alternative types (it's also possible for it to be valueless). |
| 893 | |
| 894 | **Documentation:** |
| 895 | [std::variant](https://en.cppreference.com/w/cpp/utility/variant) |
| 896 | |
| 897 | **Notes:** |
| 898 | *** promo |
| 899 | Banned for now because it does not provide safety guarantees in the case of |
| 900 | misuse. The Chromium C++ team is investigating the possibility of hardening the |
| 901 | C++ library so that the standard version can be used. In the meanwhile, use |
| 902 | `absl::variant` instead. |
| 903 | *** |
| 904 | |
| 905 | ### std::optional <sup>[banned]</sup> |
| 906 | |
| 907 | ```c++ |
| 908 | std::optional<std::string> s; |
| 909 | ``` |
| 910 | |
| 911 | **Description:** The class template `std::optional` manages an optional |
| 912 | contained value, i.e. a value that may or may not be present. A common use case |
| 913 | for optional is the return value of a function that may fail. |
| 914 | |
| 915 | **Documentation:** |
| 916 | [std::optional](https://en.cppreference.com/w/cpp/utility/optional) |
| 917 | |
| 918 | **Notes:** |
| 919 | *** promo |
| 920 | Banned for now because it does not provide safety guarantees in the case of |
| 921 | misuse. The Chromium C++ team is investigating the possibility of hardening the |
| 922 | C++ library so that the standard version can be used. In the meanwhile, use |
| 923 | `absl::optional` instead. |
| 924 | *** |
| 925 | |
Avi Drissman | bc6545f4 | 2022-05-03 17:47:38 | [diff] [blame] | 926 | ### std::in_place/in_place_type/in_place_index/in_place_t/in_place_type_t/in_place_index_t <sup>[banned]</sup> |
| 927 | |
| 928 | ```c++ |
| 929 | std::optional<std::complex<double>> opt{std::in_place, 0, 1}; |
| 930 | std::variant<int, float> v{std::in_place_type<int>, 1.4}; |
| 931 | ``` |
| 932 | |
| 933 | **Description:** The `std::in_place` are disambiguation tags for |
| 934 | `std::optional`, `std::variant`, and `std::any` to indicate that the object |
| 935 | should be constructed in-place. |
| 936 | |
| 937 | **Documentation:** |
| 938 | [std::in_place](https://en.cppreference.com/w/cpp/utility/in_place) |
| 939 | |
| 940 | **Notes:** |
| 941 | *** promo |
| 942 | Banned for now because `std::optional`, `std::variant`, and `std::any` are all |
| 943 | banned for now. Because `absl::optional` and `absl::variant` are used instead, |
| 944 | and they require `absl::in_place`, use `absl::in_place` for non-Abseil Chromium |
| 945 | code. See the |
| 946 | [discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ZspmuJPpv6s/m/wYYTCiRwAAAJ). |
| 947 | *** |
| 948 | |
Avi Drissman | 37b7d51 | 2022-04-01 22:01:40 | [diff] [blame] | 949 | ### std::clamp <sup>[banned]</sup> |
| 950 | |
| 951 | ```c++ |
| 952 | int x = std::clamp(inp, 0, 100); |
| 953 | ``` |
| 954 | |
| 955 | **Description:** Clamps a value between a minimum and a maximum. |
| 956 | |
| 957 | **Documentation:** |
| 958 | [std::clamp](https://en.cppreference.com/w/cpp/algorithm/clamp) |
| 959 | |
| 960 | **Notes:** |
| 961 | *** promo |
| 962 | Banned for now because it does not provide safety guarantees in the case of |
| 963 | misuse. The Chromium C++ team is investigating the possibility of hardening the |
| 964 | C++ library so that the standard version can be used. In the meanwhile, use |
| 965 | `base::clamp` instead. |
| 966 | *** |
| 967 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 968 | ## C++17 TBD Language Features {#core-review-17} |
| 969 | |
| 970 | The following C++17 language features are not allowed in the Chromium codebase. |
| 971 | See the top of this page on how to propose moving a feature from this list into |
| 972 | the allowed or banned sections. |
| 973 | |
| 974 | ### Declaring non-type template parameters with auto <sup>[tbd]</sup> |
| 975 | |
| 976 | ```c++ |
| 977 | template <auto... seq> |
| 978 | struct my_integer_sequence { |
| 979 | // ... |
| 980 | }; |
| 981 | auto seq = my_integer_sequence<0, 1, 2>(); // Type deduced to be `int`. |
| 982 | ``` |
| 983 | |
| 984 | **Description:** Following the deduction rules of `auto`, while respecting the |
| 985 | non-type template parameter list of allowable types, template arguments can be |
| 986 | deduced from the types of its arguments. |
| 987 | |
| 988 | **Documentation:** |
| 989 | [Template parameters](https://en.cppreference.com/w/cpp/language/template_parameters) |
| 990 | |
| 991 | **Notes:** |
| 992 | *** promo |
| 993 | None |
| 994 | *** |
| 995 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 996 | ### constexpr lambda <sup>[tbd]</sup> |
| 997 | |
| 998 | ```c++ |
| 999 | auto identity = [](int n) constexpr { return n; }; |
| 1000 | static_assert(identity(123) == 123); |
| 1001 | ``` |
| 1002 | |
| 1003 | **Description:** Compile-time lambdas using constexpr. |
| 1004 | |
| 1005 | **Documentation:** |
| 1006 | [Lambda expressions](https://en.cppreference.com/w/cpp/language/lambda) |
| 1007 | |
| 1008 | **Notes:** |
| 1009 | *** promo |
| 1010 | None |
| 1011 | *** |
| 1012 | |
| 1013 | ### Lambda capture this by value <sup>[tbd]</sup> |
| 1014 | |
| 1015 | ```c++ |
| 1016 | const auto l = [*this] { return member_; } |
| 1017 | ``` |
| 1018 | |
| 1019 | **Description:** `*this` captures the current object by copy, while `this` |
| 1020 | continues to capture by reference. |
| 1021 | |
| 1022 | **Documentation:** |
| 1023 | [Lambda capture](https://en.cppreference.com/w/cpp/language/lambda#Lambda_capture) |
| 1024 | |
| 1025 | **Notes:** |
| 1026 | *** promo |
| 1027 | None |
| 1028 | *** |
| 1029 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 1030 | ### UTF-8 character literals <sup>[tbd]</sup> |
| 1031 | |
| 1032 | ```c++ |
| 1033 | char x = u8'x'; |
| 1034 | ``` |
| 1035 | |
| 1036 | **Description:** A character literal that begins with `u8` is a character |
| 1037 | literal of type `char`. The value of a UTF-8 character literal is equal to its |
| 1038 | ISO 10646 code point value. |
| 1039 | |
| 1040 | **Documentation:** |
| 1041 | [Character literal](https://en.cppreference.com/w/cpp/language/character_literal) |
| 1042 | |
| 1043 | **Notes:** |
| 1044 | *** promo |
| 1045 | C++20 changes the type to `char8_t`, causing migration hazards for code using |
| 1046 | this. |
| 1047 | *** |
| 1048 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 1049 | ### using declaration for attributes <sup>[tbd]</sup> |
| 1050 | |
| 1051 | ```c++ |
| 1052 | [[using CC: opt(1), debug]] // same as [[CC:opt(1), CC::debug]] |
| 1053 | ``` |
| 1054 | |
| 1055 | **Description:** Specifies a common namespace for a list of attributes. |
| 1056 | |
| 1057 | **Documentation:** |
| 1058 | [Attribute specifier sequence](https://en.cppreference.com/w/cpp/language/attributes) |
| 1059 | |
| 1060 | **Notes:** |
| 1061 | *** promo |
| 1062 | See similar attribute macros in base/compiler_specific.h. |
| 1063 | *** |
| 1064 | |
| 1065 | ### __has_include <sup>[tbd]</sup> |
| 1066 | |
| 1067 | ```c++ |
| 1068 | #if __has_include(<optional>) ... |
| 1069 | ``` |
| 1070 | |
| 1071 | **Description:** Checks whether a file is available for inclusion, i.e. the file |
| 1072 | exists. |
| 1073 | |
| 1074 | **Documentation:** |
| 1075 | [Source file inclusion](https://en.cppreference.com/w/cpp/preprocessor/include) |
| 1076 | |
| 1077 | **Notes:** |
| 1078 | *** promo |
| 1079 | None |
| 1080 | *** |
| 1081 | |
| 1082 | ## C++17 TBD Library Features {#library-review-17} |
| 1083 | |
| 1084 | The following C++17 library features are not allowed in the Chromium codebase. |
| 1085 | See the top of this page on how to propose moving a feature from this list into |
| 1086 | the allowed or banned sections. |
| 1087 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 1088 | ### std::string_view <sup>[tbd]</sup> |
| 1089 | |
| 1090 | ```c++ |
| 1091 | std::string_view str = "foo"; |
| 1092 | ``` |
| 1093 | |
| 1094 | **Description:** A non-owning reference to a string. Useful for providing an |
| 1095 | abstraction on top of strings (e.g. for parsing). |
| 1096 | |
| 1097 | **Documentation:** |
| 1098 | [std::basic_string_view](https://en.cppreference.com/w/cpp/string/basic_string_view) |
| 1099 | |
| 1100 | **Notes:** |
| 1101 | *** promo |
| 1102 | See also `absl::string_view` and `base::StringPiece`. |
| 1103 | *** |
| 1104 | |
| 1105 | ### std::invoke <sup>[tbd]</sup> |
| 1106 | |
| 1107 | ```c++ |
| 1108 | static_assert(std::invoke(std::plus<>(), 1, 2) == 3); |
| 1109 | ``` |
| 1110 | |
| 1111 | **Description:** Invokes a `Callable` object with parameters. An example of a |
| 1112 | `Callable` object is `base::Callback` where an object can be called similarly to |
| 1113 | a regular function. |
| 1114 | |
| 1115 | **Documentation:** |
| 1116 | [std::invoke](https://en.cppreference.com/w/cpp/utility/functional/invoke) |
| 1117 | |
| 1118 | **Notes:** |
| 1119 | *** promo |
| 1120 | See also `base::invoke`. |
| 1121 | *** |
| 1122 | |
| 1123 | ### std::apply <sup>[tbd]</sup> |
| 1124 | |
| 1125 | ```c++ |
| 1126 | static_assert(std::apply(std::plus<>(), std::make_tuple(1, 2)) == 3); |
| 1127 | ``` |
| 1128 | |
| 1129 | **Description:** Invokes a `Callable` object with a tuple of arguments. |
| 1130 | |
| 1131 | **Documentation:** |
| 1132 | [std::apply](https://en.cppreference.com/w/cpp/utility/apply) |
| 1133 | |
| 1134 | **Notes:** |
| 1135 | *** promo |
| 1136 | See also `absl::apply` and `base::apply`. |
| 1137 | *** |
| 1138 | |
| 1139 | ### std::byte <sup>[tbd]</sup> |
| 1140 | |
| 1141 | ```c++ |
| 1142 | std::byte b = 0xFF; |
| 1143 | int i = std::to_integer<int>(b); // 0xFF |
| 1144 | ``` |
| 1145 | |
| 1146 | **Description:** A standard way of representing data as a byte. `std::byte` is |
| 1147 | neither a character type nor an arithmetic type, and the only operator overloads |
| 1148 | available are bitwise operations. |
| 1149 | |
| 1150 | **Documentation:** |
| 1151 | [std::byte](https://en.cppreference.com/w/cpp/types/byte) |
| 1152 | |
| 1153 | **Notes:** |
| 1154 | *** promo |
| 1155 | None |
| 1156 | *** |
| 1157 | |
| 1158 | ### Splicing for maps and sets <sup>[tbd]</sup> |
| 1159 | |
| 1160 | ```c++ |
| 1161 | std::map<...>::extract |
| 1162 | std::map<...>::merge |
| 1163 | std::set<...>::extract |
| 1164 | std::set<...>::merge |
| 1165 | ``` |
| 1166 | |
| 1167 | **Description:** Moving nodes and merging containers without the overhead of |
| 1168 | expensive copies, moves, or heap allocations/deallocations. |
| 1169 | |
| 1170 | **Documentation:** |
| 1171 | [std::map::extract](https://en.cppreference.com/w/cpp/container/map/extract), |
| 1172 | [std::map::merge](https://en.cppreference.com/w/cpp/container/map/merge) |
| 1173 | |
| 1174 | **Notes:** |
| 1175 | *** promo |
| 1176 | None |
| 1177 | *** |
| 1178 | |
| 1179 | ### Parallel algorithms <sup>[tbd]</sup> |
| 1180 | |
| 1181 | ```c++ |
| 1182 | auto it = std::find(std::execution::par, std::begin(vec), std::end(vec), 2); |
| 1183 | ``` |
| 1184 | |
| 1185 | **Description:** Many of the STL algorithms, such as the `copy`, `find` and |
| 1186 | `sort` methods, now support the parallel execution policies: `seq`, `par`, and |
| 1187 | `par_unseq` which translate to "sequentially", "parallel" and |
| 1188 | "parallel unsequenced". |
| 1189 | |
| 1190 | **Documentation:** |
| 1191 | [execution_policy_tag_t](https://en.cppreference.com/w/cpp/algorithm/execution_policy_tag_t) |
| 1192 | |
| 1193 | **Notes:** |
| 1194 | *** promo |
| 1195 | None |
| 1196 | *** |
| 1197 | |
| 1198 | ### std::make_from_tuple <sup>[tbd]</sup> |
| 1199 | |
| 1200 | ```c++ |
| 1201 | // Calls Foo(int, double): |
| 1202 | auto foo = std::make_from_tuple<Foo>(std::make_tuple(1, 3.5)); |
| 1203 | ``` |
| 1204 | |
| 1205 | **Description:** Constructs an object from a tuple of arguments. |
| 1206 | |
| 1207 | **Documentation:** |
| 1208 | [std::make_from_tuple](https://en.cppreference.com/w/cpp/utility/make_from_tuple) |
| 1209 | |
| 1210 | **Notes:** |
| 1211 | *** promo |
| 1212 | See also `absl::make_from_tuple`. |
| 1213 | *** |
| 1214 | |
| 1215 | ### Searchers <sup>[tbd]</sup> |
| 1216 | |
| 1217 | ```c++ |
| 1218 | auto it = std::search(haystack.begin(), haystack.end(), |
| 1219 | std::boyer_moore_searcher(needle.begin(), needle.end())); |
| 1220 | ``` |
| 1221 | |
| 1222 | **Description:** Alternate string searching algorithms. |
| 1223 | |
| 1224 | **Documentation:** |
| 1225 | [Searchers](https://en.cppreference.com/w/cpp/utility/functional#Searchers) |
| 1226 | |
| 1227 | **Notes:** |
| 1228 | *** promo |
| 1229 | None |
| 1230 | *** |
| 1231 | |
| 1232 | ### std::as_const <sup>[tbd]</sup> |
| 1233 | |
| 1234 | ```c++ |
| 1235 | auto&& const_ref = std::as_const(mutable_obj); |
| 1236 | ``` |
| 1237 | |
| 1238 | **Description:** Forms reference to const T. |
| 1239 | |
| 1240 | **Documentation:** |
| 1241 | [std::as_const](https://en.cppreference.com/w/cpp/utility/as_const) |
| 1242 | |
| 1243 | **Notes:** |
| 1244 | *** promo |
| 1245 | See also `base::as_const`. |
| 1246 | *** |
| 1247 | |
| 1248 | ### std::not_fn <sup>[tbd]</sup> |
| 1249 | |
| 1250 | ```c++ |
| 1251 | auto nonwhite = std::find_if(str.begin(), str.end(), std::not_fn(IsWhitespace)); |
| 1252 | ``` |
| 1253 | |
| 1254 | **Description:** Creates a forwarding call wrapper that returns the negation of |
| 1255 | the callable object it holds. |
| 1256 | |
| 1257 | **Documentation:** |
| 1258 | [std::not_fn](https://en.cppreference.com/w/cpp/utility/functional/not_fn) |
| 1259 | |
| 1260 | **Notes:** |
| 1261 | *** promo |
| 1262 | See also `base::not_fn`. |
| 1263 | *** |
| 1264 | |
| 1265 | ### Uninitialized memory algorithms <sup>[tbd]</sup> |
| 1266 | |
| 1267 | ```c++ |
| 1268 | std::destroy_at(ptr); |
| 1269 | std::destroy(ptr, ptr + 8); |
| 1270 | std::destroy_n(ptr, 8); |
| 1271 | std::uninitialized_move(src.begin(), src.end(), dest.begin()); |
| 1272 | std::uninitialized_value_construct(std::begin(storage), std::end(storage)); |
| 1273 | ``` |
| 1274 | |
| 1275 | **Description:** Replaces direct constructor and destructor calls when manually |
| 1276 | managing memory. |
| 1277 | |
| 1278 | **Documentation:** |
| 1279 | [std::destroy_at](https://en.cppreference.com/w/cpp/memory/destroy_at), |
| 1280 | [std::destroy](https://en.cppreference.com/w/cpp/memory/destroy), |
| 1281 | [std::destroy_n](https://en.cppreference.com/w/cpp/memory/destroy_n), |
| 1282 | [std::uninitialized_move](https://en.cppreference.com/w/cpp/memory/uninitialized_move), |
| 1283 | [std::uninitialized_value_construct](https://en.cppreference.com/w/cpp/memory/uninitialized_value_construct) |
| 1284 | |
| 1285 | **Notes:** |
| 1286 | *** promo |
| 1287 | None |
| 1288 | *** |
| 1289 | |
| 1290 | ### std::pmr::memory_resource and std::polymorphic_allocator <sup>[tbd]</sup> |
| 1291 | |
| 1292 | ```c++ |
| 1293 | #include <memory_resource> |
| 1294 | ``` |
| 1295 | |
| 1296 | **Description:** Manages memory allocations using runtime polymorphism. |
| 1297 | |
| 1298 | **Documentation:** |
| 1299 | [std::pmr::memory_resource](https://en.cppreference.com/w/cpp/memory/memory_resource), |
| 1300 | [std::pmr::polymorphic_allocator](https://en.cppreference.com/w/cpp/memory/polymorphic_allocator), |
| 1301 | |
| 1302 | **Notes:** |
| 1303 | *** promo |
| 1304 | May not be supported in libc++, according to the |
| 1305 | [library features table](https://en.cppreference.com/w/cpp/17) |
| 1306 | *** |
| 1307 | |
| 1308 | ### std::aligned_alloc <sup>[tbd]</sup> |
| 1309 | |
| 1310 | ```c++ |
| 1311 | int* p2 = static_cast<int*>(std::aligned_alloc(1024, 1024)); |
| 1312 | ``` |
| 1313 | |
| 1314 | **Description:** Allocates uninitialized storage with the specified alignment. |
| 1315 | |
| 1316 | **Documentation:** |
| 1317 | [std::aligned_alloc](https://en.cppreference.com/w/cpp/memory/c/aligned_alloc) |
| 1318 | |
| 1319 | **Notes:** |
| 1320 | *** promo |
| 1321 | None |
| 1322 | *** |
| 1323 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 1324 | ### std::is_swappable <sup>[tbd]</sup> |
| 1325 | |
| 1326 | ```c++ |
| 1327 | std::is_swappable<T> |
| 1328 | std::is_swappable_with_v<T, U> |
| 1329 | ``` |
| 1330 | |
| 1331 | **Description:** Checks whether classes may be swapped. |
| 1332 | |
| 1333 | **Documentation:** |
| 1334 | [std::is_swappable](https://en.cppreference.com/w/cpp/types/is_swappable) |
| 1335 | |
| 1336 | **Notes:** |
| 1337 | *** promo |
| 1338 | None |
| 1339 | *** |
| 1340 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 1341 | ### std::is_aggregate <sup>[tbd]</sup> |
| 1342 | |
| 1343 | ```c++ |
| 1344 | if constexpr(std::is_aggregate_v<T>) { ... |
| 1345 | ``` |
| 1346 | |
| 1347 | **Description:** Checks wither the given type is an aggregate type. |
| 1348 | |
| 1349 | **Documentation:** |
| 1350 | [std::is_aggregate](https://en.cppreference.com/w/cpp/types/is_aggregate) |
| 1351 | |
| 1352 | **Notes:** |
| 1353 | *** promo |
| 1354 | None |
| 1355 | *** |
| 1356 | |
| 1357 | ### std::has_unique_object_representations <sup>[tbd]</sup> |
| 1358 | |
| 1359 | ```c++ |
| 1360 | std::has_unique_object_representations_v<foo> |
| 1361 | ``` |
| 1362 | |
| 1363 | **Description:** Checks wither the given type is trivially copyable and any two |
| 1364 | objects with the same value have the same object representation. |
| 1365 | |
| 1366 | **Documentation:** |
| 1367 | [std::has_unique_object_representations](https://en.cppreference.com/w/cpp/types/has_unique_object_representations) |
| 1368 | |
| 1369 | **Notes:** |
| 1370 | *** promo |
| 1371 | None |
| 1372 | *** |
| 1373 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 1374 | ### std::reduce <sup>[tbd]</sup> |
| 1375 | |
| 1376 | ```c++ |
| 1377 | std::reduce(std::execution::par, v.cbegin(), v.cend()); |
| 1378 | ``` |
| 1379 | |
| 1380 | **Description:** Like `std::accumulate` except the elements of the range may be |
| 1381 | grouped and rearranged in arbitrary order. |
| 1382 | |
| 1383 | **Documentation:** |
| 1384 | [std::reduce](https://en.cppreference.com/w/cpp/algorithm/reduce) |
| 1385 | |
| 1386 | **Notes:** |
| 1387 | *** promo |
| 1388 | Makes the most sense in conjunction with `std::execution::par`. |
| 1389 | *** |
| 1390 | |
| 1391 | ### std::inclusive_scan <sup>[tbd]</sup> |
| 1392 | |
| 1393 | ```c++ |
| 1394 | std::inclusive_scan(data.begin(), data.end(), output.begin()); |
| 1395 | ``` |
| 1396 | |
| 1397 | **Description:** Like `std::accumulate` but writes the result at each step into |
| 1398 | the output range. |
| 1399 | |
| 1400 | **Documentation:** |
| 1401 | [std::inclusive_scan](https://en.cppreference.com/w/cpp/algorithm/inclusive_scan) |
| 1402 | |
| 1403 | **Notes:** |
| 1404 | *** promo |
| 1405 | None |
| 1406 | *** |
| 1407 | |
| 1408 | ### std::exclusive_scan <sup>[tbd]</sup> |
| 1409 | |
| 1410 | ```c++ |
| 1411 | std::exclusive_scan(data.begin(), data.end(), output.begin()); |
| 1412 | ``` |
| 1413 | |
| 1414 | **Description:** Like `std::inclusive_scan` but omits the current element from |
| 1415 | the written output at each step; that is, results are "one value behind" those |
| 1416 | of `std::inclusive_scan`. |
| 1417 | |
| 1418 | **Documentation:** |
| 1419 | [std::exclusive_scan](https://en.cppreference.com/w/cpp/algorithm/exclusive_scan) |
| 1420 | |
| 1421 | **Notes:** |
| 1422 | *** promo |
| 1423 | None |
| 1424 | *** |
| 1425 | |
| 1426 | ### std::gcd <sup>[tbd]</sup> |
| 1427 | |
| 1428 | ```c++ |
| 1429 | static_assert(std::gcd(12, 18) == 6); |
| 1430 | ``` |
| 1431 | |
| 1432 | **Description:** Computes the greatest common divisor of its arguments. |
| 1433 | |
| 1434 | **Documentation:** |
| 1435 | [std::gcd](https://en.cppreference.com/w/cpp/numeric/gcd) |
| 1436 | |
| 1437 | **Notes:** |
| 1438 | *** promo |
| 1439 | None |
| 1440 | *** |
| 1441 | |
| 1442 | ### std::lcm <sup>[tbd]</sup> |
| 1443 | |
| 1444 | ```c++ |
| 1445 | static_assert(std::lcm(12, 18) == 36); |
| 1446 | ``` |
| 1447 | |
| 1448 | **Description:** Computes the least common multiple of its arguments. |
| 1449 | |
| 1450 | **Documentation:** |
| 1451 | [std::lcm](https://en.cppreference.com/w/cpp/numeric/lcm) |
| 1452 | |
| 1453 | **Notes:** |
| 1454 | *** promo |
| 1455 | None |
| 1456 | *** |
| 1457 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 1458 | ### Mathematical special functions <sup>[tbd]</sup> |
| 1459 | |
| 1460 | ```c++ |
| 1461 | std::assoc_laguerre() |
| 1462 | std::assoc_legendre() |
| 1463 | std::beta() |
| 1464 | std::comp_ellint_1() |
| 1465 | std::comp_ellint_2() |
| 1466 | std::comp_ellint_3() |
| 1467 | std::cyl_bessel_i() |
| 1468 | std::cyl_bessel_j() |
| 1469 | std::cyl_bessel_k() |
| 1470 | std::cyl_neumann() |
| 1471 | std::ellint_1() |
| 1472 | std::ellint_2() |
| 1473 | std::ellint_3() |
| 1474 | std::expint() |
| 1475 | std::hermite() |
| 1476 | std::legendre() |
| 1477 | std::laguerre() |
| 1478 | std::riemann_zeta() |
| 1479 | std::sph_bessel() |
| 1480 | std::sph_legendre() |
| 1481 | std::sph_neumann() |
| 1482 | ``` |
| 1483 | |
| 1484 | **Description:** A variety of mathematical functions. |
| 1485 | |
| 1486 | **Documentation:** |
| 1487 | [Mathematical special functions](https://en.cppreference.com/w/cpp/numeric/special_functions) |
| 1488 | |
| 1489 | **Notes:** |
| 1490 | *** promo |
| 1491 | May not be supported in libc++, according to the |
| 1492 | [library features table](https://en.cppreference.com/w/cpp/17) |
| 1493 | *** |
| 1494 | |
| 1495 | ### 3D std::hypot <sup>[tbd]</sup> |
| 1496 | |
| 1497 | ```c++ |
| 1498 | double dist = std::hypot(1.0, 2.5, 3.7); |
| 1499 | ``` |
| 1500 | |
| 1501 | **Description:** Computes the distance from the origin in 3D space. |
| 1502 | |
| 1503 | **Documentation:** |
| 1504 | [std::hypot](https://en.cppreference.com/w/cpp/numeric/math/hypot) |
| 1505 | |
| 1506 | **Notes:** |
| 1507 | *** promo |
| 1508 | None |
| 1509 | *** |
| 1510 | |
| 1511 | ### Cache line interface <sup>[tbd]</sup> |
| 1512 | |
| 1513 | ```c++ |
| 1514 | alignas(std::hardware_destructive_interference_size) std::atomic<int> cat; |
| 1515 | static_assert(sizeof(S) <= std::hardware_constructive_interference_size); |
| 1516 | ``` |
| 1517 | |
| 1518 | **Description:** A portable way to access the L1 data cache line size. |
| 1519 | |
| 1520 | **Documentation:** |
| 1521 | [Hardware interference size](https://en.cppreference.com/w/cpp/thread/hardware_destructive_interference_size) |
| 1522 | |
| 1523 | **Notes:** |
| 1524 | *** promo |
| 1525 | May not be supported in libc++, according to the |
| 1526 | [library features table](https://en.cppreference.com/w/cpp/17) |
| 1527 | *** |
| 1528 | |
| 1529 | ### std::launder <sup>[tbd]</sup> |
| 1530 | |
| 1531 | ```c++ |
| 1532 | struct Y { int z; }; |
| 1533 | alignas(Y) std::byte s[sizeof(Y)]; |
| 1534 | Y* q = new(&s) Y{2}; |
| 1535 | const int h = std::launder(reinterpret_cast<Y*>(&s))->z; |
| 1536 | ``` |
| 1537 | |
| 1538 | **Description:** When used to wrap a pointer, makes it valid to access the |
| 1539 | resulting object in cases it otherwise wouldn't have been, in a very limited set |
| 1540 | of circumstances. |
| 1541 | |
| 1542 | **Documentation:** |
| 1543 | [std::launder](https://en.cppreference.com/w/cpp/utility/launder) |
| 1544 | |
| 1545 | **Notes:** |
| 1546 | *** promo |
| 1547 | None |
| 1548 | *** |
| 1549 | |
| 1550 | ### std::to_chars/std::from_chars <sup>[tbd]</sup> |
| 1551 | |
| 1552 | ```c++ |
| 1553 | std::to_chars(str.data(), str.data() + str.size(), 42); |
| 1554 | std::from_chars(str.data(), str.data() + str.size(), result); |
| 1555 | ``` |
| 1556 | |
| 1557 | **Description:** Locale-independent, non-allocating, non-throwing functions to |
| 1558 | convert values to/from character strings, designed for use in high-throughput |
| 1559 | contexts. |
| 1560 | |
| 1561 | **Documentation:** |
| 1562 | [std::to_chars](https://en.cppreference.com/w/cpp/utility/to_chars), |
| 1563 | [std::from_chars](https://en.cppreference.com/w/cpp/utility/from_chars) |
| 1564 | |
| 1565 | **Notes:** |
| 1566 | *** promo |
| 1567 | None |
| 1568 | *** |
| 1569 | |
| 1570 | ### std::atomic<T>::is_always_lock_free <sup>[tbd]</sup> |
| 1571 | |
| 1572 | ```c++ |
| 1573 | template <typename T> |
| 1574 | struct is_lock_free_impl |
| 1575 | : std::integral_constant<bool, std::atomic<T>::is_always_lock_free> {}; |
| 1576 | ``` |
| 1577 | |
| 1578 | **Description:** True when the given atomic type is always lock-free. |
| 1579 | |
| 1580 | **Documentation:** |
| 1581 | [std::atomic<T>::is_always_lock_free](https://en.cppreference.com/w/cpp/atomic/atomic/is_always_lock_free) |
| 1582 | |
| 1583 | **Notes:** |
| 1584 | *** promo |
| 1585 | None |
| 1586 | *** |
| 1587 | |
| 1588 | ### std::scoped_lock <sup>[tbd]</sup> |
| 1589 | |
| 1590 | ```c++ |
| 1591 | std::scoped_lock lock(e1.m, e2.m); |
| 1592 | ``` |
| 1593 | |
| 1594 | **Description:** Provides an RAII-style mechanism for owning one or more mutexes |
| 1595 | for the duration of a scoped block. |
| 1596 | |
| 1597 | **Documentation:** |
| 1598 | [std::scoped_lock](https://en.cppreference.com/w/cpp/thread/scoped_lock) |
| 1599 | |
| 1600 | **Notes:** |
| 1601 | *** promo |
| 1602 | See also `base::AutoLock`. |
| 1603 | *** |
| 1604 | |
| 1605 | ### std::timespec_get <sup>[tbd]</sup> |
| 1606 | |
| 1607 | ```c++ |
| 1608 | std::timespec ts; |
| 1609 | std::timespec_get(&ts, TIME_UTC); |
| 1610 | ``` |
| 1611 | |
| 1612 | **Description:** Gets the current calendar time in the given time base. |
| 1613 | |
| 1614 | **Documentation:** |
| 1615 | [std::timespec_get](https://en.cppreference.com/w/cpp/chrono/c/timespec_get) |
| 1616 | |
| 1617 | **Notes:** |
| 1618 | *** promo |
| 1619 | None |
| 1620 | *** |
| 1621 | |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1622 | ## Abseil Allowed Library Features {#absl-allowlist} |
| 1623 | |
| 1624 | The following Abseil library features are allowed in the Chromium codebase. |
| 1625 | |
danakj | b30d180 | 2022-07-07 20:52:14 | [diff] [blame] | 1626 | ### Attribute macros <sup>[allowed]</sup> |
| 1627 | |
| 1628 | ```c++ |
| 1629 | const char* name() ABSL_ATTRIBUTE_RETURNS_NONNULL { return "hello world"; } |
| 1630 | ``` |
| 1631 | |
| 1632 | **Description:** Macros that conditionally resolve to attributes. Prefer to use |
| 1633 | standard C++ attributes, such as `[[fallthrough]]`. Use these macros for |
| 1634 | non-standard attributes, which may not be present in all compilers. |
| 1635 | |
| 1636 | **Documentation:** |
| 1637 | [attributes.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/base/attributes.h) |
| 1638 | |
| 1639 | **Notes:** |
| 1640 | *** promo |
| 1641 | [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/vQmaBfbyBGM/m/HHOYUZ5YAwAJ) |
| 1642 | *** |
| 1643 | |
Alex Turner | e24079f0 | 2022-01-25 20:08:57 | [diff] [blame] | 1644 | ### 128bit integer <sup>[allowed]</sup> |
| 1645 | |
| 1646 | ```c++ |
| 1647 | uint64_t a; |
| 1648 | absl::uint128 v = a; |
| 1649 | ``` |
| 1650 | |
| 1651 | **Description:** Signed and unsigned 128-bit integer types meant to mimic |
| 1652 | intrinsic types as closely as possible. |
| 1653 | |
| 1654 | **Documentation:** |
| 1655 | [Numerics](https://abseil.io/docs/cpp/guides/numeric) |
| 1656 | |
| 1657 | **Notes:** |
| 1658 | *** promo |
| 1659 | [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/6l8MlO7vEek) |
| 1660 | *** |
| 1661 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 1662 | ### Optional <sup>[allowed]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1663 | |
| 1664 | ```c++ |
| 1665 | absl::optional |
| 1666 | ``` |
| 1667 | |
| 1668 | **Description:** Early adaptation of C++17 `std::optional`. |
| 1669 | |
| 1670 | **Documentation:** |
| 1671 | [std::optional](https://en.cppreference.com/w/cpp/utility/optional) |
| 1672 | |
| 1673 | **Notes:** |
| 1674 | *** promo |
| 1675 | Replaces `base::Optional`. |
| 1676 | [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/zUGqagX1NFU) |
| 1677 | *** |
| 1678 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 1679 | ### Status <sup>[allowed]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1680 | |
| 1681 | ```c++ |
| 1682 | absl::Status |
| 1683 | ``` |
| 1684 | |
| 1685 | **Description:** Type for returning detailed errors. |
| 1686 | |
| 1687 | **Documentation:** |
| 1688 | [status.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/status/status.h) |
| 1689 | |
| 1690 | **Notes:** |
| 1691 | *** promo |
| 1692 | Approved for use inside a wrapper type. Use |
| 1693 | [abseil_string_conversions.h](https://source.chromium.org/chromium/chromium/src/+/main:base/strings/abseil_string_conversions.h) |
| 1694 | to convert to and from |
| 1695 | [absl::string_view](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/strings/string_view.h) |
| 1696 | so the wrapper can expose |
| 1697 | [base::StringPiece](https://source.chromium.org/chromium/chromium/src/+/main:base/strings/string_piece.h). |
| 1698 | Use |
| 1699 | [absl::Cord](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/strings/cord.h) |
| 1700 | directly as minimally necessary to interface; do not expose in the wrapper type |
| 1701 | API. |
| 1702 | |
| 1703 | [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ImdFCSZ-NMA) |
| 1704 | *** |
| 1705 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 1706 | ### Variant <sup>[allowed]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1707 | |
| 1708 | ```c++ |
| 1709 | absl::variant |
| 1710 | ``` |
| 1711 | |
| 1712 | **Description:** Early adaptation of C++17 `std::variant`. |
| 1713 | |
| 1714 | **Documentation:** |
| 1715 | [std::variant](https://en.cppreference.com/w/cpp/utility/variant) |
| 1716 | |
| 1717 | **Notes:** |
| 1718 | *** promo |
| 1719 | [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/DqvG-TpvMyU) |
| 1720 | *** |
| 1721 | |
Avi Drissman | bc6545f4 | 2022-05-03 17:47:38 | [diff] [blame] | 1722 | ### in_place <sup>[allowed]</sup> |
| 1723 | |
| 1724 | ```c++ |
| 1725 | absl::in_place |
| 1726 | ``` |
| 1727 | |
| 1728 | **Description:** Early adaptation of C++17 `std::in_place`. |
| 1729 | |
| 1730 | **Documentation:** |
| 1731 | [std::in_place](https://en.cppreference.com/w/cpp/utility/in_place) |
| 1732 | |
| 1733 | **Notes:** |
| 1734 | *** promo |
| 1735 | Because the Abseil versions of `optional` and `variant` are used, the Abseil |
| 1736 | version of `in_place` is used in Chromium. See the |
| 1737 | [discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ZspmuJPpv6s/m/wYYTCiRwAAAJ). |
| 1738 | *** |
| 1739 | |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1740 | ## Abseil Banned Library Features {#absl-blocklist} |
| 1741 | |
| 1742 | The following Abseil library features are not allowed in the Chromium codebase. |
| 1743 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 1744 | ### Any <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1745 | |
| 1746 | ```c++ |
| 1747 | absl::any a = int{5}; |
| 1748 | EXPECT_THAT(absl::any_cast<int>(&a), Pointee(5)); |
| 1749 | EXPECT_EQ(absl::any_cast<size_t>(&a), nullptr); |
| 1750 | ``` |
| 1751 | |
| 1752 | **Description:** Early adaptation of C++17 `std::any`. |
| 1753 | |
| 1754 | **Documentation:** [std::any](https://en.cppreference.com/w/cpp/utility/any) |
| 1755 | |
| 1756 | **Notes:** |
| 1757 | *** promo |
| 1758 | Banned since workaround for lack of RTTI isn't compatible with the component |
Daniel Cheng | c05fcc6 | 2022-01-12 16:54:29 | [diff] [blame] | 1759 | build ([Bug](https://crbug.com/1096380)). Also see `std::any`. |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1760 | *** |
| 1761 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 1762 | ### Command line flags <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1763 | |
| 1764 | ```c++ |
| 1765 | ABSL_FLAG(bool, logs, false, "print logs to stderr"); |
| 1766 | app --logs=true; |
| 1767 | ``` |
| 1768 | |
| 1769 | **Description:** Allows programmatic access to flag values passed on the |
| 1770 | command-line to binaries. |
| 1771 | |
| 1772 | **Documentation:** [Flags Library](https://abseil.io/docs/cpp/guides/flags) |
| 1773 | |
| 1774 | **Notes:** |
| 1775 | *** promo |
| 1776 | Banned since workaround for lack of RTTI isn't compatible with the component |
| 1777 | build. ([Bug](https://crbug.com/1096380)) Use `base::CommandLine` instead. |
| 1778 | *** |
| 1779 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 1780 | ### Span <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1781 | |
| 1782 | ```c++ |
| 1783 | absl::Span |
| 1784 | ``` |
| 1785 | |
| 1786 | **Description:** Early adaptation of C++20 `std::span`. |
| 1787 | |
| 1788 | **Documentation:** [Using absl::Span](https://abseil.io/tips/93) |
| 1789 | |
| 1790 | **Notes:** |
| 1791 | *** promo |
| 1792 | Banned due to being less std::-compliant than `base::span`. Keep using |
| 1793 | `base::span`. |
| 1794 | *** |
| 1795 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 1796 | ### string_view <sup>[banned]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1797 | |
| 1798 | ```c++ |
| 1799 | absl::string_view |
| 1800 | ``` |
| 1801 | |
| 1802 | **Description:** Early adaptation of C++17 `std::string_view`. |
| 1803 | |
| 1804 | **Documentation:** [absl::string_view](https://abseil.io/tips/1) |
| 1805 | |
| 1806 | **Notes:** |
| 1807 | *** promo |
| 1808 | Banned due to only working with 8-bit characters. Keep using |
| 1809 | `base::StringPiece` from `base/strings/`. |
| 1810 | *** |
| 1811 | |
Daniel Cheng | 2248b33 | 2022-07-27 06:16:59 | [diff] [blame^] | 1812 | ### FunctionRef <sup>[banned]</sup> |
| 1813 | |
| 1814 | ```c++ |
| 1815 | absl::FunctionRef |
| 1816 | ``` |
| 1817 | |
| 1818 | **Description:** Type for holding a non-owning reference to an object of any |
| 1819 | invocable type. |
| 1820 | |
| 1821 | **Documentation:** |
| 1822 | [function_ref.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/function_ref.h) |
| 1823 | |
| 1824 | **Notes:** |
| 1825 | *** promo |
| 1826 | - `absl::FunctionRef` is banned due to allowing implicit conversions between |
| 1827 | function signatures in potentially surprising ways. For example, a callable |
| 1828 | with the signature `int()` will bind to `absl::FunctionRef<void()>`: the |
| 1829 | return value from the callable will be silently discarded. |
| 1830 | - In Chromium, use `base::FunctionRef` instead. |
| 1831 | - Unlike `base::OnceCallback` and `base::RepeatingCallback`, `base::FunctionRef` |
| 1832 | supports capturing lambdas. |
| 1833 | - Useful when passing an invocable object to a function that synchronously calls |
| 1834 | the invocable object, e.g. `ForEachFrame(base::FunctionRef<void(Frame&)>)`. |
| 1835 | This can often result in clearer code than code that is templated to accept |
| 1836 | lambdas, e.g. with `template <typename Invocable> void |
| 1837 | ForEachFrame(Invocable invocable)`, it is much less obvious what arguments |
| 1838 | will be passed to `invocable`. |
| 1839 | - For now, `base::OnceCallback` and `base::RepeatingCallback` intentionally |
| 1840 | disallow conversions to `base::FunctionRef`, under the theory that the |
| 1841 | callback should be a capturing lambda instead. Attempting to use this |
| 1842 | conversion will trigger a `static_assert` requesting additional feedback for |
| 1843 | use cases where this conversion would be valuable. |
| 1844 | - *Important:* `base::FunctionRef` must not outlive the function call. Like |
| 1845 | `base::StringPiece`, `base::FunctionRef` is a *non-owning* reference. Using a |
| 1846 | `base::FunctionRef` as a return value or class field is dangerous and likely |
| 1847 | to result in lifetime bugs. |
| 1848 | - [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/JVN4E4IIYA0/m/V0EVUVLiBwAJ) |
| 1849 | *** |
| 1850 | |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1851 | ## Abseil TBD Features {#absl-review} |
| 1852 | |
| 1853 | The following Abseil library features are not allowed in the Chromium codebase. |
| 1854 | See the top of this page on how to propose moving a feature from this list into |
| 1855 | the allowed or banned sections. |
| 1856 | |
Danil Chapovalov | a9f2703 | 2022-06-20 16:56:14 | [diff] [blame] | 1857 | ### AnyInvocable <sup>[tbd]</sup> |
| 1858 | |
| 1859 | ```c++ |
| 1860 | absl::AnyInvocable |
| 1861 | ``` |
| 1862 | |
| 1863 | **Description:** An equivalent of the C++23 std::move_only_function. |
| 1864 | |
| 1865 | **Documentation:** |
| 1866 | * [any_invocable.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/any_invocable.h) |
| 1867 | * [std::move_only_function](https://en.cppreference.com/w/cpp/utility/functional/move_only_function/move_only_function) |
| 1868 | |
| 1869 | **Notes:** |
| 1870 | *** promo |
| 1871 | Overlaps with `base::RepeatingCallback`, `base::OnceCallback`. |
| 1872 | *** |
| 1873 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 1874 | ### bind_front <sup>[tbd]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1875 | |
| 1876 | ```c++ |
| 1877 | absl::bind_front |
| 1878 | ``` |
| 1879 | |
| 1880 | **Description:** Binds the first N arguments of an invocable object and stores them by value. |
| 1881 | |
| 1882 | **Documentation:** |
| 1883 | * [bind_front.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/bind_front.h) |
| 1884 | * [Avoid std::bind](https://abseil.io/tips/108) |
| 1885 | |
| 1886 | **Notes:** |
| 1887 | *** promo |
| 1888 | Overlaps with `base::Bind`. |
| 1889 | *** |
| 1890 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 1891 | ### Cleanup <sup>[tbd]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1892 | |
| 1893 | ```c++ |
| 1894 | FILE* sink_file = fopen(sink_path, "w"); |
| 1895 | auto sink_closer = absl::MakeCleanup([sink_file] { fclose(sink_file); }); |
| 1896 | ``` |
| 1897 | |
| 1898 | **Description:** Implements the scope guard idiom, invoking the contained |
| 1899 | callback's `operator()() &&` on scope exit. |
| 1900 | |
| 1901 | **Documentation:** |
| 1902 | [cleanup.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/cleanup/cleanup.h) |
| 1903 | |
| 1904 | **Notes:** |
| 1905 | *** promo |
| 1906 | Similar to `defer` in Golang. |
| 1907 | *** |
| 1908 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 1909 | ### Containers <sup>[tbd]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1910 | |
| 1911 | ```c++ |
| 1912 | absl::flat_hash_map |
| 1913 | absl::flat_hash_set |
| 1914 | absl::node_hash_map |
| 1915 | absl::node_hash_set |
| 1916 | absl::btree_map |
| 1917 | absl::btree_set |
| 1918 | absl::btree_multimap |
| 1919 | absl::btree_multiset |
| 1920 | absl::InlinedVector |
| 1921 | absl::FixedArray |
| 1922 | ``` |
| 1923 | |
| 1924 | **Description:** Alternatives to STL containers designed to be more efficient |
| 1925 | in the general case. |
| 1926 | |
| 1927 | **Documentation:** |
| 1928 | * [Containers](https://abseil.io/docs/cpp/guides/container) |
| 1929 | * [Hash](https://abseil.io/docs/cpp/guides/hash) |
| 1930 | |
| 1931 | **Notes:** |
| 1932 | *** promo |
| 1933 | Supplements `base/containers/`. |
| 1934 | *** |
| 1935 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 1936 | ### Container utilities <sup>[tbd]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1937 | |
| 1938 | ```c++ |
| 1939 | auto it = absl::c_find(container, value); |
| 1940 | ``` |
| 1941 | |
| 1942 | **Description:** Container-based versions of algorithmic functions within C++ |
| 1943 | standard library. |
| 1944 | |
| 1945 | **Documentation:** |
| 1946 | [container.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/algorithm/container.h) |
| 1947 | |
| 1948 | **Notes:** |
| 1949 | *** promo |
| 1950 | Overlaps with `base/ranges/algorithm.h`. |
| 1951 | *** |
| 1952 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 1953 | ### Random <sup>[tbd]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1954 | |
| 1955 | ```c++ |
| 1956 | absl::BitGen bitgen; |
| 1957 | size_t index = absl::Uniform(bitgen, 0u, elems.size()); |
| 1958 | ``` |
| 1959 | |
| 1960 | **Description:** Functions and utilities for generating pseudorandom data. |
| 1961 | |
| 1962 | **Documentation:** [Random library](https://abseil.io/docs/cpp/guides/random) |
| 1963 | |
| 1964 | **Notes:** |
| 1965 | *** promo |
| 1966 | Overlaps with `base/rand_util.h`. |
| 1967 | *** |
| 1968 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 1969 | ### StatusOr <sup>[tbd]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1970 | |
| 1971 | ```c++ |
| 1972 | absl::StatusOr<T> |
| 1973 | ``` |
| 1974 | |
| 1975 | **Description:** An object that is either a usable value, or an error Status |
| 1976 | explaining why such a value is not present. |
| 1977 | |
| 1978 | **Documentation:** |
| 1979 | [statusor.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/status/statusor.h) |
| 1980 | |
| 1981 | **Notes:** |
| 1982 | *** promo |
| 1983 | None |
| 1984 | *** |
| 1985 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 1986 | ### String Formatting <sup>[tbd]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 1987 | |
| 1988 | ```c++ |
| 1989 | absl::StrFormat |
| 1990 | ``` |
| 1991 | |
| 1992 | **Description:** A typesafe replacement for the family of printf() string |
| 1993 | formatting routines. |
| 1994 | |
| 1995 | **Documentation:** |
| 1996 | [String Formatting](https://abseil.io/docs/cpp/guides/format) |
| 1997 | |
| 1998 | **Notes:** |
| 1999 | *** promo |
| 2000 | None |
| 2001 | *** |
| 2002 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 2003 | ### Strings Library <sup>[tbd]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 2004 | |
| 2005 | ```c++ |
| 2006 | absl::StrSplit |
| 2007 | absl::StrJoin |
| 2008 | absl::StrCat |
| 2009 | absl::StrAppend |
| 2010 | absl::Substitute |
| 2011 | absl::StrContains |
| 2012 | ``` |
| 2013 | |
| 2014 | **Description:** Classes and utility functions for manipulating and comparing |
| 2015 | strings. |
| 2016 | |
| 2017 | **Documentation:** |
| 2018 | [String Utilities](https://abseil.io/docs/cpp/guides/strings) |
| 2019 | |
| 2020 | **Notes:** |
| 2021 | *** promo |
| 2022 | Overlaps with `base/strings`. |
| 2023 | *** |
| 2024 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 2025 | ### Synchronization <sup>[tbd]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 2026 | |
| 2027 | ```c++ |
| 2028 | absl::Mutex |
| 2029 | ``` |
| 2030 | |
| 2031 | **Description:** Primitives for managing tasks across different threads. |
| 2032 | |
| 2033 | **Documentation:** |
| 2034 | [Synchronization](https://abseil.io/docs/cpp/guides/synchronization) |
| 2035 | |
| 2036 | **Notes:** |
| 2037 | *** promo |
| 2038 | Overlaps with `Lock` in `base/synchronization/`. |
| 2039 | *** |
| 2040 | |
danakj | a6f71cb1 | 2021-12-15 21:04:49 | [diff] [blame] | 2041 | ### Time library <sup>[tbd]</sup> |
Joe Mason | fe4f256 | 2021-09-15 15:23:13 | [diff] [blame] | 2042 | |
| 2043 | ```c++ |
| 2044 | absl::Duration |
| 2045 | absl::Time |
| 2046 | absl::TimeZone |
| 2047 | absl::CivilDay |
| 2048 | ``` |
| 2049 | |
| 2050 | **Description:** Abstractions for holding time values, both in terms of |
| 2051 | absolute time and civil time. |
| 2052 | |
| 2053 | **Documentation:** [Time](https://abseil.io/docs/cpp/guides/time) |
| 2054 | |
| 2055 | **Notes:** |
| 2056 | *** promo |
| 2057 | Overlaps with `Time` APIs in `base/`. |
| 2058 | *** |