blob: 299061566db1555767b4af1dbdf69690ed426128 [file] [log] [blame] [view]
Joe Masonfe4f2562021-09-15 15:23:131# 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).
5It summarizes the supported state of new and updated language and library
6features in recent C++ standards and the [Abseil](https://abseil.io/about/)
7library. This guide applies to both Chromium and its subprojects, though
8subprojects can choose to be more restrictive if necessary for toolchain
9support._
10
11The C++ language has in recent years received an updated standard every three
Peter Kasting1865f2772021-12-23 21:23:5812years (C++11, C++14, etc.). For various reasons, Chromium does not immediately
Joe Masonfe4f2562021-09-15 15:23:1313allow new features on the publication of such a standard. Instead, once
Hong Xu68ba51a02022-04-27 22:21:3514Chromium supports the toolchain to a certain extent (e.g., build support is
15ready), a standard is declared "_initially supported_", with new
16language/library features banned pending discussion but not yet allowed.
Joe Masonfe4f2562021-09-15 15:23:1317
18You 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).
20Include a short blurb on what the feature is and why you think it should or
21should not be allowed, along with links to any relevant previous discussion. If
22the list arrives at some consensus, send a codereview to change this file
23accordingly, linking to your discussion thread.
24
25If an item remains on the TBD list two years after initial support is added,
26style arbiters should explicitly move it to an appropriate allowlist or
27blocklist, allowing it if there are no obvious reasons to ban.
28
29The current status of existing standards and Abseil features is:
30
31* **C++11:** _Default allowed; see banned features below_
Peter Kastinge2c5ee82023-02-15 17:23:0832* **C++14:** _Default allowed_
Peter Kasting1865f2772021-12-23 21:23:5833* **C++17:** Initially supported December 23, 2021; see allowed/banned/TBD
34 features below
Arthur Sonzognid8517c92023-03-03 09:41:4535* **C++20:** _Not yet supported in Chromium_, with the exception of
36 [designated initializers](https://google.github.io/styleguide/cppguide.html#Designated_initializers)
Peter Kasting1865f2772021-12-23 21:23:5837* **C++23:** _Not yet standardized_
Peter Kastinge2c5ee82023-02-15 17:23:0838* **Abseil:** _Default allowed; see banned/TBD features below_
39 * absl::AnyInvocable: Initially supported June 20, 2022
40 * Log library: Initially supported Aug 31, 2022
41 * CRC32C library: Initially supported Dec 5, 2022
Joe Masonfe4f2562021-09-15 15:23:1342
43[TOC]
44
Peter Kasting1865f2772021-12-23 21:23:5845## C++11 Banned Language Features {#core-blocklist-11}
Joe Masonfe4f2562021-09-15 15:23:1346
47The following C++11 language features are not allowed in the Chromium codebase.
48
danakja6f71cb12021-12-15 21:04:4949### Inline Namespaces <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:1350
51```c++
52inline 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
62Banned in the
63[Google Style Guide](https://google.github.io/styleguide/cppguide.html#Namespaces).
64Unclear how it will work with components.
65***
66
danakja6f71cb12021-12-15 21:04:4967### long long Type <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:1368
69```c++
70long 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
Peter Kastinge2c5ee82023-02-15 17:23:0880Use a `<stdint.h>` type if you need a 64-bit number.
Joe Masonfe4f2562021-09-15 15:23:1381[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/chromium-dev/RxugZ-pIDxk)
82***
83
danakja6f71cb12021-12-15 21:04:4984### User-Defined Literals <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:1385
86```c++
Peter Kastinge2c5ee82023-02-15 17:23:0887DistanceType var = 12_km;
Joe Masonfe4f2562021-09-15 15:23:1388```
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
97Banned in the
98[Google Style Guide](https://google.github.io/styleguide/cppguide.html#Operator_Overloading).
99***
100
Peter Kasting1865f2772021-12-23 21:23:58101## C++11 Banned Library Features {#library-blocklist-11}
Joe Masonfe4f2562021-09-15 15:23:13102
103The following C++11 library features are not allowed in the Chromium codebase.
104
Peter Kastinge2c5ee82023-02-15 17:23:08105### &lt;cfenv&gt;, &lt;fenv.h&gt; <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13106
107```c++
108#include <cfenv>
109#include <fenv.h>
110```
111
112**Description:** Provides floating point status flags and control modes for
Peter Kastinge2c5ee82023-02-15 17:23:08113C-compatible code.
Joe Masonfe4f2562021-09-15 15:23:13114
115**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08116[Standard library header `<cfenv>`](https://en.cppreference.com/w/cpp/header/cfenv)
Joe Masonfe4f2562021-09-15 15:23:13117
118**Notes:**
119*** promo
120Banned by the
121[Google Style Guide](https://google.github.io/styleguide/cppguide.html#C++11)
122due to concerns about compiler support.
123***
124
Peter Kastinge2c5ee82023-02-15 17:23:08125### &lt;chrono&gt; <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13126
127```c++
128#include <chrono>
129```
130
Peter Kastinge2c5ee82023-02-15 17:23:08131**Description:** A standard date and time library.
Joe Masonfe4f2562021-09-15 15:23:13132
133**Documentation:**
134[Date and time utilities](https://en.cppreference.com/w/cpp/chrono)
135
136**Notes:**
137*** promo
Peter Kastinge2c5ee82023-02-15 17:23:08138Overlaps with `base/time`. Keep using the `base/time` classes.
Joe Masonfe4f2562021-09-15 15:23:13139***
140
Peter Kastinge2c5ee82023-02-15 17:23:08141### &lt;exception&gt; <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13142
143```c++
144#include <exception>
145```
146
Peter Kastinge2c5ee82023-02-15 17:23:08147**Description:** Exception throwing and handling.
Joe Masonfe4f2562021-09-15 15:23:13148
149**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08150[Standard library header `<exception>`](https://en.cppreference.com/w/cpp/header/exception)
Joe Masonfe4f2562021-09-15 15:23:13151
152**Notes:**
153*** promo
154Exceptions are banned by the
155[Google Style Guide](https://google.github.io/styleguide/cppguide.html#Exceptions)
156and disabled in Chromium compiles. However, the `noexcept` specifier is
157explicitly allowed.
Peter Kastinge2c5ee82023-02-15 17:23:08158
Joe Masonfe4f2562021-09-15 15:23:13159[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/chromium-dev/8i4tMqNpHhg)
160***
161
Peter Kastinge2c5ee82023-02-15 17:23:08162### &lt;random&gt; <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13163
164```c++
Peter Kastinge2c5ee82023-02-15 17:23:08165#include <random>
Joe Masonfe4f2562021-09-15 15:23:13166```
167
Joe Masonfe4f2562021-09-15 15:23:13168**Description:** Random number generation algorithms and utilities.
169
170**Documentation:**
171[Pseudo-random number generation](https://en.cppreference.com/w/cpp/numeric/random)
172
173**Notes:**
174*** promo
175Do not use any random number engines from `<random>`. Instead, use
176`base::RandomBitGenerator`.
Peter Kastinge2c5ee82023-02-15 17:23:08177
Joe Masonfe4f2562021-09-15 15:23:13178[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/16Xmw05C-Y0)
179***
180
Peter Kastinge2c5ee82023-02-15 17:23:08181### &lt;ratio&gt; <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13182
183```c++
Peter Kastinge2c5ee82023-02-15 17:23:08184#include <ratio>
Joe Masonfe4f2562021-09-15 15:23:13185```
186
Peter Kastinge2c5ee82023-02-15 17:23:08187**Description:** Provides compile-time rational numbers.
Joe Masonfe4f2562021-09-15 15:23:13188
189**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08190[`std::ratio`](https://en.cppreference.com/w/cpp/numeric/ratio/ratio)
Joe Masonfe4f2562021-09-15 15:23:13191
192**Notes:**
193*** promo
194Banned by the
195[Google Style Guide](https://google.github.io/styleguide/cppguide.html#C++11)
196due to concerns that this is tied to a more template-heavy interface style.
197***
198
Peter Kastinge2c5ee82023-02-15 17:23:08199### &lt;regex&gt; <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13200
201```c++
202#include <regex>
203```
204
Peter Kastinge2c5ee82023-02-15 17:23:08205**Description:** A standard regular expressions library.
Joe Masonfe4f2562021-09-15 15:23:13206
207**Documentation:**
208[Regular expressions library](https://en.cppreference.com/w/cpp/regex)
209
210**Notes:**
211*** promo
212Overlaps with many regular expression libraries in Chromium. When in doubt, use
Peter Kastinge2c5ee82023-02-15 17:23:08213`third_party/re2`.
Joe Masonfe4f2562021-09-15 15:23:13214***
215
Peter Kastinge2c5ee82023-02-15 17:23:08216### std::bind <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13217
218```c++
Peter Kastinge2c5ee82023-02-15 17:23:08219auto x = std::bind(function, args, ...);
Joe Masonfe4f2562021-09-15 15:23:13220```
221
Peter Kastinge2c5ee82023-02-15 17:23:08222**Description:** Declares a function object bound to certain arguments.
Joe Masonfe4f2562021-09-15 15:23:13223
224**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08225[`std::bind`](https://en.cppreference.com/w/cpp/utility/functional/bind)
Joe Masonfe4f2562021-09-15 15:23:13226
227**Notes:**
228*** promo
Peter Kastinge2c5ee82023-02-15 17:23:08229Use `base::Bind` instead. Compared to `std::bind`, `base::Bind` helps prevent
230lifetime issues by preventing binding of capturing lambdas and by forcing
231callers to declare raw pointers as `Unretained`.
Joe Masonfe4f2562021-09-15 15:23:13232
Peter Kastinge2c5ee82023-02-15 17:23:08233[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/SoEj7oIDNuA)
Joe Masonfe4f2562021-09-15 15:23:13234***
235
Peter Kastinge2c5ee82023-02-15 17:23:08236### std::function <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13237
238```c++
Peter Kastinge2c5ee82023-02-15 17:23:08239std::function x = [] { return 10; };
240std::function y = std::bind(foo, args);
Joe Masonfe4f2562021-09-15 15:23:13241```
242
Peter Kastinge2c5ee82023-02-15 17:23:08243**Description:** Wraps a standard polymorphic function.
Joe Masonfe4f2562021-09-15 15:23:13244
245**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08246[`std::function`](https://en.cppreference.com/w/cpp/utility/functional/function)
247
248**Notes:**
249*** promo
250Use `base::{Once,Repeating}Callback` instead. Compared to `std::function`,
251`base::{Once,Repeating}Callback` directly supports Chromium's refcounting
252classes and weak pointers and deals with additional thread safety concerns.
253
254[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/SoEj7oIDNuA)
255***
256
257### std::shared_ptr <sup>[banned]</sup>
258
259```c++
260std::shared_ptr<int> x = std::make_shared<int>(10);
261```
262
263**Description:** Allows shared ownership of a pointer through reference counts.
264
265**Documentation:**
266[`std::shared_ptr`](https://en.cppreference.com/w/cpp/memory/shared_ptr)
267
268**Notes:**
269*** promo
270Unlike `base::RefCounted`, uses extrinsic rather than intrinsic reference
271counting. Could plausibly be used in Chromium, but would require significant
272migration.
273
274[Google Style Guide](https://google.github.io/styleguide/cppguide.html#Ownership_and_Smart_Pointers),
275[Discussion Thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/aT2wsBLKvzI)
276***
277
278### std::{sto{i,l,ul,ll,ull,f,d,ld},to_string} <sup>[banned]</sup>
279
280```c++
281int x = std::stoi("10");
282```
283
284**Description:** Converts strings to/from numbers.
285
286**Documentation:**
287[`std::stoi`, `std::stol`, `std::stoll`](https://en.cppreference.com/w/cpp/string/basic_string/stol),
288[`std::stoul`, `std::stoull`](https://en.cppreference.com/w/cpp/string/basic_string/stoul),
289[`std::stof`, `std::stod`, `std::stold`](https://en.cppreference.com/w/cpp/string/basic_string/stof),
290[`std::to_string`](https://en.cppreference.com/w/cpp/string/basic_string/to_string)
Joe Masonfe4f2562021-09-15 15:23:13291
292**Notes:**
293*** promo
294The string-to-number conversions rely on exceptions to communicate failure,
295while the number-to-string conversions have performance concerns and depend on
Peter Kastinge2c5ee82023-02-15 17:23:08296the locale. Use `base/strings/string_number_conversions.h` instead.
Joe Masonfe4f2562021-09-15 15:23:13297***
298
Peter Kastinge2c5ee82023-02-15 17:23:08299### std::weak_ptr <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13300
Peter Kastinge2c5ee82023-02-15 17:23:08301```c++
302std::weak_ptr<int> x = my_shared_x;
303```
304
305**Description:** Allows a weak reference to a `std::shared_ptr`.
306
307**Documentation:**
308[`std::weak_ptr`](https://en.cppreference.com/w/cpp/memory/weak_ptr)
309
310**Notes:**
311*** promo
312Banned because `std::shared_ptr` is banned. Use `base::WeakPtr` instead.
Joe Masonfe4f2562021-09-15 15:23:13313***
314
Peter Kastinge2c5ee82023-02-15 17:23:08315### Thread Support Library <sup>[banned]</sup>
316
317```c++
318#include <barrier> // C++20
319#include <condition_variable>
320#include <future>
321#include <latch> // C++20
322#include <mutex>
323#include <semaphore> // C++20
324#include <stop_token> // C++20
325#include <thread>
326```
327
Joe Masonfe4f2562021-09-15 15:23:13328**Description:** Provides a standard multithreading library using `std::thread`
329and associates
330
331**Documentation:**
332[Thread support library](https://en.cppreference.com/w/cpp/thread)
333
334**Notes:**
335*** promo
Peter Kastinge2c5ee82023-02-15 17:23:08336Overlaps with many classes in `base/synchronization`. `base::Thread` is tightly
337coupled to `base::MessageLoop` which would make it hard to replace. We should
338investigate using standard mutexes, or unique_lock, etc. to replace our
339locking/synchronization classes.
Joe Masonfe4f2562021-09-15 15:23:13340***
341
Peter Kasting1865f2772021-12-23 21:23:58342## C++17 Allowed Language Features {#core-allowlist-17}
343
344The following C++17 language features are allowed in the Chromium codebase.
345
Peter Kasting6d77e9d2023-02-09 21:58:18346### Class Template Argument Deduction (CTAD) <sup>[allowed]</sup>
Peter Kasting1865f2772021-12-23 21:23:58347
348```c++
349template <typename T>
350struct MyContainer {
351 MyContainer(T val) : val{val} {}
352 // ...
353};
354MyContainer c1(1); // Type deduced to be `int`.
355```
356
357**Description:** Automatic template argument deduction much like how it's done
358for functions, but now including class constructors.
359
360**Documentation:**
361[Class template argument deduction](https://en.cppreference.com/w/cpp/language/class_template_argument_deduction)
362
363**Notes:**
364*** promo
365Usage is governed by the
366[Google Style Guide](https://google.github.io/styleguide/cppguide.html#CTAD).
367***
368
Peter Kasting6d77e9d2023-02-09 21:58:18369### constexpr if <sup>[allowed]</sup>
Daniel Cheng7919c8d2022-07-08 01:36:22370
371```c++
Peter Kasting6d77e9d2023-02-09 21:58:18372if constexpr (cond) { ...
Daniel Cheng7919c8d2022-07-08 01:36:22373```
374
Peter Kasting6d77e9d2023-02-09 21:58:18375**Description:** Write code that is instantiated depending on a compile-time
376condition.
Daniel Cheng7919c8d2022-07-08 01:36:22377
378**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08379[`if` statement](https://en.cppreference.com/w/cpp/language/if)
Daniel Cheng7919c8d2022-07-08 01:36:22380
381**Notes:**
382*** promo
Peter Kasting6d77e9d2023-02-09 21:58:18383[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/op2ePZnjP0w)
Daniel Cheng7919c8d2022-07-08 01:36:22384***
385
Peter Kasting6d77e9d2023-02-09 21:58:18386### constexpr lambda <sup>[allowed]</sup>
Andrew Rayskiy6ce944d2022-01-04 18:13:33387
388```c++
Peter Kasting6d77e9d2023-02-09 21:58:18389auto identity = [](int n) constexpr { return n; };
390static_assert(identity(123) == 123);
Andrew Rayskiy6ce944d2022-01-04 18:13:33391```
392
Peter Kasting6d77e9d2023-02-09 21:58:18393**Description:** Compile-time lambdas using constexpr.
Andrew Rayskiy6ce944d2022-01-04 18:13:33394
395**Documentation:**
Peter Kasting6d77e9d2023-02-09 21:58:18396[Lambda expressions](https://en.cppreference.com/w/cpp/language/lambda)
Andrew Rayskiy6ce944d2022-01-04 18:13:33397
398**Notes:**
399*** promo
Peter Kasting6d77e9d2023-02-09 21:58:18400[Discussion thread](https://groups.google.com/u/1/a/chromium.org/g/cxx/c/jNMsxFTd30M)
401***
402
403### Declaring non-type template parameters with auto <sup>[allowed]</sup>
404
405```c++
406template <auto... seq>
407struct my_integer_sequence {
408 // ...
409};
410auto seq = my_integer_sequence<0, 1, 2>(); // Type deduced to be `int`.
411```
412
413**Description:** Following the deduction rules of `auto`, while respecting the
414non-type template parameter list of allowable types, template arguments can be
415deduced from the types of its arguments.
416
417**Documentation:**
418[Template parameters](https://en.cppreference.com/w/cpp/language/template_parameters)
419
420**Notes:**
421*** promo
422[Discussion thread](https://groups.google.com/u/1/a/chromium.org/g/cxx/c/jNMsxFTd30M)
Andrew Rayskiy6ce944d2022-01-04 18:13:33423***
424
Roland Bockf5242fb2022-01-05 17:54:38425### fallthrough attribute <sup>[allowed]</sup>
Roland Bock767858e2022-01-04 20:25:21426
427```c++
428case 1:
429 DoSomething();
430 [[fallthrough]];
431case 2:
432 break;
433```
434
435**Description:**
436The `[[fallthrough]]` attribute can be used in switch statements to indicate
437when intentionally falling through to the next case.
438
439**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08440[C++ attribute: `fallthrough`](https://en.cppreference.com/w/cpp/language/attributes/fallthrough)
Roland Bock767858e2022-01-04 20:25:21441
442**Notes:**
443*** promo
Peter Kasting6d77e9d2023-02-09 21:58:18444[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/JrvyFd243QI)
Roland Bock767858e2022-01-04 20:25:21445***
446
Peter Kasting6d77e9d2023-02-09 21:58:18447### Fold expressions <sup>[allowed]</sup>
Jayson Adams62f16c02022-01-11 15:03:35448
449```c++
Peter Kasting6d77e9d2023-02-09 21:58:18450template <typename... Args>
451auto sum(Args... args) {
452 return (... + args);
453}
Jayson Adams62f16c02022-01-11 15:03:35454```
455
Peter Kasting6d77e9d2023-02-09 21:58:18456**Description:** A fold expression performs a fold of a template parameter pack
457over a binary operator.
Jayson Adams62f16c02022-01-11 15:03:35458
459**Documentation:**
Peter Kasting6d77e9d2023-02-09 21:58:18460[Fold expression](https://en.cppreference.com/w/cpp/language/fold)
Jayson Adams62f16c02022-01-11 15:03:35461
462**Notes:**
463*** promo
Peter Kasting6d77e9d2023-02-09 21:58:18464[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/4DTm3idXz0w)
Jayson Adams62f16c02022-01-11 15:03:35465***
466
Avi Drissman37b7d512022-04-01 22:01:40467### Inline variables <sup>[allowed]</sup>
Bruce Dawsonbb51b0032022-01-14 19:24:22468
469```c++
470struct S {
471 static constexpr int kZero = 0; // constexpr implies inline here.
472};
473
Victor Vianna7f2773f2022-02-08 14:44:56474inline constexpr int kOne = 1; // Explicit inline needed here.
Bruce Dawsonbb51b0032022-01-14 19:24:22475```
476
477**Description:** The `inline` specifier can be applied to variables as well as
478to functions. A variable declared inline has the same semantics as a function
479declared inline. It can also be used to declare and define a static member
480variable, such that it does not need to be initialized in the source file.
481
482**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08483[`inline` specifier](https://en.cppreference.com/w/cpp/language/inline)
Bruce Dawsonbb51b0032022-01-14 19:24:22484
485**Notes:**
486*** promo
487Inline variables in anonymous namespaces in header files will still get one copy
488per translation unit, so they must be outside of an anonymous namespace to be
489effective.
490
491Mutable inline variables and taking the address of inline variables are banned
492since these will break the component build.
493
Peter Kasting6d77e9d2023-02-09 21:58:18494[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/hmyGFD80ocE)
495***
496
497### __has_include <sup>[allowed]</sup>
498
499```c++
500#if __has_include(<optional>) ...
501```
502
503**Description:** Checks whether a file is available for inclusion, i.e. the file
504exists.
505
506**Documentation:**
507[Source file inclusion](https://en.cppreference.com/w/cpp/preprocessor/include)
508
509**Notes:**
510*** promo
511[Discussion thread](https://groups.google.com/u/1/a/chromium.org/g/cxx/c/jNMsxFTd30M)
512***
513
514### Lambda capture this by value <sup>[allowed]</sup>
515
516```c++
517const auto l = [*this] { return member_; }
518```
519
520**Description:** `*this` captures the current object by copy, while `this`
521continues to capture by reference.
522
523**Documentation:**
524[Lambda capture](https://en.cppreference.com/w/cpp/language/lambda#Lambda_capture)
525
526**Notes:**
527*** promo
528[Discussion thread](https://groups.google.com/u/1/a/chromium.org/g/cxx/c/jNMsxFTd30M)
529***
530
531### maybe_unused attribute <sup>[allowed]</sup>
532
533```c++
534struct [[maybe_unused]] MyUnusedThing;
535[[maybe_unused]] int x;
536```
537
538**Description:**
539The `[[maybe_unused]]` attribute can be used to indicate that individual
540variables, functions, or fields of a class/struct/enum can be left unused.
541
542**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08543[C++ attribute: `maybe_unused`](https://en.cppreference.com/w/cpp/language/attributes/maybe_unused)
Peter Kasting6d77e9d2023-02-09 21:58:18544
545**Notes:**
546*** promo
547[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/jPLfU5eRg8M/)
548***
549
550### Nested namespaces <sup>[allowed]</sup>
551
552```c++
553namespace A::B::C { ...
554```
555
556**Description:** Using the namespace resolution operator to create nested
557namespace definitions.
558
559**Documentation:**
560[Namespaces](https://en.cppreference.com/w/cpp/language/namespace)
561
562**Notes:**
563*** promo
564[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/gLdR3apDSmg/)
565***
566
567### nodiscard attribute <sup>[allowed]</sup>
568
569```c++
570struct [[nodiscard]] ErrorOrValue;
571[[nodiscard]] bool DoSomething();
572```
573
574**Description:**
575The `[[nodiscard]]` attribute can be used to indicate that
576
577 - the return value of a function should not be ignored
578 - values of annotated classes/structs/enums returned from functions should not
579 be ignored
580
581**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08582[C++ attribute: `nodiscard`](https://en.cppreference.com/w/cpp/language/attributes/nodiscard)
Peter Kasting6d77e9d2023-02-09 21:58:18583
584**Notes:**
585*** promo
586This replaces the previous `WARN_UNUSED_RESULT` macro, which was a wrapper
587around the compiler-specific `__attribute__((warn_unused_result))`.
588
589[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/nH7Ar8pZ1Dw)
590***
591
592### Selection statements with initializer <sup>[allowed]</sup>
593
594```c++
595if (int a = Func(); a < 3) { ...
596switch (int a = Func(); a) { ...
597```
598
599**Description:** New versions of the if and switch statements which simplify
600common code patterns and help users keep scopes tight.
601
602**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08603[`if` statement](https://en.cppreference.com/w/cpp/language/if),
604[`switch` statement](https://en.cppreference.com/w/cpp/language/switch)
Peter Kasting6d77e9d2023-02-09 21:58:18605
606**Notes:**
607*** promo
608[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/4GP43nftePE)
609***
610
611### Structured bindings <sup>[allowed]</sup>
612
613```c++
614const auto [x, y] = FuncReturningStdPair();
615```
616
617**Description:** Allows writing `auto [x, y, z] = expr;` where the type of
618`expr` is a tuple-like object, whose elements are bound to the variables `x`,
619`y`, and `z` (which this construct declares). Tuple-like objects include
620`std::tuple`, `std::pair`, `std::array`, and aggregate structures.
621
622**Documentation:**
623[Structured binding declaration](https://en.cppreference.com/w/cpp/language/structured_binding)
624[Explanation of structured binding types](https://jguegant.github.io/blogs/tech/structured-bindings.html)
625
626**Notes:**
627*** promo
628In C++17, structured bindings don't work with lambda captures.
629[C++20 will allow capturing structured bindings by value](https://wg21.link/p1091r3).
630
631This feature forces omitting type names. Its use should follow
632[the guidance around `auto` in Google C++ Style guide](https://google.github.io/styleguide/cppguide.html#Type_deduction).
633
634[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ExfSorNLNf4)
635***
636
637### using declaration for attributes <sup>[allowed]</sup>
638
639```c++
640[[using CC: opt(1), debug]] // same as [[CC:opt(1), CC::debug]]
641```
642
643**Description:** Specifies a common namespace for a list of attributes.
644
645**Documentation:**
646[Attribute specifier sequence](https://en.cppreference.com/w/cpp/language/attributes)
647
648**Notes:**
649*** promo
650See similar attribute macros in base/compiler_specific.h.
Bruce Dawsonbb51b0032022-01-14 19:24:22651***
652
Peter Kasting1865f2772021-12-23 21:23:58653## C++17 Allowed Library Features {#library-allowlist-17}
654
655The following C++17 language features are allowed in the Chromium codebase.
656
Peter Kasting6d77e9d2023-02-09 21:58:18657### 3D std::hypot <sup>[allowed]</sup>
Peter Kasting1865f2772021-12-23 21:23:58658
659```c++
Peter Kasting6d77e9d2023-02-09 21:58:18660double dist = std::hypot(1.0, 2.5, 3.7);
Peter Kasting1865f2772021-12-23 21:23:58661```
662
Peter Kasting6d77e9d2023-02-09 21:58:18663**Description:** Computes the distance from the origin in 3D space.
Peter Kasting1865f2772021-12-23 21:23:58664
665**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08666[`std::hypot`](https://en.cppreference.com/w/cpp/numeric/math/hypot)
Peter Kasting1865f2772021-12-23 21:23:58667
668**Notes:**
669*** promo
Peter Kasting6d77e9d2023-02-09 21:58:18670[Discussion thread](https://groups.google.com/u/1/a/chromium.org/g/cxx/c/jNMsxFTd30M)
Peter Kasting1865f2772021-12-23 21:23:58671***
672
Peter Kasting6d77e9d2023-02-09 21:58:18673### Searchers <sup>[allowed]</sup>
Daniel Chenga371b4c2022-01-14 18:24:27674
675```c++
Peter Kasting6d77e9d2023-02-09 21:58:18676auto it = std::search(haystack.begin(), haystack.end(),
677 std::boyer_moore_searcher(needle.begin(), needle.end()));
Daniel Chenga371b4c2022-01-14 18:24:27678```
679
Peter Kasting6d77e9d2023-02-09 21:58:18680**Description:** Alternate string searching algorithms.
Daniel Chenga371b4c2022-01-14 18:24:27681
682**Documentation:**
Peter Kasting6d77e9d2023-02-09 21:58:18683[Searchers](https://en.cppreference.com/w/cpp/utility/functional#Searchers)
Daniel Chenga371b4c2022-01-14 18:24:27684
685**Notes:**
686*** promo
Peter Kasting6d77e9d2023-02-09 21:58:18687[Discussion thread](https://groups.google.com/u/1/a/chromium.org/g/cxx/c/jNMsxFTd30M)
Daniel Chenga371b4c2022-01-14 18:24:27688***
689
Peter Kastingd51bed72022-09-09 19:40:00690### std::apply <sup>[allowed]</sup>
691
692```c++
693static_assert(std::apply(std::plus<>(), std::make_tuple(1, 2)) == 3);
694```
695
696**Description:** Invokes a `Callable` object with a tuple of arguments.
697
698**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08699[`std::apply`](https://en.cppreference.com/w/cpp/utility/apply)
Peter Kastingd51bed72022-09-09 19:40:00700
701**Notes:**
702*** promo
703[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/cNZm_g39fyM)
704***
705
Lei Zhangaaf6fc22022-10-06 23:12:42706### std::as_const <sup>[allowed]</sup>
707
708```c++
709auto&& const_ref = std::as_const(mutable_obj);
710```
711
712**Description:** Forms reference to const T.
713
714**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08715[`std::as_const`](https://en.cppreference.com/w/cpp/utility/as_const)
Lei Zhangaaf6fc22022-10-06 23:12:42716
717**Notes:**
718*** promo
719[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/5Uo4iJK6Mf4)
720***
721
Peter Kasting6d77e9d2023-02-09 21:58:18722### std::atomic<T>::is_always_lock_free <sup>[allowed]</sup>
Daniel Cheng4ddc9202022-02-24 20:19:26723
724```c++
Peter Kasting6d77e9d2023-02-09 21:58:18725template <typename T>
726struct is_lock_free_impl
727: std::integral_constant<bool, std::atomic<T>::is_always_lock_free> {};
Daniel Cheng4ddc9202022-02-24 20:19:26728```
729
Peter Kasting6d77e9d2023-02-09 21:58:18730**Description:** True when the given atomic type is always lock-free.
Daniel Cheng4ddc9202022-02-24 20:19:26731
732**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08733[`std::atomic<T>::is_always_lock_free`](https://en.cppreference.com/w/cpp/atomic/atomic/is_always_lock_free)
Daniel Cheng4ddc9202022-02-24 20:19:26734
735**Notes:**
736*** promo
Peter Kasting6d77e9d2023-02-09 21:58:18737[Discussion thread](https://groups.google.com/u/1/a/chromium.org/g/cxx/c/jNMsxFTd30M)
Avi Drissman67c3b1e2022-04-27 20:42:49738***
739
Peter Kastinge2c5ee82023-02-15 17:23:08740### std::{{con,dis}junction,negation} <sup>[allowed]</sup>
Avi Drissmanc109efd2022-04-27 22:03:35741
742```c++
743template<typename T, typename... Ts>
744std::enable_if_t<std::conjunction_v<std::is_same<T, Ts>...>>
745func(T, Ts...) { ...
746```
747
748**Description:** Performs logical operations on type traits.
749
750**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08751[`std::conjunction`](https://en.cppreference.com/w/cpp/types/conjunction),
752[`std::disjunction`](https://en.cppreference.com/w/cpp/types/disjunction),
753[`std::negation`](https://en.cppreference.com/w/cpp/types/negation)
Avi Drissmanc109efd2022-04-27 22:03:35754
755**Notes:**
756*** promo
Peter Kasting6d77e9d2023-02-09 21:58:18757[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/YhlF_sTDSc0)
Avi Drissmanc109efd2022-04-27 22:03:35758***
759
Peter Kasting6d77e9d2023-02-09 21:58:18760### std::exclusive_scan <sup>[allowed]</sup>
761
762```c++
763std::exclusive_scan(data.begin(), data.end(), output.begin());
764```
765
766**Description:** Like `std::inclusive_scan` but omits the current element from
767the written output at each step; that is, results are "one value behind" those
768of `std::inclusive_scan`.
769
770**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08771[`std::exclusive_scan`](https://en.cppreference.com/w/cpp/algorithm/exclusive_scan)
Peter Kasting6d77e9d2023-02-09 21:58:18772
773**Notes:**
774*** promo
775[Discussion thread](https://groups.google.com/u/1/a/chromium.org/g/cxx/c/jNMsxFTd30M)
776***
777
778### std::gcd <sup>[allowed]</sup>
779
780```c++
781static_assert(std::gcd(12, 18) == 6);
782```
783
784**Description:** Computes the greatest common divisor of its arguments.
785
786**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08787[`std::gcd`](https://en.cppreference.com/w/cpp/numeric/gcd)
Peter Kasting6d77e9d2023-02-09 21:58:18788
789**Notes:**
790*** promo
791[Discussion thread](https://groups.google.com/u/1/a/chromium.org/g/cxx/c/jNMsxFTd30M)
792***
793
794### std::has_unique_object_representations <sup>[allowed]</sup>
795
796```c++
797std::has_unique_object_representations_v<foo>
798```
799
800**Description:** Checks wither the given type is trivially copyable and any two
801objects with the same value have the same object representation.
802
803**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08804[`std::has_unique_object_representations`](https://en.cppreference.com/w/cpp/types/has_unique_object_representations)
Peter Kasting6d77e9d2023-02-09 21:58:18805
806**Notes:**
807*** promo
808[Discussion thread](https://groups.google.com/u/1/a/chromium.org/g/cxx/c/jNMsxFTd30M)
809***
810
811### std::inclusive_scan <sup>[allowed]</sup>
812
813```c++
814std::inclusive_scan(data.begin(), data.end(), output.begin());
815```
816
817**Description:** Like `std::accumulate` but writes the result at each step into
818the output range.
819
820**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08821[`std::inclusive_scan`](https://en.cppreference.com/w/cpp/algorithm/inclusive_scan)
Peter Kasting6d77e9d2023-02-09 21:58:18822
823**Notes:**
824*** promo
825[Discussion thread](https://groups.google.com/u/1/a/chromium.org/g/cxx/c/jNMsxFTd30M)
826***
827
Peter Kastinge2c5ee82023-02-15 17:23:08828### std::invoke <sup>[allowed]</sup>
829
830```c++
831static_assert(std::invoke(std::plus<>(), 1, 2) == 3);
832```
833
834**Description:** Invokes a callable object with parameters. A callable object is
835e.g. a function, function pointer, functor (that is, an object that provides
836`operator()`), lambda, etc.
837
838**Documentation:**
839[`std::invoke`](https://en.cppreference.com/w/cpp/utility/functional/invoke)
840
841**Notes:**
842*** promo
843[Migration bug](https://crbug.com/1412520)
844***
845
Peter Kasting6d77e9d2023-02-09 21:58:18846### std::is_aggregate <sup>[allowed]</sup>
847
848```c++
849if constexpr(std::is_aggregate_v<T>) { ...
850```
851
852**Description:** Checks wither the given type is an aggregate type.
853
854**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08855[`std::is_aggregate`](https://en.cppreference.com/w/cpp/types/is_aggregate)
Peter Kasting6d77e9d2023-02-09 21:58:18856
857**Notes:**
858*** promo
859[Discussion thread](https://groups.google.com/u/1/a/chromium.org/g/cxx/c/jNMsxFTd30M)
860***
861
862### std::is_invocable <sup>[allowed]</sup>
863
864```c++
865std::is_invocable_v<Fn, 1, "Hello">
866```
867
868**Description:** Checks whether a function may be invoked with the given
869argument types. The `_r` variant also evaluates whether the result is
870convertible to a given type.
871
872**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08873[`std::is_invocable`](https://en.cppreference.com/w/cpp/types/is_invocable)
Peter Kasting6d77e9d2023-02-09 21:58:18874
875**Notes:**
876*** promo
877[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/YhlF_sTDSc0)
878***
879
880### std::is_swappable <sup>[allowed]</sup>
881
882```c++
883std::is_swappable<T>
884std::is_swappable_with_v<T, U>
885```
886
887**Description:** Checks whether classes may be swapped.
888
889**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08890[`std::is_swappable`](https://en.cppreference.com/w/cpp/types/is_swappable)
Peter Kasting6d77e9d2023-02-09 21:58:18891
892**Notes:**
893*** promo
894[Discussion thread](https://groups.google.com/u/1/a/chromium.org/g/cxx/c/jNMsxFTd30M)
895***
896
897### std::launder <sup>[allowed]</sup>
898
899```c++
900struct Y { int z; };
901alignas(Y) std::byte s[sizeof(Y)];
902Y* q = new(&s) Y{2};
903const int h = std::launder(reinterpret_cast<Y*>(&s))->z;
904```
905
906**Description:** When used to wrap a pointer, makes it valid to access the
907resulting object in cases it otherwise wouldn't have been, in a very limited set
908of circumstances.
909
910**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08911[`std::launder`](https://en.cppreference.com/w/cpp/utility/launder)
Peter Kasting6d77e9d2023-02-09 21:58:18912
913**Notes:**
914*** promo
915[Discussion thread](https://groups.google.com/u/1/a/chromium.org/g/cxx/c/jNMsxFTd30M)
916***
917
918### std::lcm <sup>[allowed]</sup>
919
920```c++
921static_assert(std::lcm(12, 18) == 36);
922```
923
924**Description:** Computes the least common multiple of its arguments.
925
926**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08927[`std::lcm`](https://en.cppreference.com/w/cpp/numeric/lcm)
Peter Kasting6d77e9d2023-02-09 21:58:18928
929**Notes:**
930*** promo
931[Discussion thread](https://groups.google.com/u/1/a/chromium.org/g/cxx/c/jNMsxFTd30M)
932***
933
934### std::make_from_tuple <sup>[allowed]</sup>
935
936```c++
937// Calls Foo(int, double):
938auto foo = std::make_from_tuple<Foo>(std::make_tuple(1, 3.5));
939```
940
941**Description:** Constructs an object from a tuple of arguments.
942
943**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08944[`std::make_from_tuple`](https://en.cppreference.com/w/cpp/utility/make_from_tuple)
Peter Kasting6d77e9d2023-02-09 21:58:18945
946**Notes:**
947*** promo
948[Discussion thread](https://groups.google.com/u/1/a/chromium.org/g/cxx/c/jNMsxFTd30M)
949***
950
Peter Kastinge2c5ee82023-02-15 17:23:08951### std::map::{extract,merge} <sup>[allowed]</sup>
Peter Kasting6d77e9d2023-02-09 21:58:18952
953```c++
954std::map<...>::extract
955std::map<...>::merge
956std::set<...>::extract
957std::set<...>::merge
958```
959
960**Description:** Moving nodes and merging containers without the overhead of
961expensive copies, moves, or heap allocations/deallocations.
962
963**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08964[`std::map::extract`](https://en.cppreference.com/w/cpp/container/map/extract),
965[`std::map::merge`](https://en.cppreference.com/w/cpp/container/map/merge)
Peter Kasting6d77e9d2023-02-09 21:58:18966
967**Notes:**
968*** promo
969[Discussion thread](https://groups.google.com/u/1/a/chromium.org/g/cxx/c/jNMsxFTd30M)
970***
971
972### std::map::insert_or_assign <sup>[allowed]</sup>
973
974```c++
975std::map<std::string, std::string> m;
976m.insert_or_assign("c", "cherry");
977m.insert_or_assign("c", "clementine");
978```
979
980**Description:** Like `operator[]`, but returns more information and does not
981require default-constructibility of the mapped type.
982
983**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08984[`std::map::insert_or_assign`](https://en.cppreference.com/w/cpp/container/map/insert_or_assign)
Peter Kasting6d77e9d2023-02-09 21:58:18985
986**Notes:**
987*** promo
988[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/Uv2tUfIwUfQ)
989***
990
991### std::map::try_emplace <sup>[allowed]</sup>
992
993```c++
994std::map<std::string, std::string> m;
995m.try_emplace("c", 10, 'c');
996m.try_emplace("c", "Won't be inserted");
997```
998
999**Description:** Like `emplace`, but does not move from rvalue arguments if the
1000insertion does not happen.
1001
1002**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:081003[`std::map::try_emplace`](https://en.cppreference.com/w/cpp/container/map/try_emplace),
Peter Kasting6d77e9d2023-02-09 21:58:181004
1005**Notes:**
1006*** promo
1007[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/Uv2tUfIwUfQ)
1008***
1009
Peter Kastinge2c5ee82023-02-15 17:23:081010### std::not_fn <sup>[allowed]</sup>
1011
1012```c++
1013auto nonwhite = std::find_if(str.begin(), str.end(), std::not_fn(IsWhitespace));
1014```
1015
1016**Description:** Creates a forwarding call wrapper that returns the negation of
1017the callable object it holds.
1018
1019**Documentation:**
1020[`std::not_fn`](https://en.cppreference.com/w/cpp/utility/functional/not_fn)
1021
1022**Notes:**
1023*** promo
1024[Migration bug](https://crbug.com/1412529)
1025***
1026
1027### std::{size,empty,data} <sup>[allowed]</sup>
1028
1029```c++
1030char buffer[260];
1031memcpy(std::data(buffer), source_str.data(), std::size(buffer));
1032
1033if (!std::empty(container)) { ... }
1034```
1035
1036**Description:** Non-member versions of what are often member functions on STL
1037containers. Primarily useful when:
1038- using `std::size()` as a replacement for the old `arraysize()` macro.
1039- writing code that needs to generically operate across things like
1040 `std::vector` and `std::list` (which provide `size()`, `empty()`, and `data()
1041 member functions), `std::array` and `std::initialize_list` (which only provide
1042 a subset of the aforementioned member functions), and regular arrays (which
1043 have no member functions at all).
1044
1045**Documentation:**
1046[`std::size`](https://en.cppreference.com/w/cpp/iterator/size),
1047[`std::empty`](https://en.cppreference.com/w/cpp/iterator/empty),
1048[`std::data`](https://en.cppreference.com/w/cpp/iterator/data)
1049
1050**Notes:**
1051*** promo
1052[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/58qlA3zk5ZI)
1053
1054Prefer range-based for loops over `std::size()`: range-based for loops work even
1055for regular arrays.
1056***
1057
Peter Kasting6d77e9d2023-02-09 21:58:181058### Type trait variable templates <sup>[allowed]</sup>
1059
1060```c++
1061bool b = std::is_same_v<int, std::int32_t>;
1062```
1063
1064**Description:** Syntactic sugar to provide convenient access to `::value`
1065members by simply adding `_v`.
1066
1067**Documentation:**
1068[Type support](https://en.cppreference.com/w/cpp/types)
1069
1070**Notes:**
1071*** promo
1072[Discussion thread](Non://groups.google.com/a/chromium.org/g/cxx/c/KEa-0AOGRNY)
1073***
1074
1075### Uninitialized memory algorithms <sup>[allowed]</sup>
1076
1077```c++
Peter Kasting6d77e9d2023-02-09 21:58:181078std::destroy(ptr, ptr + 8);
Peter Kastinge2c5ee82023-02-15 17:23:081079std::destroy_at(ptr);
Peter Kasting6d77e9d2023-02-09 21:58:181080std::destroy_n(ptr, 8);
1081std::uninitialized_move(src.begin(), src.end(), dest.begin());
1082std::uninitialized_value_construct(std::begin(storage), std::end(storage));
1083```
1084
1085**Description:** Replaces direct constructor and destructor calls when manually
1086managing memory.
1087
1088**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:081089[`std::destroy`](https://en.cppreference.com/w/cpp/memory/destroy),
1090[`std::destroy_at`](https://en.cppreference.com/w/cpp/memory/destroy_at),
1091[`std::destroy_n`](https://en.cppreference.com/w/cpp/memory/destroy_n),
1092[`std::uninitialized_move`](https://en.cppreference.com/w/cpp/memory/uninitialized_move),
1093[`std::uninitialized_value_construct`](https://en.cppreference.com/w/cpp/memory/uninitialized_value_construct)
Peter Kasting6d77e9d2023-02-09 21:58:181094
1095**Notes:**
1096*** promo
1097[Discussion thread](https://groups.google.com/u/1/a/chromium.org/g/cxx/c/jNMsxFTd30M)
1098***
1099
1100## C++17 Banned Library Features {#library-blocklist-17}
1101
1102The following C++17 library features are not allowed in the Chromium codebase.
1103
1104### std::aligned_alloc <sup>[banned]</sup>
1105
1106```c++
1107int* p2 = static_cast<int*>(std::aligned_alloc(1024, 1024));
1108```
1109
1110**Description:** Allocates uninitialized storage with the specified alignment.
1111
1112**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:081113[`std::aligned_alloc`](https://en.cppreference.com/w/cpp/memory/c/aligned_alloc)
Peter Kasting6d77e9d2023-02-09 21:58:181114
1115**Notes:**
1116*** promo
1117[Will be allowed soon](https://crbug.com/1412818); for now, use
1118`base::AlignedAlloc`.
1119***
1120
1121### std::any <sup>[banned]</sup>
1122
1123```c++
1124std::any x = 5;
1125```
1126
1127**Description:** A type-safe container for single values of any type.
1128
1129**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:081130[`std::any`](https://en.cppreference.com/w/cpp/utility/any)
Peter Kasting6d77e9d2023-02-09 21:58:181131
1132**Notes:**
1133*** promo
1134[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/00cpZ07nye4)
1135
1136Banned since workaround for lack of RTTI isn't compatible with the component
1137build ([Bug](https://crbug.com/1096380)). Also see `absl::any`.
1138***
1139
1140### std::clamp <sup>[banned]</sup>
1141
1142```c++
1143int x = std::clamp(inp, 0, 100);
1144```
1145
1146**Description:** Clamps a value between a minimum and a maximum.
1147
1148**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:081149[`std::clamp`](https://en.cppreference.com/w/cpp/algorithm/clamp)
Peter Kasting6d77e9d2023-02-09 21:58:181150
1151**Notes:**
1152*** promo
Peter Kastinge2c5ee82023-02-15 17:23:081153[Will be allowed soon](https://crbug.com/1373621); for now, use `base::clamp`.
Peter Kasting6d77e9d2023-02-09 21:58:181154***
1155
1156### std::filesystem <sup>[banned]</sup>
1157
1158```c++
1159#include <filesystem>
1160```
1161
1162**Description:** A standard way to manipulate files, directories, and paths in a
1163filesystem.
1164
1165**Documentation:**
1166[Filesystem library](https://en.cppreference.com/w/cpp/filesystem)
1167
1168**Notes:**
1169*** promo
1170Banned by the [Google Style Guide](https://google.github.io/styleguide/cppguide.html#Other_Features).
1171***
1172
Peter Kastinge2c5ee82023-02-15 17:23:081173### std::hardware_{con,de}structive_interference_size <sup>[banned]</sup>
Anton Bikineevc6a022582022-10-10 19:08:581174
1175```c++
1176struct SharedData {
1177 ReadOnlyFrequentlyUsed data;
1178 alignas(std::hardware_destructive_interference_size) std::atomic<size_t> counter;
1179};
1180```
1181
1182**Description:** The `std::hardware_destructive_interference_size` constant is
1183useful to avoid false sharing (destructive interference) between variables that
1184would otherwise occupy the same cacheline. In contrast,
1185`std::hardware_constructive_interference_size` is helpful to promote true
1186sharing (constructive interference), e.g. to support better locality for
1187non-contended data.
1188
1189**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:081190[`std::hardware_destructive_interference_size`](https://en.cppreference.com/w/cpp/thread/hardware_destructive_interference_size),
1191[`std::hardware_constructive_interference_size`](https://en.cppreference.com/w/cpp/thread/hardware_destructive_interference_size)
Anton Bikineevc6a022582022-10-10 19:08:581192
1193**Notes:**
1194*** promo
Peter Kasting6d77e9d2023-02-09 21:58:181195Banned for now since these are
1196[not supported yet](https://github.com/llvm/llvm-project/issues/60174). Allow
1197once supported.
1198[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/cwktrFxxUY4)
Avi Drissman37b7d512022-04-01 22:01:401199***
1200
Peter Kastinge2c5ee82023-02-15 17:23:081201### std::in_place[_type,_index][_t] <sup>[banned]</sup>
Avi Drissmanbc6545f42022-05-03 17:47:381202
1203```c++
1204std::optional<std::complex<double>> opt{std::in_place, 0, 1};
1205std::variant<int, float> v{std::in_place_type<int>, 1.4};
1206```
1207
1208**Description:** The `std::in_place` are disambiguation tags for
1209`std::optional`, `std::variant`, and `std::any` to indicate that the object
1210should be constructed in-place.
1211
1212**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:081213[`std::in_place`](https://en.cppreference.com/w/cpp/utility/in_place)
Avi Drissmanbc6545f42022-05-03 17:47:381214
1215**Notes:**
1216*** promo
1217Banned for now because `std::optional`, `std::variant`, and `std::any` are all
1218banned for now. Because `absl::optional` and `absl::variant` are used instead,
1219and they require `absl::in_place`, use `absl::in_place` for non-Abseil Chromium
1220code. See the
Peter Kasting6d77e9d2023-02-09 21:58:181221[discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ZspmuJPpv6s).
Avi Drissmanbc6545f42022-05-03 17:47:381222***
1223
Peter Kasting6d77e9d2023-02-09 21:58:181224### std::optional <sup>[banned]</sup>
1225
1226```c++
1227std::optional<std::string> s;
1228```
1229
1230**Description:** The class template `std::optional` manages an optional
1231contained value, i.e. a value that may or may not be present. A common use case
1232for optional is the return value of a function that may fail.
1233
1234**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:081235[`std::optional`](https://en.cppreference.com/w/cpp/utility/optional)
Peter Kasting6d77e9d2023-02-09 21:58:181236
1237**Notes:**
1238*** promo
Peter Kastinge2c5ee82023-02-15 17:23:081239[Will be allowed soon](https://crbug.com/1373619); for now, use
Peter Kasting6d77e9d2023-02-09 21:58:181240`absl::optional`.
1241***
1242
1243### std::[u16]string_view <sup>[banned]</sup>
1244
1245```c++
1246std::string_view str = "foo";
1247std::u16string_view str16 = u"bar";
1248```
1249
1250**Description:** A non-owning reference to a string. Useful for providing an
1251abstraction on top of strings (e.g. for parsing).
1252
1253**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:081254[`std::basic_string_view`](https://en.cppreference.com/w/cpp/string/basic_string_view)
Peter Kasting6d77e9d2023-02-09 21:58:181255
1256**Notes:**
1257*** promo
1258[Will be allowed soon](https://crbug.com/691162); for now, use
1259`base::StringPiece[16]`.
1260***
1261
1262### std::uncaught_exceptions <sup>[banned]</sup>
1263
1264```c++
1265int count = std::uncaught_exceptions();
1266```
1267
1268**Description:** Determines whether there are live exception objects.
1269
1270**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:081271[`std::uncaught_exceptions`](https://en.cppreference.com/w/cpp/error/uncaught_exception)
Peter Kasting6d77e9d2023-02-09 21:58:181272
1273**Notes:**
1274*** promo
1275Banned because exceptions are banned.
1276***
1277
1278### std::variant <sup>[banned]</sup>
1279
1280```c++
1281std::variant<int, double> v = 12;
1282```
1283
1284**Description:** The class template `std::variant` represents a type-safe
1285`union`. An instance of `std::variant` at any given time holds a value of one of
1286its alternative types (it's also possible for it to be valueless).
1287
1288**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:081289[`std::variant`](https://en.cppreference.com/w/cpp/utility/variant)
Peter Kasting6d77e9d2023-02-09 21:58:181290
1291**Notes:**
1292*** promo
Peter Kastinge2c5ee82023-02-15 17:23:081293[Will be allowed soon](https://crbug.com/1373620); for now, use `absl::variant`.
Peter Kasting6d77e9d2023-02-09 21:58:181294***
1295
1296### Transparent std::owner_less <sup>[banned]</sup>
1297
1298```c++
1299std::map<std::weak_ptr<T>, U, std::owner_less<>>
1300```
1301
1302**Description:** Function object providing mixed-type owner-based ordering of
1303shared and weak pointers, regardless of the type of the pointee.
1304
1305**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:081306[`std::owner_less`](https://en.cppreference.com/w/cpp/memory/owner_less)
Peter Kasting6d77e9d2023-02-09 21:58:181307
1308**Notes:**
1309*** promo
1310Banned since `std::shared_ptr` and `std::weak_ptr` are banned.
1311***
1312
1313### weak_from_this <sup>[banned]</sup>
1314
1315```c++
1316auto weak_ptr = weak_from_this();
1317```
1318
1319**Description:** Returns a `std::weak_ptr<T>` that tracks ownership of `*this`
1320by all existing `std::shared_ptr`s that refer to `*this`.
1321
1322**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:081323[`std::enable_shared_from_this<T>::weak_from_this`](https://en.cppreference.com/w/cpp/memory/enable_shared_from_this/weak_from_this)
Peter Kasting6d77e9d2023-02-09 21:58:181324
1325**Notes:**
1326*** promo
1327Banned since `std::shared_ptr` and `std::weak_ptr` are banned.
Avi Drissman37b7d512022-04-01 22:01:401328***
1329
Peter Kasting1865f2772021-12-23 21:23:581330## C++17 TBD Language Features {#core-review-17}
1331
1332The following C++17 language features are not allowed in the Chromium codebase.
1333See the top of this page on how to propose moving a feature from this list into
1334the allowed or banned sections.
1335
Peter Kasting1865f2772021-12-23 21:23:581336### UTF-8 character literals <sup>[tbd]</sup>
1337
1338```c++
Peter Kastinge2c5ee82023-02-15 17:23:081339char x = u8'x'; // C++17
1340char8_t x = u8'x'; // C++20
Peter Kasting1865f2772021-12-23 21:23:581341```
1342
1343**Description:** A character literal that begins with `u8` is a character
Peter Kastinge2c5ee82023-02-15 17:23:081344literal of type `char` (C++17) or `char8_t` (C++20). The value of a UTF-8
1345character literal is equal to its ISO 10646 code point value.
Peter Kasting1865f2772021-12-23 21:23:581346
1347**Documentation:**
1348[Character literal](https://en.cppreference.com/w/cpp/language/character_literal)
1349
1350**Notes:**
1351*** promo
Peter Kastinge2c5ee82023-02-15 17:23:081352None
Peter Kasting1865f2772021-12-23 21:23:581353***
1354
Peter Kasting1865f2772021-12-23 21:23:581355## C++17 TBD Library Features {#library-review-17}
1356
1357The following C++17 library features are not allowed in the Chromium codebase.
1358See the top of this page on how to propose moving a feature from this list into
1359the allowed or banned sections.
1360
Peter Kasting1865f2772021-12-23 21:23:581361### Mathematical special functions <sup>[tbd]</sup>
1362
1363```c++
1364std::assoc_laguerre()
1365std::assoc_legendre()
1366std::beta()
1367std::comp_ellint_1()
1368std::comp_ellint_2()
1369std::comp_ellint_3()
1370std::cyl_bessel_i()
1371std::cyl_bessel_j()
1372std::cyl_bessel_k()
1373std::cyl_neumann()
1374std::ellint_1()
1375std::ellint_2()
1376std::ellint_3()
1377std::expint()
1378std::hermite()
1379std::legendre()
1380std::laguerre()
1381std::riemann_zeta()
1382std::sph_bessel()
1383std::sph_legendre()
1384std::sph_neumann()
1385```
1386
1387**Description:** A variety of mathematical functions.
1388
1389**Documentation:**
1390[Mathematical special functions](https://en.cppreference.com/w/cpp/numeric/special_functions)
1391
1392**Notes:**
1393*** promo
1394May not be supported in libc++, according to the
1395[library features table](https://en.cppreference.com/w/cpp/17)
1396***
1397
Peter Kasting6d77e9d2023-02-09 21:58:181398### Parallel algorithms <sup>[tbd]</sup>
Peter Kasting1865f2772021-12-23 21:23:581399
1400```c++
Peter Kasting6d77e9d2023-02-09 21:58:181401auto it = std::find(std::execution::par, std::begin(vec), std::end(vec), 2);
Peter Kasting1865f2772021-12-23 21:23:581402```
1403
Peter Kasting6d77e9d2023-02-09 21:58:181404**Description:** Many of the STL algorithms, such as the `copy`, `find` and
1405`sort` methods, now support the parallel execution policies: `seq`, `par`, and
1406`par_unseq` which translate to "sequentially", "parallel" and
1407"parallel unsequenced".
Peter Kasting1865f2772021-12-23 21:23:581408
1409**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:081410[`std::execution::sequenced_policy`, `std::execution::parallel_policy`, `std::execution::parallel_unsequenced_policy`, `std::execution::unsequenced_policy`](https://en.cppreference.com/w/cpp/algorithm/execution_policy_tag_t)
Peter Kasting1865f2772021-12-23 21:23:581411
1412**Notes:**
1413*** promo
Peter Kasting6d77e9d2023-02-09 21:58:181414May not be supported in libc++, according to the
1415[library features table](https://en.cppreference.com/w/cpp/17)
Peter Kasting1865f2772021-12-23 21:23:581416***
1417
Peter Kasting6d77e9d2023-02-09 21:58:181418### std::byte <sup>[tbd]</sup>
Peter Kasting1865f2772021-12-23 21:23:581419
1420```c++
Peter Kasting6d77e9d2023-02-09 21:58:181421std::byte b = 0xFF;
1422int i = std::to_integer<int>(b); // 0xFF
Peter Kasting1865f2772021-12-23 21:23:581423```
1424
Peter Kasting6d77e9d2023-02-09 21:58:181425**Description:** A standard way of representing data as a byte. `std::byte` is
1426neither a character type nor an arithmetic type, and the only operator overloads
1427available are bitwise operations.
Peter Kasting1865f2772021-12-23 21:23:581428
1429**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:081430[`std::byte`](https://en.cppreference.com/w/cpp/types/byte)
Peter Kasting6d77e9d2023-02-09 21:58:181431
1432**Notes:**
1433*** promo
1434No current consensus; see
1435[discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/bBY0gZa1Otk).
1436***
1437
Peter Kastinge2c5ee82023-02-15 17:23:081438### std::{pmr::memory_resource,polymorphic_allocator} <sup>[tbd]</sup>
Peter Kasting6d77e9d2023-02-09 21:58:181439
1440```c++
1441#include <memory_resource>
1442```
1443
1444**Description:** Manages memory allocations using runtime polymorphism.
1445
1446**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:081447[`std::pmr::memory_resource`](https://en.cppreference.com/w/cpp/memory/memory_resource),
1448[`std::pmr::polymorphic_allocator`](https://en.cppreference.com/w/cpp/memory/polymorphic_allocator)
Peter Kasting6d77e9d2023-02-09 21:58:181449
1450**Notes:**
1451*** promo
1452May not be supported in libc++, according to the
1453[library features table](https://en.cppreference.com/w/cpp/17)
1454***
1455
1456### std::reduce <sup>[tbd]</sup>
1457
1458```c++
1459std::reduce(std::execution::par, v.cbegin(), v.cend());
1460```
1461
1462**Description:** Like `std::accumulate` except the elements of the range may be
1463grouped and rearranged in arbitrary order.
1464
1465**Documentation:**
1466[std::reduce](https://en.cppreference.com/w/cpp/algorithm/reduce)
1467
1468**Notes:**
1469*** promo
1470Makes the most sense in conjunction with `std::execution::par`.
1471***
1472
1473### std::timespec_get <sup>[tbd]</sup>
1474
1475```c++
1476std::timespec ts;
1477std::timespec_get(&ts, TIME_UTC);
1478```
1479
1480**Description:** Gets the current calendar time in the given time base.
1481
1482**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:081483[`std::timespec_get`](https://en.cppreference.com/w/cpp/chrono/c/timespec_get)
Peter Kasting1865f2772021-12-23 21:23:581484
1485**Notes:**
1486*** promo
1487None
1488***
1489
Peter Kastinge2c5ee82023-02-15 17:23:081490### std::{from,to}_chars <sup>[tbd]</sup>
Peter Kasting1865f2772021-12-23 21:23:581491
1492```c++
Peter Kasting1865f2772021-12-23 21:23:581493std::from_chars(str.data(), str.data() + str.size(), result);
Peter Kastinge2c5ee82023-02-15 17:23:081494std::to_chars(str.data(), str.data() + str.size(), 42);
Peter Kasting1865f2772021-12-23 21:23:581495```
1496
1497**Description:** Locale-independent, non-allocating, non-throwing functions to
Peter Kastinge2c5ee82023-02-15 17:23:081498convert values from/to character strings, designed for use in high-throughput
Peter Kasting1865f2772021-12-23 21:23:581499contexts.
1500
1501**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:081502[`std::from_chars`](https://en.cppreference.com/w/cpp/utility/from_chars)
1503[`std::to_chars`](https://en.cppreference.com/w/cpp/utility/to_chars),
Peter Kasting1865f2772021-12-23 21:23:581504
1505**Notes:**
1506*** promo
1507None
1508***
1509
Arthur Sonzognid8517c92023-03-03 09:41:451510## C++20 Allowed Language Features {#core-allowlist-20}
1511
1512### Designated initializers <sup>[allowed]</sup>
1513
1514```c++
1515struct S { int x = 1; int y = 2; }
1516S s{ .y = 3 }; // OK, s.x == 1, s.y == 3
1517```
1518
1519**Description:** Allows explicit initialization of subsets of aggregate members
1520at construction.
1521
1522**Documentation:**
1523[Designated initializers](https://en.cppreference.com/w/cpp/language/aggregate_initialization#Designated_initializers)
1524
1525**Notes:**
1526*** promo
1527None
1528***
1529
Joe Masonfe4f2562021-09-15 15:23:131530## Abseil Banned Library Features {#absl-blocklist}
1531
1532The following Abseil library features are not allowed in the Chromium codebase.
1533
danakja6f71cb12021-12-15 21:04:491534### Any <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:131535
1536```c++
1537absl::any a = int{5};
1538EXPECT_THAT(absl::any_cast<int>(&a), Pointee(5));
1539EXPECT_EQ(absl::any_cast<size_t>(&a), nullptr);
1540```
1541
1542**Description:** Early adaptation of C++17 `std::any`.
1543
1544**Documentation:** [std::any](https://en.cppreference.com/w/cpp/utility/any)
1545
1546**Notes:**
1547*** promo
1548Banned since workaround for lack of RTTI isn't compatible with the component
Daniel Chengc05fcc62022-01-12 16:54:291549build ([Bug](https://crbug.com/1096380)). Also see `std::any`.
Joe Masonfe4f2562021-09-15 15:23:131550***
1551
Peter Kasting4f35bfc2022-10-18 18:39:121552### bind_front <sup>[banned]</sup>
1553
1554```c++
1555absl::bind_front
1556```
1557
1558**Description:** Binds the first N arguments of an invocable object and stores them by value.
1559
1560**Documentation:**
1561* [bind_front.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/bind_front.h)
1562* [Avoid std::bind](https://abseil.io/tips/108)
1563
1564**Notes:**
1565*** promo
1566Banned due to overlap with `base::Bind`. Use `base::Bind` instead.
1567***
1568
danakja6f71cb12021-12-15 21:04:491569### Command line flags <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:131570
1571```c++
1572ABSL_FLAG(bool, logs, false, "print logs to stderr");
1573app --logs=true;
1574```
1575
1576**Description:** Allows programmatic access to flag values passed on the
1577command-line to binaries.
1578
1579**Documentation:** [Flags Library](https://abseil.io/docs/cpp/guides/flags)
1580
1581**Notes:**
1582*** promo
1583Banned since workaround for lack of RTTI isn't compatible with the component
1584build. ([Bug](https://crbug.com/1096380)) Use `base::CommandLine` instead.
1585***
1586
Peter Kasting4f35bfc2022-10-18 18:39:121587### Container utilities <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:131588
1589```c++
Peter Kasting4f35bfc2022-10-18 18:39:121590auto it = absl::c_find(container, value);
Joe Masonfe4f2562021-09-15 15:23:131591```
1592
Peter Kasting4f35bfc2022-10-18 18:39:121593**Description:** Container-based versions of algorithmic functions within C++
1594standard library.
Joe Masonfe4f2562021-09-15 15:23:131595
Peter Kasting4f35bfc2022-10-18 18:39:121596**Documentation:**
1597[container.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/algorithm/container.h)
Joe Masonfe4f2562021-09-15 15:23:131598
1599**Notes:**
1600*** promo
Peter Kasting4f35bfc2022-10-18 18:39:121601Banned due to overlap with `base/ranges/algorithm.h`. Use the `base/ranges/`
1602facilities instead.
Joe Masonfe4f2562021-09-15 15:23:131603***
1604
Daniel Cheng2248b332022-07-27 06:16:591605### FunctionRef <sup>[banned]</sup>
1606
1607```c++
1608absl::FunctionRef
1609```
1610
1611**Description:** Type for holding a non-owning reference to an object of any
1612invocable type.
1613
1614**Documentation:**
1615[function_ref.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/function_ref.h)
1616
1617**Notes:**
1618*** promo
1619- `absl::FunctionRef` is banned due to allowing implicit conversions between
1620 function signatures in potentially surprising ways. For example, a callable
1621 with the signature `int()` will bind to `absl::FunctionRef<void()>`: the
1622 return value from the callable will be silently discarded.
1623- In Chromium, use `base::FunctionRef` instead.
1624- Unlike `base::OnceCallback` and `base::RepeatingCallback`, `base::FunctionRef`
1625 supports capturing lambdas.
1626- Useful when passing an invocable object to a function that synchronously calls
1627 the invocable object, e.g. `ForEachFrame(base::FunctionRef<void(Frame&)>)`.
1628 This can often result in clearer code than code that is templated to accept
1629 lambdas, e.g. with `template <typename Invocable> void
1630 ForEachFrame(Invocable invocable)`, it is much less obvious what arguments
1631 will be passed to `invocable`.
1632- For now, `base::OnceCallback` and `base::RepeatingCallback` intentionally
1633 disallow conversions to `base::FunctionRef`, under the theory that the
1634 callback should be a capturing lambda instead. Attempting to use this
1635 conversion will trigger a `static_assert` requesting additional feedback for
1636 use cases where this conversion would be valuable.
1637- *Important:* `base::FunctionRef` must not outlive the function call. Like
1638 `base::StringPiece`, `base::FunctionRef` is a *non-owning* reference. Using a
1639 `base::FunctionRef` as a return value or class field is dangerous and likely
1640 to result in lifetime bugs.
Peter Kasting6d77e9d2023-02-09 21:58:181641- [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/JVN4E4IIYA0)
Daniel Cheng2248b332022-07-27 06:16:591642***
1643
Peter Kasting4f35bfc2022-10-18 18:39:121644### Random <sup>[banned]</sup>
1645
1646```c++
1647absl::BitGen bitgen;
1648size_t index = absl::Uniform(bitgen, 0u, elems.size());
1649```
1650
1651**Description:** Functions and utilities for generating pseudorandom data.
1652
1653**Documentation:** [Random library](https://abseil.io/docs/cpp/guides/random)
1654
1655**Notes:**
1656*** promo
1657Banned because most uses of random values in Chromium should be using a
1658cryptographically secure generator. Use `base/rand_util.h` instead.
1659***
1660
1661### Span <sup>[banned]</sup>
1662
1663```c++
1664absl::Span
1665```
1666
1667**Description:** Early adaptation of C++20 `std::span`.
1668
1669**Documentation:** [Using absl::Span](https://abseil.io/tips/93)
1670
1671**Notes:**
1672*** promo
1673Banned due to being less std::-compliant than `base::span`. Keep using
1674`base::span`.
1675***
1676
1677### StatusOr <sup>[banned]</sup>
1678
1679```c++
1680absl::StatusOr<T>
1681```
1682
1683**Description:** An object that is either a usable value, or an error Status
1684explaining why such a value is not present.
1685
1686**Documentation:**
1687[statusor.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/status/statusor.h)
1688
1689**Notes:**
1690*** promo
1691Banned due to overlap with `base::expected`. Use `base::expected` instead.
1692***
1693
1694### String Formatting <sup>[banned]</sup>
1695
1696```c++
1697absl::StrFormat
1698```
1699
1700**Description:** A typesafe replacement for the family of printf() string
1701formatting routines.
1702
1703**Documentation:**
1704[String Formatting](https://abseil.io/docs/cpp/guides/format)
1705
1706**Notes:**
1707*** promo
1708Banned for now due to overlap with `base::StringPrintf()`. See
1709[migration bug](https://bugs.chromium.org/p/chromium/issues/detail?id=1371963).
1710***
1711
1712### string_view <sup>[banned]</sup>
1713
1714```c++
1715absl::string_view
1716```
1717
1718**Description:** Early adaptation of C++17 `std::string_view`.
1719
1720**Documentation:** [absl::string_view](https://abseil.io/tips/1)
1721
1722**Notes:**
1723*** promo
1724Banned due to only working with 8-bit characters. Keep using
1725`base::StringPiece` from `base/strings/`.
1726***
1727
1728### Strings Library <sup>[banned]</sup>
1729
1730```c++
1731absl::StrSplit
1732absl::StrJoin
1733absl::StrCat
1734absl::StrAppend
1735absl::Substitute
1736absl::StrContains
1737```
1738
1739**Description:** Classes and utility functions for manipulating and comparing
1740strings.
1741
1742**Documentation:**
1743[String Utilities](https://abseil.io/docs/cpp/guides/strings)
1744
1745**Notes:**
1746*** promo
1747Banned for now due to overlap with `base/strings`. We
1748[should re-evalute](https://bugs.chromium.org/p/chromium/issues/detail?id=1371966)
1749when we've
1750[migrated](https://bugs.chromium.org/p/chromium/issues/detail?id=691162) from
1751`base::StringPiece` to `std::string_view`.
1752***
1753
1754### Synchronization <sup>[banned]</sup>
1755
1756```c++
1757absl::Mutex
1758```
1759
1760**Description:** Primitives for managing tasks across different threads.
1761
1762**Documentation:**
1763[Synchronization](https://abseil.io/docs/cpp/guides/synchronization)
1764
1765**Notes:**
1766*** promo
1767Banned due to overlap with `base/synchronization/`. We would love
1768[more testing](https://bugs.chromium.org/p/chromium/issues/detail?id=1371969) on
1769whether there are compelling reasons to prefer base, absl, or std
1770synchronization primitives; for now, use `base/synchronization/`.
1771***
1772
1773### Time library <sup>[banned]</sup>
1774
1775```c++
1776absl::Duration
1777absl::Time
1778absl::TimeZone
1779absl::CivilDay
1780```
1781
1782**Description:** Abstractions for holding time values, both in terms of
1783absolute time and civil time.
1784
1785**Documentation:** [Time](https://abseil.io/docs/cpp/guides/time)
1786
1787**Notes:**
1788*** promo
1789Banned due to overlap with `base/time/`. Use `base/time/` instead.
1790***
1791
Joe Masonfe4f2562021-09-15 15:23:131792## Abseil TBD Features {#absl-review}
1793
1794The following Abseil library features are not allowed in the Chromium codebase.
1795See the top of this page on how to propose moving a feature from this list into
1796the allowed or banned sections.
1797
Danil Chapovalova9f27032022-06-20 16:56:141798### AnyInvocable <sup>[tbd]</sup>
1799
1800```c++
1801absl::AnyInvocable
1802```
1803
1804**Description:** An equivalent of the C++23 std::move_only_function.
1805
1806**Documentation:**
1807* [any_invocable.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/any_invocable.h)
1808* [std::move_only_function](https://en.cppreference.com/w/cpp/utility/functional/move_only_function/move_only_function)
1809
1810**Notes:**
1811*** promo
1812Overlaps with `base::RepeatingCallback`, `base::OnceCallback`.
1813***
1814
danakja6f71cb12021-12-15 21:04:491815### Containers <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:131816
1817```c++
1818absl::flat_hash_map
1819absl::flat_hash_set
1820absl::node_hash_map
1821absl::node_hash_set
1822absl::btree_map
1823absl::btree_set
1824absl::btree_multimap
1825absl::btree_multiset
1826absl::InlinedVector
1827absl::FixedArray
1828```
1829
1830**Description:** Alternatives to STL containers designed to be more efficient
1831in the general case.
1832
1833**Documentation:**
1834* [Containers](https://abseil.io/docs/cpp/guides/container)
1835* [Hash](https://abseil.io/docs/cpp/guides/hash)
1836
1837**Notes:**
1838*** promo
1839Supplements `base/containers/`.
1840***
1841
Mirko Bonadeide812cd2022-12-07 22:38:321842### CRC32C library <sup>[tbd]</sup>
1843
1844**Description:** API for computing CRC32C values as checksums for arbitrary
1845sequences of bytes provided as a string buffer.
1846
1847**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:081848[crc32.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/crc/crc32c.h)
Mirko Bonadeide812cd2022-12-07 22:38:321849
1850**Notes:**
1851*** promo
1852Overlaps with //third_party/crc32c.
1853***
1854
Peter Kasting4f35bfc2022-10-18 18:39:121855### Log macros and related classes <sup>[tbd]</sup>
Danil Chapovalov6719fb12022-08-31 13:52:491856
1857```c++
1858LOG(INFO) << message;
1859CHECK(condition);
1860absl::AddLogSink(&custom_sink_to_capture_absl_logs);
1861```
1862
1863**Description:** Macros and related classes to perform debug loggings
1864
1865**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:081866[log.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/log/log.h)
1867[check.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/log/check.h)
Danil Chapovalov6719fb12022-08-31 13:52:491868
1869**Notes:**
1870*** promo
1871Overlaps and uses same macros names as `base/logging.h`.
1872***