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