blob: 6c0de5ca75073a370e55f76a8ac7eb9f9eb66b8b [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++
Jan Keitel2bc3592b2025-05-02 14:51:581312template <typename T>
1313 requires (std::is_same_v<std::remove_cvref_t<T>, int>)
Peter Kastingf86817c2023-11-13 18:00:111314void foo(T t);
1315```
1316
1317**Description:** Provides a way to remove const, volatile, and reference
1318qualifiers from a type.
1319
1320**Documentation:**
1321[`std::remove_cvref`](https://en.cppreference.com/w/cpp/types/remove_cvref)
1322
1323**Notes:**
1324*** promo
Peter Kastingb2887c32024-01-08 21:41:041325None
Peter Kastingf86817c2023-11-13 18:00:111326***
1327
1328### std::ssize <sup>[allowed]</sup>
1329
1330```c++
1331str.replace(it, it + std::ssize(substr), 1, 'x');
1332```
1333
1334**Description:** Returns the size of an object as a signed type.
1335
1336**Documentation:**
1337[`std::size`, `std::ssize`](https://en.cppreference.com/w/cpp/iterator/size)
1338
1339**Notes:**
1340*** promo
1341[Migration bug](https://crbug.com/1414543)
1342***
1343
1344### std::string::(starts,ends)_with <sup>[allowed]</sup>
1345
1346```c++
1347const std::string str = "Foo bar";
1348const bool is_true = str.ends_with("bar");
1349```
1350
1351**Description:** Tests whether a string starts or ends with a particular
1352character or string.
1353
1354**Documentation:**
1355[`std::basic_string<CharT,Traits,Allocator>::starts_with`](https://en.cppreference.com/w/cpp/string/basic_string/starts_with),
1356[`std::basic_string<CharT,Traits,Allocator>::ends_with`](https://en.cppreference.com/w/cpp/string/basic_string/ends_with)
1357
1358**Notes:**
1359*** promo
1360[Migration bug](https://crbug.com/1414647)
1361***
1362
Peter Kastingf86817c2023-11-13 18:00:111363## C++20 Banned Language Features {#core-blocklist-20}
1364
1365The following C++20 language features are not allowed in the Chromium codebase.
1366
Peter Kasting72d110f12024-02-06 19:45:261367### char8_t <sup>[banned]</sup>
1368
1369```c++
1370char8_t c = u8'x';
1371```
1372
1373**Description:** A single UTF-8 code unit. Similar to `unsigned char`, but
1374considered a distinct type.
1375
1376**Documentation:**
1377[Fundamental types](https://en.cppreference.com/w/cpp/language/types#char8_t)
1378
1379**Notes:**
1380*** promo
1381Use `char` and unprefixed character literals. Non-UTF-8 encodings are rare
1382enough in Chromium that the value of distinguishing them at the type level is
1383low, and `char8_t*` is not interconvertible with `char*` (what ~all Chromium,
1384STL, and platform-specific APIs use), so using `u8` prefixes would obligate us
1385to insert casts everywhere. If you want to declare at a type level that a block
1386of data is string-like and not an arbitrary binary blob, prefer
1387`std::string[_view]` over `char*`.
1388***
1389
Peter Kastingf86817c2023-11-13 18:00:111390### Modules <sup>[banned]</sup>
1391
1392```c++
1393export module helloworld; // module declaration
1394
1395import <iostream>; // import declaration
1396
1397export void hello() { // export declaration
1398 std::cout << "Hello world!\n";
1399}
1400```
1401
1402**Description:** Modules provide an alternative to many uses of headers which
1403allows for faster compilation, better tooling support, and reduction of problems
1404like "include what you use".
1405
1406**Documentation:**
1407[Modules](https://en.cppreference.com/w/cpp/language/modules)
1408
1409**Notes:**
1410*** promo
1411Not yet sufficiently supported in Clang and GN. Re-evaluate when support
1412improves.
1413***
1414
Peter Kasting8bc046d22023-11-14 00:38:031415### [[no_unique_address]] <sup>[banned]</sup>
1416
1417```c++
1418struct Empty {};
1419struct X {
1420 int i;
1421 [[no_unique_address]] Empty e;
1422};
1423```
1424
1425**Description:** Allows a data member to be overlapped with other members.
1426
1427**Documentation:**
1428[C++ attribute: `no_unique_address`](https://en.cppreference.com/w/cpp/language/attributes/no_unique_address)
1429
1430**Notes:**
1431*** promo
1432Has no effect on Windows, for compatibility with Microsoft's ABI. Use
1433`NO_UNIQUE_ADDRESS` from `base/compiler_specific.h` instead. Do not use (either
1434form) on members of unions due to
1435[potential memory safety problems](https://github.com/llvm/llvm-project/issues/60711).
Peter Kasting8bc046d22023-11-14 00:38:031436***
1437
Peter Kastingf86817c2023-11-13 18:00:111438## C++20 Banned Library Features {#library-blocklist-20}
1439
1440The following C++20 library features are not allowed in the Chromium codebase.
1441
1442### std::atomic_ref <sup>[banned]</sup>
1443
1444```c++
1445struct S { int a; int b; };
1446S not_atomic;
1447std::atomic_ref<S> is_atomic(not_atomic);
1448```
1449
1450**Description:** Allows atomic access to objects that might not themselves be
1451atomic types. While any atomic_ref to an object exists, the object must be
1452accessed exclusively through atomic_ref instances.
1453
1454**Documentation:**
1455[`std::atomic_ref`](https://en.cppreference.com/w/cpp/atomic/atomic_ref)
1456
1457**Notes:**
1458*** promo
1459Banned due to being [unimplemented in libc++](https://reviews.llvm.org/D72240).
1460
1461[Migration bug](https://crbug.com/1422701) (once this is allowed)
1462***
1463
1464### std::bind_front <sup>[banned]</sup>
1465
1466```c++
1467int minus(int a, int b);
1468auto fifty_minus_x = std::bind_front(minus, 50);
1469int forty = fifty_minus_x(10);
1470```
1471
1472**Description:** An updated version of `std::bind` with fewer gotchas, similar
1473to `absl::bind_front`.
1474
1475**Documentation:**
1476[`std::bind_front`, `std::bind_back`](https://en.cppreference.com/w/cpp/utility/functional/bind_front)
1477
1478**Notes:**
1479*** promo
1480Overlaps with `base::Bind`.
1481***
1482
Avi Drissman70cb7f72023-12-12 17:44:371483### std::bit_cast <sup>[banned]</sup>
1484
1485```c++
1486float quake_rsqrt(float number) {
1487 long i = std::bit_cast<long>(number);
1488 i = 0x5f3759df - (i >> 1); // wtf?
1489 float y = std::bit_cast<float>(i);
1490 return y * (1.5f - (0.5f * number * y * y));
1491}
1492```
1493
1494**Description:** Returns an value constructed with the same bits as an value of
1495a different type.
1496
1497**Documentation:**
1498[`std::bit_cast`](https://en.cppreference.com/w/cpp/numeric/bit_cast)
1499
1500**Notes:**
1501*** promo
1502The `std::` version of `bit_cast` allows casting of pointer and reference types,
1503which is both useless in that it doesn't avoid UB, and dangerous in that it
1504allows arbitrary casting away of modifiers like `const`. Instead of using
1505`bit_cast` on pointers, use standard C++ casts. For use on values, use
1506`base::bit_cast` which does not allow this unwanted usage.
1507***
1508
Peter Kastingf86817c2023-11-13 18:00:111509### std::{c8rtomb,mbrtoc8} <sup>[banned]</sup>
1510
1511```c++
1512std::u8string_view strv = u8"zß水🍌";
1513std::mbstate_t state;
1514char out[MB_LEN_MAX] = {0};
1515for (char8_t c : strv) {
1516 size_t rc = std::c8rtomb(out, c, &state);
1517 ...
1518```
1519
1520**Description:** Converts a code point between UTF-8 and a multibyte character
1521encoded using the current C locale.
1522
1523**Documentation:**
1524[`std::c8rtomb`](https://en.cppreference.com/w/cpp/string/multibyte/c8rtomb),
1525[`std::mbrtoc8`](https://en.cppreference.com/w/cpp/string/multibyte/mbrtoc8)
1526
1527**Notes:**
1528*** promo
1529Chromium functionality should not vary with the C locale.
1530***
1531
Peter Kasting8e2424c2024-10-22 16:21:551532### Range factories and range adaptors <sup>[banned]</sup>
Daniel Cheng89719222024-07-04 04:59:291533
1534```c++
Daniel Cheng89719222024-07-04 04:59:291535// Prints 1, 2, 3, 4, 5, 6.
1536for (auto i : std::ranges::iota_view(1, 7)) {
1537 std::cout << i << '\n';
1538}
Peter Kasting8e2424c2024-10-22 16:21:551539
1540constexpr int kArr[] = {6, 2, 8, 4, 4, 2};
1541constexpr auto plus_one = std::views::transform([](int n){ return n + 1; });
1542static_assert(std::ranges::equal(kArr | plus_one, {7, 3, 9, 5, 5, 3}));
Daniel Cheng89719222024-07-04 04:59:291543```
1544
1545**Description:** Lightweight objects that represent iterable sequences.
1546Provides facilities for lazy operations on ranges, along with composition into
1547pipelines.
1548
1549**Documentation:**
1550[Ranges library](https://en.cppreference.com/w/cpp/ranges)
1551
1552**Notes:**
1553*** promo
1554Banned in Chrome due to questions about the design, impact on build time, and
1555runtime performance.
1556
1557[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ZnIbkfJ0Glw)
1558***
1559
Peter Kasting8e2424c2024-10-22 16:21:551560### std::ranges::view_interface <sup>[banned]</sup>
1561
1562```c++
1563class MyView : public std::ranges::view_interface<MyView> { ... };
1564```
1565
1566**Description:** CRTP base class for implementing custom view objects.
1567
1568**Documentation:**
1569[`std::ranges::view_interface`](https://en.cppreference.com/w/cpp/ranges/view_interface)
1570
1571**Notes:**
1572*** promo
1573Banned in Chrome since range factories and adapters are banned, and this would
1574primarily allow authors to create similar functionality.
1575
1576[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ZnIbkfJ0Glw)
1577***
1578
Peter Kastinge73b89d2024-11-26 19:35:521579### &lt;span&gt; <sup>[banned]</sup>
1580
1581```c++
1582#include <span>
1583```
1584
1585**Description:** Utilities for non-owning views over a sequence of objects.
1586
1587**Documentation:**
1588[](https://en.cppreference.com/w/cpp/header/span)
1589
1590**Notes:**
1591*** promo
1592Superseded by `base::span`, which has a richer functionality set.
1593***
1594
Nick Diego Yamanee522ae82024-02-27 04:23:221595### std::to_address <sup>[banned]</sup>
1596
1597```c++
1598std::vector<int> numbers;
1599int* i = std::to_address(numbers.begin());
1600```
1601
1602**Description:** Converts a pointer-like object to a pointer, even if the
1603pointer does not refer to a constructed object (in which case an expression like
1604`&*p` is UB).
1605
1606**Documentation:**
1607[`std::to_address`](https://en.cppreference.com/w/cpp/memory/to_address)
1608
1609**Notes:**
1610*** promo
1611Banned because it is not guaranteed to be SFINAE-compatible. Use
1612base::to_address, which does guarantee this.
1613***
1614
Peter Kastingf86817c2023-11-13 18:00:111615### &lt;syncstream&gt; <sup>[banned]</sup>
1616
1617```c++
1618#include <syncstream>
1619```
1620
1621**Description:** Facilities for multithreaded access to streams.
1622
1623**Documentation:**
1624[Standard library header `<syncstream>`](https://en.cppreference.com/w/cpp/header/syncstream)
1625
1626**Notes:**
1627*** promo
1628Banned due to being unimplemented per
1629[the libc++ C++20 status page](https://libcxx.llvm.org/Status/Cxx20.html).
1630Reevaluate usefulness once implemented.
1631***
1632
1633## C++20 TBD Language Features {#core-review-20}
1634
1635The following C++20 language features are not allowed in the Chromium codebase.
1636See the top of this page on how to propose moving a feature from this list into
1637the allowed or banned sections.
1638
1639### Aggregate initialization using parentheses <sup>[tbd]</sup>
1640
1641```c++
1642struct B {
1643 int a;
1644 int&& r;
1645} b2(1, 1); // Warning: dangling reference
1646```
1647
1648**Description:** Allows initialization of aggregates using parentheses, not just
1649braces.
1650
1651**Documentation:**
1652[Aggregate initialization](https://en.cppreference.com/w/cpp/language/aggregate_initialization),
1653[Direct initialization](https://en.cppreference.com/w/cpp/language/direct_initialization)
1654
1655**Notes:**
1656*** promo
1657There are subtle but important differences between brace- and paren-init of
1658aggregates. The parenthesis style appears to have more pitfalls (allowing
1659narrowing conversions, not extending lifetimes of temporaries bound to
1660references).
1661***
1662
Peter Kastingf86817c2023-11-13 18:00:111663### Coroutines <sup>[tbd]</sup>
1664
1665```c++
1666co_return 1;
1667```
1668
1669**Description:** Allows writing functions that logically block while physically
1670returning control to a caller. This enables writing some kinds of async code in
1671simple, straight-line ways without storing state in members or binding
1672callbacks.
1673
1674**Documentation:**
1675[Coroutines](https://en.cppreference.com/w/cpp/language/coroutines)
1676
1677**Notes:**
1678*** promo
1679Requires significant support code and planning around API and migration.
Peter Kastingf86817c2023-11-13 18:00:111680***
1681
Peter Kastingf86817c2023-11-13 18:00:111682## C++20 TBD Library Features {#library-review-20}
1683
1684The following C++20 library features are not allowed in the Chromium codebase.
1685See the top of this page on how to propose moving a feature from this list into
1686the allowed or banned sections.
1687
1688### &lt;coroutine&gt; <sup>[tbd]</sup>
1689
1690```c++
1691#include <coroutine>
1692```
1693
1694**Description:** Header which defines various core coroutine types.
1695
1696**Documentation:**
1697[Coroutine support](https://en.cppreference.com/w/cpp/coroutine)
1698
1699**Notes:**
1700*** promo
1701See notes on "Coroutines" above.
1702***
1703
1704### &lt;format&gt; <sup>[tbd]</sup>
1705
1706```c++
1707std::cout << std::format("Hello {}!\n", "world");
1708```
1709
1710**Description:** Utilities for producing formatted strings.
1711
1712**Documentation:**
1713[Formatting library](https://en.cppreference.com/w/cpp/utility/format)
1714
1715**Notes:**
1716*** promo
1717Has both pros and cons compared to `absl::StrFormat` (which we don't yet use).
1718Migration would be nontrivial.
1719***
1720
Peter Kastingf86817c2023-11-13 18:00:111721### &lt;source_location&gt; <sup>[tbd]</sup>
1722
1723```c++
1724#include <source_location>
1725```
1726
1727**Description:** Provides a class that can hold source code details such as
1728filenames, function names, and line numbers.
1729
1730**Documentation:**
1731[Standard library header `<source_location>`](https://en.cppreference.com/w/cpp/header/source_location)
1732
1733**Notes:**
1734*** promo
1735Seems to regress code size vs. `base::Location`.
1736***
1737
Peter Kastingf86817c2023-11-13 18:00:111738### std::u8string <sup>[tbd]</sup>
1739
1740```c++
1741std::u8string str = u8"Foo";
1742```
1743
1744**Description:** A string whose character type is `char8_t`, intended to hold
1745UTF-8-encoded text.
1746
1747**Documentation:**
1748[`std::basic_string`](https://en.cppreference.com/w/cpp/string/basic_string)
1749
1750**Notes:**
1751*** promo
1752See notes on `char8_t` above.
1753***
1754
Joe Masonfe4f2562021-09-15 15:23:131755## Abseil Banned Library Features {#absl-blocklist}
1756
1757The following Abseil library features are not allowed in the Chromium codebase.
1758
danakja6f71cb12021-12-15 21:04:491759### Any <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:131760
1761```c++
1762absl::any a = int{5};
1763EXPECT_THAT(absl::any_cast<int>(&a), Pointee(5));
1764EXPECT_EQ(absl::any_cast<size_t>(&a), nullptr);
1765```
1766
1767**Description:** Early adaptation of C++17 `std::any`.
1768
1769**Documentation:** [std::any](https://en.cppreference.com/w/cpp/utility/any)
1770
1771**Notes:**
1772*** promo
Peter Kasting72d110f12024-02-06 19:45:261773Banned since workaround for lack of RTTI
1774[isn't compatible with the component build](https://crbug.com/1096380). See also
1775`std::any`.
Joe Masonfe4f2562021-09-15 15:23:131776***
1777
Peter Kasting7fb5fbf2025-01-30 18:45:251778### AnyInvocable <sup>[banned]</sup>
1779
1780```c++
1781absl::AnyInvocable
1782```
1783
1784**Description:** An equivalent of the C++23 std::move_only_function.
1785
1786**Documentation:**
1787* [any_invocable.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/any_invocable.h)
1788* [std::move_only_function](https://en.cppreference.com/w/cpp/utility/functional/move_only_function/move_only_function)
1789
1790**Notes:**
1791*** promo
1792Banned due to overlap with `base::RepeatingCallback`, `base::OnceCallback`.
1793***
1794
Peter Kasting4b18d0c2024-09-18 00:56:111795### Attributes <sup>[banned]</sup>
1796
1797```c++
1798T* data() ABSL_ATTRIBUTE_LIFETIME_BOUND { return data_; }
1799ABSL_ATTRIBUTE_NO_TAIL_CALL ReturnType Loop();
1800struct S { bool b; int32_t i; } ABSL_ATTRIBUTE_PACKED;
1801```
1802
1803**Description:** Cross-platform macros to expose compiler-specific
1804functionality.
1805
1806**Documentation:** [attributes.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/base/attributes.h)
1807
1808**Notes:**
1809*** promo
1810Long names discourage use. Use standardized attributes over macros where
1811possible, and otherwise prefer shorter alternatives in
1812`base/compiler_specific.h`.
1813
1814[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/lVQOJTng1RU)
1815***
1816
John Admanski3bb2c1f2025-01-24 18:28:391817### btree_\* containers <sup>[banned]</sup>
1818
1819```c++
1820absl::btree_map
1821absl::btree_set
1822absl::btree_multimap
1823absl::btree_multiset
1824```
1825
1826**Description:** Alternatives to the tree-based standard library containers
1827designed to be more efficient in the general case.
1828
1829**Documentation:** [Containers](https://abseil.io/docs/cpp/guides/container)
1830
1831**Notes:**
1832*** promo
1833In theory these should be superior alternatives that could replace most uses of
1834`std::map` and company. In practice they have been found to introduce a
1835substantial code size increase. Until this problem can be resolved the use of
1836these containers is banned. Use the standard library containers instead.
1837***
1838
Peter Kasting4f35bfc2022-10-18 18:39:121839### bind_front <sup>[banned]</sup>
1840
1841```c++
1842absl::bind_front
1843```
1844
Peter Kasting3b77a0c2024-08-22 00:22:261845**Description:** Binds the first N arguments of an invocable object and stores
1846them by value.
Peter Kasting4f35bfc2022-10-18 18:39:121847
1848**Documentation:**
1849* [bind_front.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/bind_front.h)
1850* [Avoid std::bind](https://abseil.io/tips/108)
1851
1852**Notes:**
1853*** promo
Peter Kasting7fb5fbf2025-01-30 18:45:251854Banned due to overlap with `base::Bind`.
Peter Kasting4f35bfc2022-10-18 18:39:121855***
1856
danakja6f71cb12021-12-15 21:04:491857### Command line flags <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:131858
1859```c++
1860ABSL_FLAG(bool, logs, false, "print logs to stderr");
1861app --logs=true;
1862```
1863
1864**Description:** Allows programmatic access to flag values passed on the
1865command-line to binaries.
1866
1867**Documentation:** [Flags Library](https://abseil.io/docs/cpp/guides/flags)
1868
1869**Notes:**
1870*** promo
Peter Kasting72d110f12024-02-06 19:45:261871Banned since workaround for lack of RTTI
1872[isn't compatible with the component build](https://crbug.com/1096380). Use
1873`base::CommandLine` instead.
Joe Masonfe4f2562021-09-15 15:23:131874***
1875
Peter Kasting4f35bfc2022-10-18 18:39:121876### Container utilities <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:131877
1878```c++
Peter Kasting4f35bfc2022-10-18 18:39:121879auto it = absl::c_find(container, value);
Joe Masonfe4f2562021-09-15 15:23:131880```
1881
Peter Kasting4f35bfc2022-10-18 18:39:121882**Description:** Container-based versions of algorithmic functions within C++
1883standard library.
Joe Masonfe4f2562021-09-15 15:23:131884
Peter Kasting4f35bfc2022-10-18 18:39:121885**Documentation:**
1886[container.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/algorithm/container.h)
Joe Masonfe4f2562021-09-15 15:23:131887
1888**Notes:**
1889*** promo
Peter Kasting7fb5fbf2025-01-30 18:45:251890Superseded by algorithms in `std::ranges::`.
Joe Masonfe4f2562021-09-15 15:23:131891***
1892
Peter Kasting431239a2023-09-29 03:11:441893### FixedArray <sup>[banned]</sup>
1894
1895```c++
1896absl::FixedArray<MyObj> objs_;
1897```
1898
1899**Description:** A fixed size array like `std::array`, but with size determined
1900at runtime instead of compile time.
1901
1902**Documentation:**
1903[fixed_array.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/container/fixed_array.h)
1904
1905**Notes:**
1906*** promo
1907Direct construction is banned due to the risk of UB with uninitialized
1908trivially-default-constructible types. Instead use `base/types/fixed_array.h`,
1909which is a light-weight wrapper that deletes the problematic constructor.
Joe Mason6a6f2582024-01-30 20:26:431910***
Peter Kasting431239a2023-09-29 03:11:441911
Daniel Cheng2248b332022-07-27 06:16:591912### FunctionRef <sup>[banned]</sup>
1913
1914```c++
1915absl::FunctionRef
1916```
1917
1918**Description:** Type for holding a non-owning reference to an object of any
1919invocable type.
1920
1921**Documentation:**
1922[function_ref.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/function_ref.h)
1923
1924**Notes:**
1925*** promo
1926- `absl::FunctionRef` is banned due to allowing implicit conversions between
1927 function signatures in potentially surprising ways. For example, a callable
1928 with the signature `int()` will bind to `absl::FunctionRef<void()>`: the
1929 return value from the callable will be silently discarded.
1930- In Chromium, use `base::FunctionRef` instead.
1931- Unlike `base::OnceCallback` and `base::RepeatingCallback`, `base::FunctionRef`
1932 supports capturing lambdas.
1933- Useful when passing an invocable object to a function that synchronously calls
1934 the invocable object, e.g. `ForEachFrame(base::FunctionRef<void(Frame&)>)`.
1935 This can often result in clearer code than code that is templated to accept
1936 lambdas, e.g. with `template <typename Invocable> void
1937 ForEachFrame(Invocable invocable)`, it is much less obvious what arguments
1938 will be passed to `invocable`.
1939- For now, `base::OnceCallback` and `base::RepeatingCallback` intentionally
1940 disallow conversions to `base::FunctionRef`, under the theory that the
1941 callback should be a capturing lambda instead. Attempting to use this
1942 conversion will trigger a `static_assert` requesting additional feedback for
1943 use cases where this conversion would be valuable.
1944- *Important:* `base::FunctionRef` must not outlive the function call. Like
Helmut Januschka1dce9dc2024-06-11 13:05:351945 `std::string_view`, `base::FunctionRef` is a *non-owning* reference. Using a
Daniel Cheng2248b332022-07-27 06:16:591946 `base::FunctionRef` as a return value or class field is dangerous and likely
1947 to result in lifetime bugs.
Peter Kasting72d110f12024-02-06 19:45:261948
1949[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/JVN4E4IIYA0)
Daniel Cheng2248b332022-07-27 06:16:591950***
1951
Peter Kasting7fb5fbf2025-01-30 18:45:251952### Log macros and related classes <sup>[banned]</sup>
1953
1954```c++
1955LOG(INFO) << message;
1956CHECK(condition);
1957absl::AddLogSink(&custom_sink_to_capture_absl_logs);
1958```
1959
1960**Description:** Macros and related classes to perform debug loggings
1961
1962**Documentation:**
1963[log.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/log/log.h)
1964[check.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/log/check.h)
1965
1966**Notes:**
1967*** promo
1968Banned due to overlap with `base/logging.h`. We'd like to drop Chromium's
1969version and replace with the Abseil one, but no one has looked into how to
1970migrate and what impacts (e.g. build time) we'd incur. If you'd like to do this
1971work, please contact cxx@.
1972***
1973
1974### NoDestructor <sup>[banned]</sup>
1975
1976```c++
1977// Global or namespace scope.
1978ABSL_CONST_INIT absl::NoDestructor<MyRegistry> reg{"foo", "bar", 8008};
1979
1980// Function scope.
1981const std::string& MyString() {
1982 static const absl::NoDestructor<std::string> x("foo");
1983 return *x;
1984}
1985```
1986
1987**Description:** `absl::NoDestructor<T>` is a wrapper around an object of
1988type T that behaves as an object of type T but never calls T's destructor.
1989
1990**Documentation:**
1991[no_destructor.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/base/no_desctructor.h)
1992
1993**Notes:**
1994*** promo
1995Overlaps with `base::NoDestructor`. Banned pending rewriting friending of that
1996class into a form usable with this (see
1997[crbug.com/392931072](https://crbug.com/392931072)); at that point we can allow
1998this and migrate to it.
1999***
2000
2001### Nullability annotations <sup>[banned]</sup>
2002
2003```c++
2004void PaySalary(absl::NotNull<Employee *> employee) {
2005 pay(*employee); // OK to dereference
2006}
2007```
2008
2009**Description:** Annotations to more clearly specify contracts
2010
2011**Documentation:**
2012[nullability.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/base/nullability.h)
2013
2014**Notes:**
2015*** promo
2016Banned due to no feasible path to codebase-wide use and little mechanism for
2017enforcement.
2018***
2019
Peter Kasting3b77a0c2024-08-22 00:22:262020### Optional <sup>[banned]</sup>
2021
2022```c++
2023absl::optional<int> Func(bool b) {
2024 return b ? absl::make_optional(1) : abl::nullopt;
2025}
2026```
2027
2028**Description:** Early adaptation of C++17 `std::optional`.
2029
2030**Documentation:** [std::optional](https://en.cppreference.com/w/cpp/utility/optional)
2031
2032**Notes:**
2033*** promo
2034Superseded by `std::optional`. Use `std::optional` instead.
2035***
2036
Peter Kasting4f35bfc2022-10-18 18:39:122037### Random <sup>[banned]</sup>
2038
2039```c++
2040absl::BitGen bitgen;
2041size_t index = absl::Uniform(bitgen, 0u, elems.size());
2042```
2043
2044**Description:** Functions and utilities for generating pseudorandom data.
2045
2046**Documentation:** [Random library](https://abseil.io/docs/cpp/guides/random)
2047
2048**Notes:**
2049*** promo
2050Banned because most uses of random values in Chromium should be using a
2051cryptographically secure generator. Use `base/rand_util.h` instead.
2052***
2053
2054### Span <sup>[banned]</sup>
2055
2056```c++
2057absl::Span
2058```
2059
2060**Description:** Early adaptation of C++20 `std::span`.
2061
2062**Documentation:** [Using absl::Span](https://abseil.io/tips/93)
2063
2064**Notes:**
2065*** promo
2066Banned due to being less std::-compliant than `base::span`. Keep using
2067`base::span`.
2068***
2069
2070### StatusOr <sup>[banned]</sup>
2071
2072```c++
2073absl::StatusOr<T>
2074```
2075
2076**Description:** An object that is either a usable value, or an error Status
2077explaining why such a value is not present.
2078
2079**Documentation:**
2080[statusor.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/status/statusor.h)
2081
2082**Notes:**
2083*** promo
Peter Kasting72d110f12024-02-06 19:45:262084Overlaps with `base::expected`.
Peter Kasting4f35bfc2022-10-18 18:39:122085***
2086
Peter Kasting4f35bfc2022-10-18 18:39:122087### string_view <sup>[banned]</sup>
2088
2089```c++
2090absl::string_view
2091```
2092
2093**Description:** Early adaptation of C++17 `std::string_view`.
2094
2095**Documentation:** [absl::string_view](https://abseil.io/tips/1)
2096
2097**Notes:**
2098*** promo
David Benjaminea985a22023-04-18 22:05:012099Originally banned due to only working with 8-bit characters. Now it is
2100unnecessary because, in Chromium, it is the same type as `std::string_view`.
Hong Xu5e492d32023-07-27 21:38:462101Please use `std::string_view` instead.
Peter Kasting4f35bfc2022-10-18 18:39:122102***
2103
2104### Strings Library <sup>[banned]</sup>
2105
2106```c++
2107absl::StrSplit
2108absl::StrJoin
2109absl::StrCat
2110absl::StrAppend
2111absl::Substitute
2112absl::StrContains
2113```
2114
2115**Description:** Classes and utility functions for manipulating and comparing
2116strings.
2117
2118**Documentation:**
2119[String Utilities](https://abseil.io/docs/cpp/guides/strings)
2120
2121**Notes:**
2122*** promo
Peter Kasting72d110f12024-02-06 19:45:262123Overlaps with `base/strings`. We
Peter Kasting4f35bfc2022-10-18 18:39:122124[should re-evalute](https://bugs.chromium.org/p/chromium/issues/detail?id=1371966)
2125when we've
2126[migrated](https://bugs.chromium.org/p/chromium/issues/detail?id=691162) from
Peter Kasting0efc9042024-09-17 15:48:432127`base::StringPiece` to `std::string_view`. Also note that `absl::StrFormat()` is
2128not considered part of this group, and is explicitly allowed.
Peter Kasting4f35bfc2022-10-18 18:39:122129***
2130
2131### Synchronization <sup>[banned]</sup>
2132
2133```c++
2134absl::Mutex
2135```
2136
2137**Description:** Primitives for managing tasks across different threads.
2138
2139**Documentation:**
2140[Synchronization](https://abseil.io/docs/cpp/guides/synchronization)
2141
2142**Notes:**
2143*** promo
Peter Kasting72d110f12024-02-06 19:45:262144Overlaps with `base/synchronization/`. We would love
Peter Kasting4f35bfc2022-10-18 18:39:122145[more testing](https://bugs.chromium.org/p/chromium/issues/detail?id=1371969) on
2146whether there are compelling reasons to prefer base, absl, or std
2147synchronization primitives; for now, use `base/synchronization/`.
2148***
2149
2150### Time library <sup>[banned]</sup>
2151
2152```c++
2153absl::Duration
2154absl::Time
2155absl::TimeZone
2156absl::CivilDay
2157```
2158
2159**Description:** Abstractions for holding time values, both in terms of
2160absolute time and civil time.
2161
2162**Documentation:** [Time](https://abseil.io/docs/cpp/guides/time)
2163
2164**Notes:**
2165*** promo
Peter Kasting72d110f12024-02-06 19:45:262166Overlaps with `base/time/`.
Peter Kasting4f35bfc2022-10-18 18:39:122167***
Victor Hugo Vianna Silva6e84e8d42025-03-19 00:32:002168
2169### Variant <sup>[banned]</sup>
2170
2171```c++
2172absl::bad_variant_access;
2173absl::get;
2174absl::get_if;
2175absl::holds_alternative;
2176absl::monostate;
2177absl::variant;
2178absl::variant_alternative;
2179absl::variant_alternative_t;
2180absl::variant_npos;
2181absl::variant_size;
2182absl::variant_size_v;
2183absl::visit;
2184```
2185
2186**Description:** A backport of C++17's std::variant type-safe union and related utilities.
2187
2188**Notes:**
2189*** promo
2190These are just aliases to the std counterparts these days. Use std instead.
2191***
2192
2193### Utility library <sup>[banned]</sup>
2194
2195```c++
2196absl::apply;
2197absl::exchange;
2198absl::forward;
2199absl::in_place;
2200absl::in_place_index;
2201absl::in_place_index_t;
2202absl::in_place_t;
2203absl::in_place_type;
2204absl::in_place_type_t;
2205absl::index_sequence;
2206absl::index_sequence_for;
2207absl::integer_sequence;
2208absl::make_from_tuple;
2209absl::make_index_sequence;
2210absl::make_integer_sequence;
2211absl::move;
2212```
2213
2214**Description:** Backports of various C++17 template utilities.
2215
2216**Notes:**
2217*** promo
2218These are just aliases to the std counterparts these days. Use std instead.
2219***