blob: 538e5858f2885a472175196f381dc945c966dcf8 [file] [log] [blame] [view]
Joe Masonfe4f2562021-09-15 15:23:131# Modern C++ use in Chromium
2
3_This document is part of the more general
4[Chromium C++ style guide](https://chromium.googlesource.com/chromium/src/+/main/styleguide/c++/c++.md).
5It summarizes the supported state of new and updated language and library
6features in recent C++ standards and the [Abseil](https://abseil.io/about/)
7library. This guide applies to both Chromium and its subprojects, though
8subprojects can choose to be more restrictive if necessary for toolchain
9support._
10
11The C++ language has in recent years received an updated standard every three
Peter Kasting1865f2772021-12-23 21:23:5812years (C++11, C++14, etc.). For various reasons, Chromium does not immediately
Joe Masonfe4f2562021-09-15 15:23:1313allow new features on the publication of such a standard. Instead, once
Hong Xu68ba51a02022-04-27 22:21:3514Chromium supports the toolchain to a certain extent (e.g., build support is
15ready), a standard is declared "_initially supported_", with new
16language/library features banned pending discussion but not yet allowed.
Joe Masonfe4f2562021-09-15 15:23:1317
18You can propose changing the status of a feature by sending an email to
19[[email protected]](https://groups.google.com/a/chromium.org/forum/#!forum/cxx).
20Include a short blurb on what the feature is and why you think it should or
21should not be allowed, along with links to any relevant previous discussion. If
22the list arrives at some consensus, send a codereview to change this file
23accordingly, linking to your discussion thread.
24
25If an item remains on the TBD list two years after initial support is added,
26style arbiters should explicitly move it to an appropriate allowlist or
27blocklist, allowing it if there are no obvious reasons to ban.
28
29The current status of existing standards and Abseil features is:
30
31* **C++11:** _Default allowed; see banned features below_
Peter Kastinge2c5ee82023-02-15 17:23:0832* **C++14:** _Default allowed_
Peter Kasting72d110f12024-02-06 19:45:2633* **C++17:** _Default allowed; see banned features below_
Peter Kastingf86817c2023-11-13 18:00:1134* **C++20:** _Initially supported November 13, 2023; see allowed/banned/TBD
35 features below_
36* **C++23:** _Not yet officially standardized_
Danil Chapovalov0e8ed132023-09-28 08:05:3937* **Abseil:** _Default allowed; see banned/TBD features below. The following
38 dates represent the start of the two-year TBD periods for certain parts of
39 Abseil:_
40 * absl::AnyInvocable: Initially added to third_party June 20, 2022
41 * Log library: Initially added to third_party Aug 31, 2022
42 * CRC32C library: Initially added to third_party Dec 5, 2022
43 * Nullability annotation: Initially added to third_party Jun 21, 2023
44 * Overload: Initially added to third_party Sep 27, 2023
Mirko Bonadeidc928a162023-11-20 08:24:2345 * NoDestructor: Initially added to third_party Nov 15, 2023
Joe Masonfe4f2562021-09-15 15:23:1346
Joe Mason6a6f2582024-01-30 20:26:4347## Banned features and third-party code
48
49Third-party libraries may generally use banned features internally, although features
50with poor compiler support or poor security properties may make the library
51unsuitable to use with Chromium.
52
53Chromium code that calls functions exported from a third-party library may use
54banned library types that are required by the interface, as long as:
55
56 * The disallowed type is used only at the interface, and converted to and from
57 an equivalent allowed type as soon as practical on the Chromium side.
58 * The feature is not banned due to security issues or lack of compiler support.
59 If it is, discuss with
60 [[email protected]](https://groups.google.com/a/chromium.org/forum/#!forum/cxx)
61 to find a workaround.
62
Joe Masonfe4f2562021-09-15 15:23:1363[TOC]
64
Peter Kasting1865f2772021-12-23 21:23:5865## C++11 Banned Language Features {#core-blocklist-11}
Joe Masonfe4f2562021-09-15 15:23:1366
67The following C++11 language features are not allowed in the Chromium codebase.
68
danakja6f71cb12021-12-15 21:04:4969### Inline Namespaces <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:1370
71```c++
72inline namespace foo { ... }
73```
74
75**Description:** Allows better versioning of namespaces.
76
77**Documentation:**
78[Inline namespaces](https://en.cppreference.com/w/cpp/language/namespace#Inline_namespaces)
79
80**Notes:**
81*** promo
82Banned in the
83[Google Style Guide](https://google.github.io/styleguide/cppguide.html#Namespaces).
84Unclear how it will work with components.
85***
86
danakja6f71cb12021-12-15 21:04:4987### long long Type <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:1388
89```c++
90long long var = value;
91```
92
93**Description:** An integer of at least 64 bits.
94
95**Documentation:**
96[Fundamental types](https://en.cppreference.com/w/cpp/language/types)
97
98**Notes:**
99*** promo
Peter Kastinge2c5ee82023-02-15 17:23:08100Use a `<stdint.h>` type if you need a 64-bit number.
Peter Kasting72d110f12024-02-06 19:45:26101
Joe Masonfe4f2562021-09-15 15:23:13102[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/chromium-dev/RxugZ-pIDxk)
103***
104
danakja6f71cb12021-12-15 21:04:49105### User-Defined Literals <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13106
107```c++
Peter Kastinge2c5ee82023-02-15 17:23:08108DistanceType var = 12_km;
Joe Masonfe4f2562021-09-15 15:23:13109```
110
111**Description:** Allows user-defined literal expressions.
112
113**Documentation:**
114[User-defined literals](https://en.cppreference.com/w/cpp/language/user_literal)
115
116**Notes:**
117*** promo
118Banned in the
119[Google Style Guide](https://google.github.io/styleguide/cppguide.html#Operator_Overloading).
120***
121
Peter Kasting1865f2772021-12-23 21:23:58122## C++11 Banned Library Features {#library-blocklist-11}
Joe Masonfe4f2562021-09-15 15:23:13123
124The following C++11 library features are not allowed in the Chromium codebase.
125
Peter Kasting6f79b202023-08-09 21:25:41126### &lt;cctype&gt;, &lt;ctype.h&gt;, &lt;cwctype&gt;, &lt;wctype.h&gt; <sup>[banned]</sup>
127
128```c++
129#include <cctype>
130#include <cwctype>
131#include <ctype.h>
132#include <wctype.h>
133```
134
135**Description:** Provides utilities for ASCII characters.
136
137**Documentation:**
138[Standard library header `<cctype>`](https://en.cppreference.com/w/cpp/header/cctype),
139[Standard library header `<cwctype>`](https://en.cppreference.com/w/cpp/header/cwctype)
140
141**Notes:**
142*** promo
143Banned due to dependence on the C locale as well as UB when arguments don't fit
144in an `unsigned char`/`wchar_t`. Use similarly-named replacements in
145[third_party/abseil-cpp/absl/strings/ascii.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/strings/ascii.h)
146instead.
147***
148
Peter Kastinge2c5ee82023-02-15 17:23:08149### &lt;cfenv&gt;, &lt;fenv.h&gt; <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13150
151```c++
152#include <cfenv>
153#include <fenv.h>
154```
155
156**Description:** Provides floating point status flags and control modes for
Peter Kastinge2c5ee82023-02-15 17:23:08157C-compatible code.
Joe Masonfe4f2562021-09-15 15:23:13158
159**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08160[Standard library header `<cfenv>`](https://en.cppreference.com/w/cpp/header/cfenv)
Joe Masonfe4f2562021-09-15 15:23:13161
162**Notes:**
163*** promo
164Banned by the
165[Google Style Guide](https://google.github.io/styleguide/cppguide.html#C++11)
166due to concerns about compiler support.
167***
168
Peter Kastinge2c5ee82023-02-15 17:23:08169### &lt;chrono&gt; <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13170
171```c++
172#include <chrono>
173```
174
Peter Kastinge2c5ee82023-02-15 17:23:08175**Description:** A standard date and time library.
Joe Masonfe4f2562021-09-15 15:23:13176
177**Documentation:**
178[Date and time utilities](https://en.cppreference.com/w/cpp/chrono)
179
180**Notes:**
181*** promo
Peter Kasting72d110f12024-02-06 19:45:26182Overlaps with `base/time`.
Joe Masonfe4f2562021-09-15 15:23:13183***
184
Peter Kastinge2c5ee82023-02-15 17:23:08185### &lt;exception&gt; <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13186
187```c++
188#include <exception>
189```
190
Peter Kastinge2c5ee82023-02-15 17:23:08191**Description:** Exception throwing and handling.
Joe Masonfe4f2562021-09-15 15:23:13192
193**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08194[Standard library header `<exception>`](https://en.cppreference.com/w/cpp/header/exception)
Joe Masonfe4f2562021-09-15 15:23:13195
196**Notes:**
197*** promo
198Exceptions are banned by the
199[Google Style Guide](https://google.github.io/styleguide/cppguide.html#Exceptions)
200and disabled in Chromium compiles. However, the `noexcept` specifier is
201explicitly allowed.
Peter Kastinge2c5ee82023-02-15 17:23:08202
Joe Masonfe4f2562021-09-15 15:23:13203[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/chromium-dev/8i4tMqNpHhg)
204***
205
Peter Kastingc7460d982023-03-14 21:01:42206### Engines And Generators From &lt;random&gt; <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13207
208```c++
Peter Kastingc7460d982023-03-14 21:01:42209std::mt19937 generator;
Joe Masonfe4f2562021-09-15 15:23:13210```
211
Peter Kastingc7460d982023-03-14 21:01:42212**Description:** Methods of generating random numbers.
Joe Masonfe4f2562021-09-15 15:23:13213
214**Documentation:**
215[Pseudo-random number generation](https://en.cppreference.com/w/cpp/numeric/random)
216
217**Notes:**
218*** promo
Peter Kastingc7460d982023-03-14 21:01:42219Do not use any random number engines or generators from `<random>`. Instead, use
220`base::RandomBitGenerator`. (You may use the distributions from `<random>`.)
Peter Kastinge2c5ee82023-02-15 17:23:08221
Joe Masonfe4f2562021-09-15 15:23:13222[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/16Xmw05C-Y0)
223***
224
Peter Kastinge2c5ee82023-02-15 17:23:08225### &lt;ratio&gt; <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13226
227```c++
Peter Kastinge2c5ee82023-02-15 17:23:08228#include <ratio>
Joe Masonfe4f2562021-09-15 15:23:13229```
230
Peter Kastinge2c5ee82023-02-15 17:23:08231**Description:** Provides compile-time rational numbers.
Joe Masonfe4f2562021-09-15 15:23:13232
233**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08234[`std::ratio`](https://en.cppreference.com/w/cpp/numeric/ratio/ratio)
Joe Masonfe4f2562021-09-15 15:23:13235
236**Notes:**
237*** promo
238Banned by the
239[Google Style Guide](https://google.github.io/styleguide/cppguide.html#C++11)
240due to concerns that this is tied to a more template-heavy interface style.
241***
242
Peter Kastinge2c5ee82023-02-15 17:23:08243### &lt;regex&gt; <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13244
245```c++
246#include <regex>
247```
248
Peter Kastinge2c5ee82023-02-15 17:23:08249**Description:** A standard regular expressions library.
Joe Masonfe4f2562021-09-15 15:23:13250
251**Documentation:**
252[Regular expressions library](https://en.cppreference.com/w/cpp/regex)
253
254**Notes:**
255*** promo
256Overlaps with many regular expression libraries in Chromium. When in doubt, use
Peter Kastinge2c5ee82023-02-15 17:23:08257`third_party/re2`.
Joe Masonfe4f2562021-09-15 15:23:13258***
259
Peter Kastinge2c5ee82023-02-15 17:23:08260### std::bind <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13261
262```c++
Peter Kastinge2c5ee82023-02-15 17:23:08263auto x = std::bind(function, args, ...);
Joe Masonfe4f2562021-09-15 15:23:13264```
265
Peter Kastinge2c5ee82023-02-15 17:23:08266**Description:** Declares a function object bound to certain arguments.
Joe Masonfe4f2562021-09-15 15:23:13267
268**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08269[`std::bind`](https://en.cppreference.com/w/cpp/utility/functional/bind)
Joe Masonfe4f2562021-09-15 15:23:13270
271**Notes:**
272*** promo
Peter Kastinge2c5ee82023-02-15 17:23:08273Use `base::Bind` instead. Compared to `std::bind`, `base::Bind` helps prevent
274lifetime issues by preventing binding of capturing lambdas and by forcing
275callers to declare raw pointers as `Unretained`.
Joe Masonfe4f2562021-09-15 15:23:13276
Peter Kastinge2c5ee82023-02-15 17:23:08277[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/SoEj7oIDNuA)
Joe Masonfe4f2562021-09-15 15:23:13278***
279
Peter Kastinge2c5ee82023-02-15 17:23:08280### std::function <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13281
282```c++
Peter Kastinge2c5ee82023-02-15 17:23:08283std::function x = [] { return 10; };
284std::function y = std::bind(foo, args);
Joe Masonfe4f2562021-09-15 15:23:13285```
286
Peter Kastinge2c5ee82023-02-15 17:23:08287**Description:** Wraps a standard polymorphic function.
Joe Masonfe4f2562021-09-15 15:23:13288
289**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08290[`std::function`](https://en.cppreference.com/w/cpp/utility/functional/function)
291
292**Notes:**
293*** promo
Marijn Kruisselbrinke453e56c2023-12-05 22:35:13294Use `base::{Once,Repeating}Callback` or `base::FunctionRef` instead. Compared
295to `std::function`, `base::{Once,Repeating}Callback` directly supports
296Chromium's refcounting classes and weak pointers and deals with additional
297thread safety concerns.
Peter Kastinge2c5ee82023-02-15 17:23:08298
299[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/SoEj7oIDNuA)
300***
301
302### std::shared_ptr <sup>[banned]</sup>
303
304```c++
305std::shared_ptr<int> x = std::make_shared<int>(10);
306```
307
308**Description:** Allows shared ownership of a pointer through reference counts.
309
310**Documentation:**
311[`std::shared_ptr`](https://en.cppreference.com/w/cpp/memory/shared_ptr)
312
313**Notes:**
314*** promo
315Unlike `base::RefCounted`, uses extrinsic rather than intrinsic reference
316counting. Could plausibly be used in Chromium, but would require significant
317migration.
318
319[Google Style Guide](https://google.github.io/styleguide/cppguide.html#Ownership_and_Smart_Pointers),
Peter Kasting72d110f12024-02-06 19:45:26320[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/aT2wsBLKvzI)
Peter Kastinge2c5ee82023-02-15 17:23:08321***
322
323### std::{sto{i,l,ul,ll,ull,f,d,ld},to_string} <sup>[banned]</sup>
324
325```c++
326int x = std::stoi("10");
327```
328
329**Description:** Converts strings to/from numbers.
330
331**Documentation:**
332[`std::stoi`, `std::stol`, `std::stoll`](https://en.cppreference.com/w/cpp/string/basic_string/stol),
333[`std::stoul`, `std::stoull`](https://en.cppreference.com/w/cpp/string/basic_string/stoul),
334[`std::stof`, `std::stod`, `std::stold`](https://en.cppreference.com/w/cpp/string/basic_string/stof),
335[`std::to_string`](https://en.cppreference.com/w/cpp/string/basic_string/to_string)
Joe Masonfe4f2562021-09-15 15:23:13336
337**Notes:**
338*** promo
339The string-to-number conversions rely on exceptions to communicate failure,
340while the number-to-string conversions have performance concerns and depend on
Peter Kastinge2c5ee82023-02-15 17:23:08341the locale. Use `base/strings/string_number_conversions.h` instead.
Joe Masonfe4f2562021-09-15 15:23:13342***
343
Peter Kastinge2c5ee82023-02-15 17:23:08344### std::weak_ptr <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13345
Peter Kastinge2c5ee82023-02-15 17:23:08346```c++
347std::weak_ptr<int> x = my_shared_x;
348```
349
350**Description:** Allows a weak reference to a `std::shared_ptr`.
351
352**Documentation:**
353[`std::weak_ptr`](https://en.cppreference.com/w/cpp/memory/weak_ptr)
354
355**Notes:**
356*** promo
357Banned because `std::shared_ptr` is banned. Use `base::WeakPtr` instead.
Joe Masonfe4f2562021-09-15 15:23:13358***
359
Peter Kastinge2c5ee82023-02-15 17:23:08360### Thread Support Library <sup>[banned]</sup>
361
362```c++
363#include <barrier> // C++20
364#include <condition_variable>
365#include <future>
366#include <latch> // C++20
367#include <mutex>
368#include <semaphore> // C++20
369#include <stop_token> // C++20
370#include <thread>
371```
372
Joe Masonfe4f2562021-09-15 15:23:13373**Description:** Provides a standard multithreading library using `std::thread`
374and associates
375
376**Documentation:**
377[Thread support library](https://en.cppreference.com/w/cpp/thread)
378
379**Notes:**
380*** promo
Peter Kasting72d110f12024-02-06 19:45:26381Overlaps with `base/synchronization`. `base::Thread` is tightly coupled to
382`base::MessageLoop` which would make it hard to replace. We should investigate
383using standard mutexes, or `std::unique_lock`, etc. to replace our
Peter Kastinge2c5ee82023-02-15 17:23:08384locking/synchronization classes.
Joe Masonfe4f2562021-09-15 15:23:13385***
386
Peter Kasting72d110f12024-02-06 19:45:26387## C++17 Banned Language Features {#core-blocklist-17}
Peter Kasting1865f2772021-12-23 21:23:58388
Peter Kasting72d110f12024-02-06 19:45:26389The following C++17 language features are not allowed in the Chromium codebase.
Peter Kasting1865f2772021-12-23 21:23:58390
Peter Kasting72d110f12024-02-06 19:45:26391### UTF-8 character literals <sup>[banned]</sup>
Peter Kasting1865f2772021-12-23 21:23:58392
393```c++
Peter Kasting72d110f12024-02-06 19:45:26394char x = u8'x'; // C++17
395char8_t x = u8'x'; // C++20
Peter Kasting1865f2772021-12-23 21:23:58396```
397
Peter Kasting72d110f12024-02-06 19:45:26398**Description:** A character literal that begins with `u8` is a character
399literal of type `char` (C++17) or `char8_t` (C++20). The value of a UTF-8
400character literal is equal to its ISO 10646 code point value.
Peter Kasting1865f2772021-12-23 21:23:58401
402**Documentation:**
Peter Kasting72d110f12024-02-06 19:45:26403[Character literal](https://en.cppreference.com/w/cpp/language/character_literal)
Peter Kasting1865f2772021-12-23 21:23:58404
405**Notes:**
406*** promo
Peter Kasting72d110f12024-02-06 19:45:26407Banned because `char8_t` is banned. Use an unprefixed character or string
408literal; it should be encoded in the binary as UTF-8 on all supported platforms.
Peter Kasting6d77e9d2023-02-09 21:58:18409***
410
411## C++17 Banned Library Features {#library-blocklist-17}
412
413The following C++17 library features are not allowed in the Chromium codebase.
414
Peter Kasting72d110f12024-02-06 19:45:26415### Mathematical special functions <sup>[banned]</sup>
416
417```c++
418std::assoc_laguerre()
419std::assoc_legendre()
420std::beta()
421std::comp_ellint_1()
422std::comp_ellint_2()
423std::comp_ellint_3()
424std::cyl_bessel_i()
425std::cyl_bessel_j()
426std::cyl_bessel_k()
427std::cyl_neumann()
428std::ellint_1()
429std::ellint_2()
430std::ellint_3()
431std::expint()
432std::hermite()
433std::legendre()
434std::laguerre()
435std::riemann_zeta()
436std::sph_bessel()
437std::sph_legendre()
438std::sph_neumann()
439```
440
441**Description:** A variety of mathematical functions.
442
443**Documentation:**
444[Mathematical special functions](https://en.cppreference.com/w/cpp/numeric/special_functions)
445
446**Notes:**
447*** promo
448Banned due to
449[lack of libc++ support](https://libcxx.llvm.org/Status/Cxx17.html).
450***
451
452### Parallel algorithms <sup>[banned]</sup>
453
454```c++
455auto it = std::find(std::execution::par, std::begin(vec), std::end(vec), 2);
456```
457
458**Description:** Many of the STL algorithms, such as the `copy`, `find` and
459`sort` methods, now support the parallel execution policies: `seq`, `par`, and
460`par_unseq` which translate to "sequentially", "parallel" and
461"parallel unsequenced".
462
463**Documentation:**
464[`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)
465
466**Notes:**
467*** promo
468Banned because
469[libc++ support is incomplete](https://libcxx.llvm.org/Status/PSTL.html) and the
470interaction of its threading implementation with Chrome's is unclear. Prefer to
471explicitly parallelize long-running algorithms using Chrome's threading APIs, so
472the same scheduler controls, shutdown policies, tracing, etc. apply as in any
473other multithreaded code.
474***
475
Peter Kasting6d77e9d2023-02-09 21:58:18476### std::aligned_alloc <sup>[banned]</sup>
477
478```c++
479int* p2 = static_cast<int*>(std::aligned_alloc(1024, 1024));
480```
481
482**Description:** Allocates uninitialized storage with the specified alignment.
483
484**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08485[`std::aligned_alloc`](https://en.cppreference.com/w/cpp/memory/c/aligned_alloc)
Peter Kasting6d77e9d2023-02-09 21:58:18486
487**Notes:**
488*** promo
489[Will be allowed soon](https://crbug.com/1412818); for now, use
490`base::AlignedAlloc`.
491***
492
493### std::any <sup>[banned]</sup>
494
495```c++
496std::any x = 5;
497```
498
499**Description:** A type-safe container for single values of any type.
500
501**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08502[`std::any`](https://en.cppreference.com/w/cpp/utility/any)
Peter Kasting6d77e9d2023-02-09 21:58:18503
504**Notes:**
505*** promo
Peter Kasting72d110f12024-02-06 19:45:26506Banned since workaround for lack of RTTI
507[isn't compatible with the component build](https://crbug.com/1096380). See also
508`absl::any`.
Peter Kasting6d77e9d2023-02-09 21:58:18509
Peter Kasting72d110f12024-02-06 19:45:26510[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/00cpZ07nye4)
511***
512
513### std::byte <sup>[banned]</sup>
514
515```c++
516std::byte b = 0xFF;
517int i = std::to_integer<int>(b); // 0xFF
518```
519
520**Description:** The contents of a single memory unit. `std::byte` has the same
521size and aliasing rules as `unsigned char`, but does not semantically represent
522a character or arithmetic value, and does not expose operators other than
523bitwise ops.
524
525**Documentation:**
526[`std::byte`](https://en.cppreference.com/w/cpp/types/byte)
527
528**Notes:**
529*** promo
530Banned due to low marginal utility in practice, high conversion costs, and
531programmer confusion about "byte" vs. "octet". Use `uint8_t` for the common case
532of "8-bit unsigned value", and `char` for the atypical case of code that works
533with memory without regard to its contents' values or semantics (e.g allocator
534implementations).
535
536[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/bBY0gZa1Otk)
Peter Kasting6d77e9d2023-02-09 21:58:18537***
538
Peter Kasting6d77e9d2023-02-09 21:58:18539### std::filesystem <sup>[banned]</sup>
540
541```c++
542#include <filesystem>
543```
544
545**Description:** A standard way to manipulate files, directories, and paths in a
546filesystem.
547
548**Documentation:**
549[Filesystem library](https://en.cppreference.com/w/cpp/filesystem)
550
551**Notes:**
552*** promo
553Banned by the [Google Style Guide](https://google.github.io/styleguide/cppguide.html#Other_Features).
554***
555
Peter Kasting72d110f12024-02-06 19:45:26556### std::{from,to}_chars <sup>[banned]</sup>
557
558```c++
559std::from_chars(str.data(), str.data() + str.size(), result);
560std::to_chars(str.data(), str.data() + str.size(), 42);
561```
562
563**Description:** Locale-independent, non-allocating, non-throwing functions to
564convert values from/to character strings, designed for use in high-throughput
565contexts.
566
567**Documentation:**
568[`std::from_chars`](https://en.cppreference.com/w/cpp/utility/from_chars)
569[`std::to_chars`](https://en.cppreference.com/w/cpp/utility/to_chars),
570
571**Notes:**
572*** promo
573Overlaps with utilities in `base/strings/string_number_conversions.h`, which are
574easier to use correctly.
575***
576
Peter Kastinge2c5ee82023-02-15 17:23:08577### std::hardware_{con,de}structive_interference_size <sup>[banned]</sup>
Anton Bikineevc6a022582022-10-10 19:08:58578
579```c++
580struct SharedData {
581 ReadOnlyFrequentlyUsed data;
582 alignas(std::hardware_destructive_interference_size) std::atomic<size_t> counter;
583};
584```
585
586**Description:** The `std::hardware_destructive_interference_size` constant is
587useful to avoid false sharing (destructive interference) between variables that
588would otherwise occupy the same cacheline. In contrast,
589`std::hardware_constructive_interference_size` is helpful to promote true
590sharing (constructive interference), e.g. to support better locality for
591non-contended data.
592
593**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08594[`std::hardware_destructive_interference_size`](https://en.cppreference.com/w/cpp/thread/hardware_destructive_interference_size),
595[`std::hardware_constructive_interference_size`](https://en.cppreference.com/w/cpp/thread/hardware_destructive_interference_size)
Anton Bikineevc6a022582022-10-10 19:08:58596
597**Notes:**
598*** promo
Peter Kasting6d77e9d2023-02-09 21:58:18599Banned for now since these are
600[not supported yet](https://github.com/llvm/llvm-project/issues/60174). Allow
601once supported.
Peter Kasting72d110f12024-02-06 19:45:26602
Peter Kasting6d77e9d2023-02-09 21:58:18603[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/cwktrFxxUY4)
Avi Drissman37b7d512022-04-01 22:01:40604***
605
David Benjamind9d21aa12023-10-02 23:29:57606### std::in_place{_type,_index}[_t] <sup>[banned]</sup>
Avi Drissmanbc6545f42022-05-03 17:47:38607
608```c++
Avi Drissmanbc6545f42022-05-03 17:47:38609std::variant<int, float> v{std::in_place_type<int>, 1.4};
610```
611
David Benjamind9d21aa12023-10-02 23:29:57612**Description:** `std::in_place_type` and `std::in_place_index` are
613disambiguation tags for `std::variant` and `std::any` to indicate that the
614object should be constructed in-place.
Avi Drissmanbc6545f42022-05-03 17:47:38615
616**Documentation:**
David Benjamind9d21aa12023-10-02 23:29:57617[`std::in_place_type`](https://en.cppreference.com/w/cpp/utility/in_place)
Avi Drissmanbc6545f42022-05-03 17:47:38618
619**Notes:**
620*** promo
David Benjamind9d21aa12023-10-02 23:29:57621Banned for now because `std::variant` and `std::any` are banned. Because
622`absl::variant` is used instead, and it requires `absl::in_place_type`, use
623`absl::in_place_type` for non-Abseil Chromium
Peter Kasting72d110f12024-02-06 19:45:26624code.
625
626[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ZspmuJPpv6s)
627***
628
629### std::{pmr::memory_resource,polymorphic_allocator} <sup>[banned]</sup>
630
631```c++
632#include <memory_resource>
633```
634
635**Description:** Manages memory allocations using runtime polymorphism.
636
637**Documentation:**
638[`std::pmr::memory_resource`](https://en.cppreference.com/w/cpp/memory/memory_resource),
639[`std::pmr::polymorphic_allocator`](https://en.cppreference.com/w/cpp/memory/polymorphic_allocator)
640
641**Notes:**
642*** promo
643Banned because Chromium does not customize allocators
644([PartitionAlloc](https://chromium.googlesource.com/chromium/src/+/main/base/allocator/partition_allocator/PartitionAlloc.md)
645is used globally).
646***
647
648### std::timespec_get <sup>[banned]</sup>
649
650```c++
651std::timespec ts;
652std::timespec_get(&ts, TIME_UTC);
653```
654
655**Description:** Gets the current calendar time in the given time base.
656
657**Documentation:**
658[`std::timespec_get`](https://en.cppreference.com/w/cpp/chrono/c/timespec_get)
659
660**Notes:**
661*** promo
662Banned due to unclear, implementation-defined behavior. On POSIX, use
663`base::TimeDelta::ToTimeSpec()`; this could be supported on other platforms if
664desirable.
Avi Drissmanbc6545f42022-05-03 17:47:38665***
666
Peter Kasting6d77e9d2023-02-09 21:58:18667### std::uncaught_exceptions <sup>[banned]</sup>
668
669```c++
670int count = std::uncaught_exceptions();
671```
672
673**Description:** Determines whether there are live exception objects.
674
675**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08676[`std::uncaught_exceptions`](https://en.cppreference.com/w/cpp/error/uncaught_exception)
Peter Kasting6d77e9d2023-02-09 21:58:18677
678**Notes:**
679*** promo
680Banned because exceptions are banned.
681***
682
683### std::variant <sup>[banned]</sup>
684
685```c++
686std::variant<int, double> v = 12;
687```
688
689**Description:** The class template `std::variant` represents a type-safe
690`union`. An instance of `std::variant` at any given time holds a value of one of
691its alternative types (it's also possible for it to be valueless).
692
693**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08694[`std::variant`](https://en.cppreference.com/w/cpp/utility/variant)
Peter Kasting6d77e9d2023-02-09 21:58:18695
696**Notes:**
697*** promo
Peter Kastinge2c5ee82023-02-15 17:23:08698[Will be allowed soon](https://crbug.com/1373620); for now, use `absl::variant`.
Peter Kasting6d77e9d2023-02-09 21:58:18699***
700
701### Transparent std::owner_less <sup>[banned]</sup>
702
703```c++
704std::map<std::weak_ptr<T>, U, std::owner_less<>>
705```
706
707**Description:** Function object providing mixed-type owner-based ordering of
708shared and weak pointers, regardless of the type of the pointee.
709
710**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08711[`std::owner_less`](https://en.cppreference.com/w/cpp/memory/owner_less)
Peter Kasting6d77e9d2023-02-09 21:58:18712
713**Notes:**
714*** promo
715Banned since `std::shared_ptr` and `std::weak_ptr` are banned.
716***
717
718### weak_from_this <sup>[banned]</sup>
719
720```c++
721auto weak_ptr = weak_from_this();
722```
723
724**Description:** Returns a `std::weak_ptr<T>` that tracks ownership of `*this`
725by all existing `std::shared_ptr`s that refer to `*this`.
726
727**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08728[`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:18729
730**Notes:**
731*** promo
732Banned since `std::shared_ptr` and `std::weak_ptr` are banned.
Avi Drissman37b7d512022-04-01 22:01:40733***
734
Arthur Sonzognid8517c92023-03-03 09:41:45735## C++20 Allowed Language Features {#core-allowlist-20}
736
Peter Kastingf86817c2023-11-13 18:00:11737The following C++20 language features are allowed in the Chromium codebase.
738
739### Abbreviated function templates <sup>[allowed]</sup>
740
741```c++
742// template <typename T>
743// void f1(T x);
744void f1(auto x);
745
746// template <C T> // `C` is a concept
747// void f2(T x);
748void f2(C auto x);
749
750// template <typename T, C U> // `C` is a concept
751// void f3(T x, U y);
752template <typename T>
753void f3(T x, C auto y);
754
755// template<typename... Ts>
756// void f4(Ts... xs);
757void f4(auto... xs);
758```
759
760**Description:** Function params of type `auto` become syntactic sugar for
761declaring a template type for each such parameter.
762
763**Documentation:**
764[Abbreviated function template](https://en.cppreference.com/w/cpp/language/function_template#Abbreviated_function_template)
765
766**Notes:**
767*** promo
768[Migration bug](https://crbug.com/1414526)
769***
770
771### consteval <sup>[allowed]</sup>
772
773```c++
774consteval int sqr(int n) { return n * n; }
775constexpr int kHundred = sqr(10); // OK
776constexpr int quad(int n) { return sqr(sqr(n)); } // ERROR, might be runtime
777```
778
779**Description:** Specified that a function may only be used in a compile-time
780context.
781
782**Documentation:**
783[`consteval` specifier](https://en.cppreference.com/w/cpp/language/consteval)
784
785**Notes:**
786*** promo
787None
788***
789
790### Constraints and concepts <sup>[allowed]</sup>
791
792```c++
793// `Hashable` is a concept satisfied by any type `T` for which the expression
794// `std::hash<T>{}(a)` compiles and produces a value convertible to `size_t`.
795template<typename T>
796concept Hashable = requires(T a)
797{
798 { std::hash<T>{}(a) } -> std::convertible_to<size_t>;
799};
800template <Hashable T> // Only instantiable for `T`s that satisfy `Hashable`.
801void f(T) { ... }
802```
803
804**Description:** Allows bundling sets of requirements together as named
805concepts, then enforcing them on template arguments.
806
807**Documentation:**
808[Constraints and concepts](https://en.cppreference.com/w/cpp/language/constraints)
809
810**Notes:**
811*** promo
812[Migration bug](https://crbug.com/1414528)
813***
814
815### Default comparisons <sup>[allowed]</sup>
816
817```c++
Roland Bock5dbdff62024-01-20 09:33:53818class S : public T {
819 // Non-member equality operator with access to private members.
danakj4b9193e2024-02-02 17:31:33820 // Compares `T` bases, then `x`, then `y`, short-circuiting when
821 // it finds inequality.
Roland Bock5dbdff62024-01-20 09:33:53822 friend bool operator==(const S&, const S&) = default;
823
danakj4b9193e2024-02-02 17:31:33824 // Non-member ordering operator with access to private members.
825 // Compares `T` bases, then `x`, then `y`, short-circuiting when
826 // it finds an ordering difference.
827 friend auto operator<=>(const S&, const S&) = default;
828
Peter Kastingf86817c2023-11-13 18:00:11829 int x;
830 bool y;
831};
832```
833
Roland Bock5dbdff62024-01-20 09:33:53834**Description:** Requests that the compiler generate the implementation of
835any comparison operator, including `<=>`. Prefer non-member comparison
836operators. When defaulting `<=>`, also explicitly default `==`. Together these
837are sufficient to allow any comparison as long as callers do not need to take
838the address of any non-declared operator.
Peter Kastingf86817c2023-11-13 18:00:11839
840**Documentation:**
841[Default comparisons](https://en.cppreference.com/w/cpp/language/default_comparisons)
842
843**Notes:**
844*** promo
danakj4b9193e2024-02-02 17:31:33845Unlike constructors/destructors, our compiler extensions do not require these
846to be written out-of-line in the .cc file. Feel free to write `= default`
847directly in the header, as this is much simpler to write.
848
849- [Migration bug](https://crbug.com/1414530)
David Bokanc246fc92024-02-28 17:55:47850- [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/h4lVi2jHU-0/m/X0q_Bh2IAAAJ)
danakj4b9193e2024-02-02 17:31:33851
Peter Kastingf86817c2023-11-13 18:00:11852***
853
Arthur Sonzognid8517c92023-03-03 09:41:45854### Designated initializers <sup>[allowed]</sup>
855
856```c++
857struct S { int x = 1; int y = 2; }
858S s{ .y = 3 }; // OK, s.x == 1, s.y == 3
859```
860
861**Description:** Allows explicit initialization of subsets of aggregate members
862at construction.
863
864**Documentation:**
865[Designated initializers](https://en.cppreference.com/w/cpp/language/aggregate_initialization#Designated_initializers)
866
867**Notes:**
868*** promo
869None
870***
871
Peter Kastingf86817c2023-11-13 18:00:11872### __has_cpp_attribute <sup>[allowed]</sup>
873
874```c++
875#if __has_cpp_attribute(assume) // Toolchain supports C++23 `[[assume]]`.
876...
877#endif
878```
879
880**Description:** Checks whether the toolchain supports a particular standard
881attribute.
882
883**Documentation:**
884[Feature testing](https://en.cppreference.com/w/cpp/feature_test)
885
886**Notes:**
887*** promo
888None
889***
890
891### constinit <sup>[allowed]</sup>
892
893```c++
894constinit int x = 3;
895void foo() {
896 ++x;
897}
898```
899
900**Description:** Ensures that a variable can be compile-time initialized. This
901is like a milder form of `constexpr` that does not force variables to be const
902or have constant destruction.
903
904**Documentation:**
905[`constinit` specifier](https://en.cppreference.com/w/cpp/language/constinit)
906
907**Notes:**
908*** promo
909[Migration bug](https://crbug.com/1414612)
910***
911
912### Initializers for bit-field members <sup>[allowed]</sup>
913
914```c++
915struct S {
916 uint32_t x : 27 = 2;
917};
918```
919
920**Description:** Allows specifying the default initial value of a bit-field
921member, as can already be done for other member types.
922
923**Documentation:**
924[Bit-field](https://en.cppreference.com/w/cpp/language/bit_field)
925
926**Notes:**
927*** promo
928None
929***
930
931### Lambda captures with initializers that are pack expansions <sup>[allowed]</sup>
932
933```c++
934template <typename... Args>
935void foo(Args... args) {
936 const auto l = [...n = args] { (x(n), ...); };
937}
938```
939
940**Description:** Allows initializing a capture with a pack expansion.
941
942**Documentation:**
943[Lambda capture](https://en.cppreference.com/w/cpp/language/lambda#Lambda_capture)
944
945**Notes:**
946*** promo
947None
948***
949
950### Language feature-test macros <sup>[allowed]</sup>
951
952```c++
953#if !defined(__cpp_modules) || (__cpp_modules < 201907L)
954... // Toolchain does not support modules
955#endif
956```
957
958**Description:** Provides a standardized way to test the toolchain's
959implementation of a particular language feature.
960
961**Documentation:**
962[Feature testing](https://en.cppreference.com/w/cpp/feature_test)
963
964**Notes:**
965*** promo
966None
967***
968
Peter Kastingf86817c2023-11-13 18:00:11969### Range-for statements with initializer <sup>[allowed]</sup>
970
971```c++
972T foo();
973...
974for (auto& x : foo().items()) { ... } // UAF before C++23!
975for (T thing = foo(); auto& x : thing.items()) { ... } // OK
976```
977
978**Description:** Like C++17's selection statements with initializer.
979Particularly useful before C++23, since temporaries inside range-expressions are
980not lifetime-extended until the end of the loop before C++23.
981
982**Documentation:**
983[Range-based `for` loop](https://en.cppreference.com/w/cpp/language/range-for)
984
985**Notes:**
986*** promo
987[Migration bug](https://crbug.com/1414531)
988***
989
990### Three-way comparison ("spaceship") operator <sup>[allowed]</sup>
991
992```c++
993// `ordering` is an instance of `std::strong_odering` or `std::partial_ordering`
994// that describes how `a` and `b` are related.
995const auto ordering = a <=> b;
996if (ordering < 0) { ... } // `a` < `b`
997else if (ordering > 0) { ... } // `a` > `b`
998else { ... } // `a` == `b`
999```
1000
1001**Description:** Compares two objects in a fashion similar to `strcmp`. Perhaps
1002most useful when defined as an overload in a class, in which case it can replace
1003definitions of other inequalities. See also "Default comparisons".
1004
1005**Documentation:**
1006[Three-way comparison](https://en.cppreference.com/w/cpp/language/operator_comparison#Three-way_comparison)
1007
1008**Notes:**
1009*** promo
1010[Migration bug](https://crbug.com/1414530)
1011***
1012
1013## C++20 Allowed Library Features {#library-allowlist-20}
1014
1015The following C++20 library features are allowed in the Chromium codebase.
1016
1017### &lt;bit&gt; <sup>[allowed]</sup>
1018
1019```c++
1020#include <bit>
1021```
1022
1023**Description:** Provides various byte- and bit-twiddling functions, e.g.
1024counting leading zeros.
1025
1026**Documentation:**
1027[Standard library header `<bit>`](https://en.cppreference.com/w/cpp/header/bit)
1028
1029**Notes:**
1030*** promo
1031[Migration bug](https://crbug.com/1414634)
1032***
1033
1034### &lt;compare&gt; <sup>[allowed]</sup>
1035
1036```c++
1037#include <compare>
1038```
1039
1040**Description:** Concepts and classes used to implement three-way comparison
1041("spaceship", `<=>`) support.
1042
1043**Documentation:**
1044[Standard library header `<compare>`](https://en.cppreference.com/w/cpp/header/compare)
1045
1046**Notes:**
1047*** promo
1048None
1049***
1050
1051### &lt;concepts&gt; <sup>[allowed]</sup>
1052
1053```c++
1054#include <concepts>
1055```
1056
1057**Description:** Various useful concepts, many of which replace pre-concept
1058machinery in `<type_traits>`.
1059
1060**Documentation:**
1061[Standard library header `<concepts>`](https://en.cppreference.com/w/cpp/header/concepts)
1062
1063**Notes:**
1064*** promo
1065None
1066***
1067
Daniel Cheng89719222024-07-04 04:59:291068### Range algorithms <sup>[allowed]</sup>
1069
1070```c++
1071constexpr int kArr[] = {2, 4, 6, 8, 10, 12};
1072constexpr auto is_even = [] (auto x) { return x % 2 == 0; };
1073static_assert(std::ranges::all_of(kArr, is_even));
1074```
1075
1076**Description:** Provides versions of most algorithms that accept either an
1077iterator-sentinel pair or a single range argument.
1078
1079**Documentation:**
1080[Ranges algorithms](https://en.cppreference.com/w/cpp/algorithm/ranges)
1081
1082**Notes:**
1083*** promo
1084Supersedes `//base`'s backports in `//base/ranges/algorithm.h`.
1085
1086[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ZnIbkfJ0Glw)
1087***
1088
1089### Range access, range primitives, dangling iterator handling, and range concepts <sup>[allowed]</sup>
1090
1091```c++
1092// Range access:
1093constexpr int kArr[] = {2, 4, 6, 8, 10, 12};
1094static_assert(std::ranges::size(kArr) == 6);
1095
1096// Range primitives:
1097static_assert(
1098 std::same_as<std::ranges::iterator_t<decltype(kArr)>, const int*>);
1099
1100// Range concepts:
1101static_assert(std::ranges::contiguous_range<decltype(kArr)>);
1102```
1103
1104**Description:** Various helper functions and types for working with ranges.
1105
1106**Documentation:**
1107[Ranges library](https://en.cppreference.com/w/cpp/ranges)
1108
1109**Notes:**
1110*** promo
1111Supersedes `//base`'s backports in `//base//ranges/ranges.h`.
1112
1113[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ZnIbkfJ0Glw)
1114***
1115
Peter Kastingf86817c2023-11-13 18:00:111116### Library feature-test macros and &lt;version&gt; <sup>[allowed]</sup>
1117
1118```c++
1119#if !defined(__cpp_lib_atomic_value_initialization) || \
1120 (__cpp_lib_atomic_value_initialization < 201911L)
1121... // `std::atomic` is not value-initialized by default.
1122#endif
1123```
1124
1125**Description:** Provides a standardized way to test the toolchain's
1126implementation of a particular library feature.
1127
1128**Documentation:**
1129[Feature testing](https://en.cppreference.com/w/cpp/feature_test)
1130
1131**Notes:**
1132*** promo
1133None
1134***
1135
1136### &lt;numbers&gt; <sup>[allowed]</sup>
1137
1138```c++
1139#include <numbers>
1140```
1141
1142**Description:** Provides compile-time constants for many common mathematical
1143values, e.g. pi and e.
1144
1145**Documentation:**
1146[Mathematical constants](https://en.cppreference.com/w/cpp/numeric/constants)
1147
1148**Notes:**
1149*** promo
1150[Migration bug](https://crbug.com/1414635)
1151***
1152
1153### std::assume_aligned <sup>[allowed]</sup>
1154
1155```c++
1156void f(int* p) {
1157 int* aligned = std::assume_aligned<256>(p);
1158 ...
1159```
1160
1161**Description:** Informs the compiler that a pointer points to an address
1162aligned to at least some particular power of 2.
1163
1164**Documentation:**
1165[`std::assume_aligned`](https://en.cppreference.com/w/cpp/memory/assume_aligned)
1166
1167**Notes:**
1168*** promo
Peter Kastingb2887c32024-01-08 21:41:041169None
Peter Kastingf86817c2023-11-13 18:00:111170***
1171
1172### std::erase[_if] for containers <sup>[allowed]</sup>
1173
1174```c++
1175std::vector<int> numbers = ...;
1176std::erase_if(numbers, [](int x) { return x % 2 == 0; });
1177```
1178
1179**Description:** Erases from a container by value comparison or predicate,
1180avoiding the need to use the `erase(remove(...` paradigm.
1181
1182**Documentation:**
1183[`std::erase`, `std::erase_if` (`std::vector`)](https://en.cppreference.com/w/cpp/container/vector/erase2)
1184
1185**Notes:**
1186*** promo
1187[Migration bug](https://crbug.com/1414639)
1188***
1189
1190### std::is_[un]bounded_array <sup>[allowed]</sup>
1191
1192```c++
1193template <typename T>
1194static constexpr bool kBoundedArray = std::is_bounded_array_v<T>;
1195```
1196
1197**Description:** Checks if a type is an array type with a known or unknown
1198bound.
1199
1200**Documentation:**
1201[`std::is_bounded_array`](https://en.cppreference.com/w/cpp/types/is_bounded_array),
1202[`std::is_unbounded_array`](https://en.cppreference.com/w/cpp/types/is_unbounded_array)
1203
1204**Notes:**
1205*** promo
1206None
1207***
1208
1209### std::lerp <sup>[allowed]</sup>
1210
1211```c++
1212double val = std::lerp(start, end, t);
1213```
1214
1215**Description:** Linearly interpolates (or extrapolates) between two values.
1216
1217**Documentation:**
1218[`std::lerp`](https://en.cppreference.com/w/cpp/numeric/lerp)
1219
1220**Notes:**
1221*** promo
1222[Migration bug](https://crbug.com/1414537)
1223***
1224
1225### std::make_obj_using_allocator etc. <sup>[allowed]</sup>
1226
1227```c++
1228auto obj = std::make_obj_using_allocator<Obj>(alloc, ...);
1229```
1230
1231**Description:** Constructs an object using
1232[uses-allocator construction](https://en.cppreference.com/w/cpp/memory/uses_allocator).
1233
1234**Documentation:**
1235[`std::make_obj_using_allocator`](https://en.cppreference.com/w/cpp/memory/make_obj_using_allocator)
1236
1237**Notes:**
1238*** promo
1239None
1240***
1241
1242### std::make_unique_for_overwrite <sup>[allowed]</sup>
1243
1244```c++
1245auto ptr = std::make_unique_for_overwrite<int>(); // `*ptr` is uninitialized
1246```
1247
1248**Description:** Like calling `std::unique_ptr<T>(new T)` instead of the more
1249typical `std::unique_ptr<T>(new T(...))`.
1250
1251**Documentation:**
1252[`std::make_unique`, `std::make_unique_for_overwrite`](https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique)
1253
1254**Notes:**
1255*** promo
1256None
1257***
1258
1259### std::midpoint <sup>[allowed]</sup>
1260
1261```c++
1262int center = std::midpoint(top, bottom);
1263```
1264
1265**Description:** Finds the midpoint between its two arguments, avoiding any
1266possible overflow. For integral inputs, rounds towards the first argument.
1267
1268**Documentation:**
1269[`std::midpoint`](https://en.cppreference.com/w/cpp/numeric/midpoint)
1270
1271**Notes:**
1272*** promo
1273[Migration bug](https://crbug.com/1414539)
1274***
1275
1276### std::remove_cvref[_t] <sup>[allowed]</sup>
1277
1278```c++
1279template <typename T,
1280 typename = std::enable_if_t<std::is_same_v<std::remove_cvref_t<T>,
1281 int>>>
1282void foo(T t);
1283```
1284
1285**Description:** Provides a way to remove const, volatile, and reference
1286qualifiers from a type.
1287
1288**Documentation:**
1289[`std::remove_cvref`](https://en.cppreference.com/w/cpp/types/remove_cvref)
1290
1291**Notes:**
1292*** promo
Peter Kastingb2887c32024-01-08 21:41:041293None
Peter Kastingf86817c2023-11-13 18:00:111294***
1295
1296### std::ssize <sup>[allowed]</sup>
1297
1298```c++
1299str.replace(it, it + std::ssize(substr), 1, 'x');
1300```
1301
1302**Description:** Returns the size of an object as a signed type.
1303
1304**Documentation:**
1305[`std::size`, `std::ssize`](https://en.cppreference.com/w/cpp/iterator/size)
1306
1307**Notes:**
1308*** promo
1309[Migration bug](https://crbug.com/1414543)
1310***
1311
1312### std::string::(starts,ends)_with <sup>[allowed]</sup>
1313
1314```c++
1315const std::string str = "Foo bar";
1316const bool is_true = str.ends_with("bar");
1317```
1318
1319**Description:** Tests whether a string starts or ends with a particular
1320character or string.
1321
1322**Documentation:**
1323[`std::basic_string<CharT,Traits,Allocator>::starts_with`](https://en.cppreference.com/w/cpp/string/basic_string/starts_with),
1324[`std::basic_string<CharT,Traits,Allocator>::ends_with`](https://en.cppreference.com/w/cpp/string/basic_string/ends_with)
1325
1326**Notes:**
1327*** promo
1328[Migration bug](https://crbug.com/1414647)
1329***
1330
Peter Kastingf86817c2023-11-13 18:00:111331## C++20 Banned Language Features {#core-blocklist-20}
1332
1333The following C++20 language features are not allowed in the Chromium codebase.
1334
Peter Kasting72d110f12024-02-06 19:45:261335### char8_t <sup>[banned]</sup>
1336
1337```c++
1338char8_t c = u8'x';
1339```
1340
1341**Description:** A single UTF-8 code unit. Similar to `unsigned char`, but
1342considered a distinct type.
1343
1344**Documentation:**
1345[Fundamental types](https://en.cppreference.com/w/cpp/language/types#char8_t)
1346
1347**Notes:**
1348*** promo
1349Use `char` and unprefixed character literals. Non-UTF-8 encodings are rare
1350enough in Chromium that the value of distinguishing them at the type level is
1351low, and `char8_t*` is not interconvertible with `char*` (what ~all Chromium,
1352STL, and platform-specific APIs use), so using `u8` prefixes would obligate us
1353to insert casts everywhere. If you want to declare at a type level that a block
1354of data is string-like and not an arbitrary binary blob, prefer
1355`std::string[_view]` over `char*`.
1356***
1357
Peter Kastingf86817c2023-11-13 18:00:111358### Modules <sup>[banned]</sup>
1359
1360```c++
1361export module helloworld; // module declaration
1362
1363import <iostream>; // import declaration
1364
1365export void hello() { // export declaration
1366 std::cout << "Hello world!\n";
1367}
1368```
1369
1370**Description:** Modules provide an alternative to many uses of headers which
1371allows for faster compilation, better tooling support, and reduction of problems
1372like "include what you use".
1373
1374**Documentation:**
1375[Modules](https://en.cppreference.com/w/cpp/language/modules)
1376
1377**Notes:**
1378*** promo
1379Not yet sufficiently supported in Clang and GN. Re-evaluate when support
1380improves.
1381***
1382
Peter Kasting8bc046d22023-11-14 00:38:031383### [[no_unique_address]] <sup>[banned]</sup>
1384
1385```c++
1386struct Empty {};
1387struct X {
1388 int i;
1389 [[no_unique_address]] Empty e;
1390};
1391```
1392
1393**Description:** Allows a data member to be overlapped with other members.
1394
1395**Documentation:**
1396[C++ attribute: `no_unique_address`](https://en.cppreference.com/w/cpp/language/attributes/no_unique_address)
1397
1398**Notes:**
1399*** promo
1400Has no effect on Windows, for compatibility with Microsoft's ABI. Use
1401`NO_UNIQUE_ADDRESS` from `base/compiler_specific.h` instead. Do not use (either
1402form) on members of unions due to
1403[potential memory safety problems](https://github.com/llvm/llvm-project/issues/60711).
Peter Kasting8bc046d22023-11-14 00:38:031404***
1405
Peter Kastingf86817c2023-11-13 18:00:111406## C++20 Banned Library Features {#library-blocklist-20}
1407
1408The following C++20 library features are not allowed in the Chromium codebase.
1409
1410### std::atomic_ref <sup>[banned]</sup>
1411
1412```c++
1413struct S { int a; int b; };
1414S not_atomic;
1415std::atomic_ref<S> is_atomic(not_atomic);
1416```
1417
1418**Description:** Allows atomic access to objects that might not themselves be
1419atomic types. While any atomic_ref to an object exists, the object must be
1420accessed exclusively through atomic_ref instances.
1421
1422**Documentation:**
1423[`std::atomic_ref`](https://en.cppreference.com/w/cpp/atomic/atomic_ref)
1424
1425**Notes:**
1426*** promo
1427Banned due to being [unimplemented in libc++](https://reviews.llvm.org/D72240).
1428
1429[Migration bug](https://crbug.com/1422701) (once this is allowed)
1430***
1431
1432### std::bind_front <sup>[banned]</sup>
1433
1434```c++
1435int minus(int a, int b);
1436auto fifty_minus_x = std::bind_front(minus, 50);
1437int forty = fifty_minus_x(10);
1438```
1439
1440**Description:** An updated version of `std::bind` with fewer gotchas, similar
1441to `absl::bind_front`.
1442
1443**Documentation:**
1444[`std::bind_front`, `std::bind_back`](https://en.cppreference.com/w/cpp/utility/functional/bind_front)
1445
1446**Notes:**
1447*** promo
1448Overlaps with `base::Bind`.
1449***
1450
Avi Drissman70cb7f72023-12-12 17:44:371451### std::bit_cast <sup>[banned]</sup>
1452
1453```c++
1454float quake_rsqrt(float number) {
1455 long i = std::bit_cast<long>(number);
1456 i = 0x5f3759df - (i >> 1); // wtf?
1457 float y = std::bit_cast<float>(i);
1458 return y * (1.5f - (0.5f * number * y * y));
1459}
1460```
1461
1462**Description:** Returns an value constructed with the same bits as an value of
1463a different type.
1464
1465**Documentation:**
1466[`std::bit_cast`](https://en.cppreference.com/w/cpp/numeric/bit_cast)
1467
1468**Notes:**
1469*** promo
1470The `std::` version of `bit_cast` allows casting of pointer and reference types,
1471which is both useless in that it doesn't avoid UB, and dangerous in that it
1472allows arbitrary casting away of modifiers like `const`. Instead of using
1473`bit_cast` on pointers, use standard C++ casts. For use on values, use
1474`base::bit_cast` which does not allow this unwanted usage.
1475***
1476
Peter Kastingf86817c2023-11-13 18:00:111477### std::{c8rtomb,mbrtoc8} <sup>[banned]</sup>
1478
1479```c++
1480std::u8string_view strv = u8"zß水🍌";
1481std::mbstate_t state;
1482char out[MB_LEN_MAX] = {0};
1483for (char8_t c : strv) {
1484 size_t rc = std::c8rtomb(out, c, &state);
1485 ...
1486```
1487
1488**Description:** Converts a code point between UTF-8 and a multibyte character
1489encoded using the current C locale.
1490
1491**Documentation:**
1492[`std::c8rtomb`](https://en.cppreference.com/w/cpp/string/multibyte/c8rtomb),
1493[`std::mbrtoc8`](https://en.cppreference.com/w/cpp/string/multibyte/mbrtoc8)
1494
1495**Notes:**
1496*** promo
1497Chromium functionality should not vary with the C locale.
1498***
1499
Daniel Cheng89719222024-07-04 04:59:291500### Views, range factories, and range adaptors <sup>[banned]</sup>
1501
1502```c++
1503constexpr int kArr[] = {6, 2, 8, 4, 4, 2};
1504constexpr auto plus_one = std::views::transform([](int n){ return n + 1; });
1505static_assert(std::ranges::equal(kArr | plus_one, {7, 3, 9, 5, 5, 3}));
1506
1507// Prints 1, 2, 3, 4, 5, 6.
1508for (auto i : std::ranges::iota_view(1, 7)) {
1509 std::cout << i << '\n';
1510}
1511```
1512
1513**Description:** Lightweight objects that represent iterable sequences.
1514Provides facilities for lazy operations on ranges, along with composition into
1515pipelines.
1516
1517**Documentation:**
1518[Ranges library](https://en.cppreference.com/w/cpp/ranges)
1519
1520**Notes:**
1521*** promo
1522Banned in Chrome due to questions about the design, impact on build time, and
1523runtime performance.
1524
1525[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ZnIbkfJ0Glw)
1526***
1527
Nick Diego Yamanee522ae82024-02-27 04:23:221528### std::to_address <sup>[banned]</sup>
1529
1530```c++
1531std::vector<int> numbers;
1532int* i = std::to_address(numbers.begin());
1533```
1534
1535**Description:** Converts a pointer-like object to a pointer, even if the
1536pointer does not refer to a constructed object (in which case an expression like
1537`&*p` is UB).
1538
1539**Documentation:**
1540[`std::to_address`](https://en.cppreference.com/w/cpp/memory/to_address)
1541
1542**Notes:**
1543*** promo
1544Banned because it is not guaranteed to be SFINAE-compatible. Use
1545base::to_address, which does guarantee this.
1546***
1547
Peter Kastingf86817c2023-11-13 18:00:111548### &lt;syncstream&gt; <sup>[banned]</sup>
1549
1550```c++
1551#include <syncstream>
1552```
1553
1554**Description:** Facilities for multithreaded access to streams.
1555
1556**Documentation:**
1557[Standard library header `<syncstream>`](https://en.cppreference.com/w/cpp/header/syncstream)
1558
1559**Notes:**
1560*** promo
1561Banned due to being unimplemented per
1562[the libc++ C++20 status page](https://libcxx.llvm.org/Status/Cxx20.html).
1563Reevaluate usefulness once implemented.
1564***
1565
1566## C++20 TBD Language Features {#core-review-20}
1567
1568The following C++20 language features are not allowed in the Chromium codebase.
1569See the top of this page on how to propose moving a feature from this list into
1570the allowed or banned sections.
1571
1572### Aggregate initialization using parentheses <sup>[tbd]</sup>
1573
1574```c++
1575struct B {
1576 int a;
1577 int&& r;
1578} b2(1, 1); // Warning: dangling reference
1579```
1580
1581**Description:** Allows initialization of aggregates using parentheses, not just
1582braces.
1583
1584**Documentation:**
1585[Aggregate initialization](https://en.cppreference.com/w/cpp/language/aggregate_initialization),
1586[Direct initialization](https://en.cppreference.com/w/cpp/language/direct_initialization)
1587
1588**Notes:**
1589*** promo
1590There are subtle but important differences between brace- and paren-init of
1591aggregates. The parenthesis style appears to have more pitfalls (allowing
1592narrowing conversions, not extending lifetimes of temporaries bound to
1593references).
1594***
1595
Peter Kastingf86817c2023-11-13 18:00:111596### Coroutines <sup>[tbd]</sup>
1597
1598```c++
1599co_return 1;
1600```
1601
1602**Description:** Allows writing functions that logically block while physically
1603returning control to a caller. This enables writing some kinds of async code in
1604simple, straight-line ways without storing state in members or binding
1605callbacks.
1606
1607**Documentation:**
1608[Coroutines](https://en.cppreference.com/w/cpp/language/coroutines)
1609
1610**Notes:**
1611*** promo
1612Requires significant support code and planning around API and migration.
1613
1614[Prototyping bug](https://crbug.com/1403840)
1615***
1616
1617### [[likely]], [[unlikely]] <sup>[tbd]</sup>
1618
1619```c++
1620if (n > 0) [[likely]] {
1621 return 1;
1622}
1623```
1624
1625**Description:** Tells the optimizer that a particular codepath is more or less
1626likely than an alternative.
1627
1628**Documentation:**
1629[C++ attribute: `likely`, `unlikely`](https://en.cppreference.com/w/cpp/language/attributes/likely)
1630
1631**Notes:**
1632*** promo
1633[Will be allowed soon](https://crbug.com/1414620); for now, use `[UN]LIKELY`.
1634***
1635
1636## C++20 TBD Library Features {#library-review-20}
1637
1638The following C++20 library features are not allowed in the Chromium codebase.
1639See the top of this page on how to propose moving a feature from this list into
1640the allowed or banned sections.
1641
1642### &lt;coroutine&gt; <sup>[tbd]</sup>
1643
1644```c++
1645#include <coroutine>
1646```
1647
1648**Description:** Header which defines various core coroutine types.
1649
1650**Documentation:**
1651[Coroutine support](https://en.cppreference.com/w/cpp/coroutine)
1652
1653**Notes:**
1654*** promo
1655See notes on "Coroutines" above.
1656***
1657
1658### &lt;format&gt; <sup>[tbd]</sup>
1659
1660```c++
1661std::cout << std::format("Hello {}!\n", "world");
1662```
1663
1664**Description:** Utilities for producing formatted strings.
1665
1666**Documentation:**
1667[Formatting library](https://en.cppreference.com/w/cpp/utility/format)
1668
1669**Notes:**
1670*** promo
1671Has both pros and cons compared to `absl::StrFormat` (which we don't yet use).
1672Migration would be nontrivial.
1673***
1674
Peter Kastingf86817c2023-11-13 18:00:111675### &lt;source_location&gt; <sup>[tbd]</sup>
1676
1677```c++
1678#include <source_location>
1679```
1680
1681**Description:** Provides a class that can hold source code details such as
1682filenames, function names, and line numbers.
1683
1684**Documentation:**
1685[Standard library header `<source_location>`](https://en.cppreference.com/w/cpp/header/source_location)
1686
1687**Notes:**
1688*** promo
1689Seems to regress code size vs. `base::Location`.
1690***
1691
1692### &lt;span&gt; <sup>[tbd]</sup>
1693
1694```c++
1695#include <span>
1696```
1697
1698**Description:** Utilities for non-owning views over a sequence of objects.
1699
1700**Documentation:**
1701[](https://en.cppreference.com/w/cpp/header/span)
1702
1703**Notes:**
1704*** promo
1705Use `base::span` for now.
1706
1707[Migration bug](https://crbug.com/1414652)
1708***
1709
1710### std::u8string <sup>[tbd]</sup>
1711
1712```c++
1713std::u8string str = u8"Foo";
1714```
1715
1716**Description:** A string whose character type is `char8_t`, intended to hold
1717UTF-8-encoded text.
1718
1719**Documentation:**
1720[`std::basic_string`](https://en.cppreference.com/w/cpp/string/basic_string)
1721
1722**Notes:**
1723*** promo
1724See notes on `char8_t` above.
1725***
1726
Joe Masonfe4f2562021-09-15 15:23:131727## Abseil Banned Library Features {#absl-blocklist}
1728
1729The following Abseil library features are not allowed in the Chromium codebase.
1730
danakja6f71cb12021-12-15 21:04:491731### Any <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:131732
1733```c++
1734absl::any a = int{5};
1735EXPECT_THAT(absl::any_cast<int>(&a), Pointee(5));
1736EXPECT_EQ(absl::any_cast<size_t>(&a), nullptr);
1737```
1738
1739**Description:** Early adaptation of C++17 `std::any`.
1740
1741**Documentation:** [std::any](https://en.cppreference.com/w/cpp/utility/any)
1742
1743**Notes:**
1744*** promo
Peter Kasting72d110f12024-02-06 19:45:261745Banned since workaround for lack of RTTI
1746[isn't compatible with the component build](https://crbug.com/1096380). See also
1747`std::any`.
Joe Masonfe4f2562021-09-15 15:23:131748***
1749
Peter Kasting4f35bfc2022-10-18 18:39:121750### bind_front <sup>[banned]</sup>
1751
1752```c++
1753absl::bind_front
1754```
1755
1756**Description:** Binds the first N arguments of an invocable object and stores them by value.
1757
1758**Documentation:**
1759* [bind_front.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/bind_front.h)
1760* [Avoid std::bind](https://abseil.io/tips/108)
1761
1762**Notes:**
1763*** promo
Peter Kasting72d110f12024-02-06 19:45:261764Overlaps with `base::Bind`.
Peter Kasting4f35bfc2022-10-18 18:39:121765***
1766
danakja6f71cb12021-12-15 21:04:491767### Command line flags <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:131768
1769```c++
1770ABSL_FLAG(bool, logs, false, "print logs to stderr");
1771app --logs=true;
1772```
1773
1774**Description:** Allows programmatic access to flag values passed on the
1775command-line to binaries.
1776
1777**Documentation:** [Flags Library](https://abseil.io/docs/cpp/guides/flags)
1778
1779**Notes:**
1780*** promo
Peter Kasting72d110f12024-02-06 19:45:261781Banned since workaround for lack of RTTI
1782[isn't compatible with the component build](https://crbug.com/1096380). Use
1783`base::CommandLine` instead.
Joe Masonfe4f2562021-09-15 15:23:131784***
1785
Peter Kasting4f35bfc2022-10-18 18:39:121786### Container utilities <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:131787
1788```c++
Peter Kasting4f35bfc2022-10-18 18:39:121789auto it = absl::c_find(container, value);
Joe Masonfe4f2562021-09-15 15:23:131790```
1791
Peter Kasting4f35bfc2022-10-18 18:39:121792**Description:** Container-based versions of algorithmic functions within C++
1793standard library.
Joe Masonfe4f2562021-09-15 15:23:131794
Peter Kasting4f35bfc2022-10-18 18:39:121795**Documentation:**
1796[container.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/algorithm/container.h)
Joe Masonfe4f2562021-09-15 15:23:131797
1798**Notes:**
1799*** promo
Peter Kasting72d110f12024-02-06 19:45:261800Overlaps with `base/ranges/algorithm.h`.
Joe Masonfe4f2562021-09-15 15:23:131801***
1802
Peter Kasting431239a2023-09-29 03:11:441803### FixedArray <sup>[banned]</sup>
1804
1805```c++
1806absl::FixedArray<MyObj> objs_;
1807```
1808
1809**Description:** A fixed size array like `std::array`, but with size determined
1810at runtime instead of compile time.
1811
1812**Documentation:**
1813[fixed_array.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/container/fixed_array.h)
1814
1815**Notes:**
1816*** promo
1817Direct construction is banned due to the risk of UB with uninitialized
1818trivially-default-constructible types. Instead use `base/types/fixed_array.h`,
1819which is a light-weight wrapper that deletes the problematic constructor.
Joe Mason6a6f2582024-01-30 20:26:431820***
Peter Kasting431239a2023-09-29 03:11:441821
Daniel Cheng2248b332022-07-27 06:16:591822### FunctionRef <sup>[banned]</sup>
1823
1824```c++
1825absl::FunctionRef
1826```
1827
1828**Description:** Type for holding a non-owning reference to an object of any
1829invocable type.
1830
1831**Documentation:**
1832[function_ref.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/function_ref.h)
1833
1834**Notes:**
1835*** promo
1836- `absl::FunctionRef` is banned due to allowing implicit conversions between
1837 function signatures in potentially surprising ways. For example, a callable
1838 with the signature `int()` will bind to `absl::FunctionRef<void()>`: the
1839 return value from the callable will be silently discarded.
1840- In Chromium, use `base::FunctionRef` instead.
1841- Unlike `base::OnceCallback` and `base::RepeatingCallback`, `base::FunctionRef`
1842 supports capturing lambdas.
1843- Useful when passing an invocable object to a function that synchronously calls
1844 the invocable object, e.g. `ForEachFrame(base::FunctionRef<void(Frame&)>)`.
1845 This can often result in clearer code than code that is templated to accept
1846 lambdas, e.g. with `template <typename Invocable> void
1847 ForEachFrame(Invocable invocable)`, it is much less obvious what arguments
1848 will be passed to `invocable`.
1849- For now, `base::OnceCallback` and `base::RepeatingCallback` intentionally
1850 disallow conversions to `base::FunctionRef`, under the theory that the
1851 callback should be a capturing lambda instead. Attempting to use this
1852 conversion will trigger a `static_assert` requesting additional feedback for
1853 use cases where this conversion would be valuable.
1854- *Important:* `base::FunctionRef` must not outlive the function call. Like
Helmut Januschka1dce9dc2024-06-11 13:05:351855 `std::string_view`, `base::FunctionRef` is a *non-owning* reference. Using a
Daniel Cheng2248b332022-07-27 06:16:591856 `base::FunctionRef` as a return value or class field is dangerous and likely
1857 to result in lifetime bugs.
Peter Kasting72d110f12024-02-06 19:45:261858
1859[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/JVN4E4IIYA0)
Daniel Cheng2248b332022-07-27 06:16:591860***
1861
Peter Kasting4f35bfc2022-10-18 18:39:121862### Random <sup>[banned]</sup>
1863
1864```c++
1865absl::BitGen bitgen;
1866size_t index = absl::Uniform(bitgen, 0u, elems.size());
1867```
1868
1869**Description:** Functions and utilities for generating pseudorandom data.
1870
1871**Documentation:** [Random library](https://abseil.io/docs/cpp/guides/random)
1872
1873**Notes:**
1874*** promo
1875Banned because most uses of random values in Chromium should be using a
1876cryptographically secure generator. Use `base/rand_util.h` instead.
1877***
1878
1879### Span <sup>[banned]</sup>
1880
1881```c++
1882absl::Span
1883```
1884
1885**Description:** Early adaptation of C++20 `std::span`.
1886
1887**Documentation:** [Using absl::Span](https://abseil.io/tips/93)
1888
1889**Notes:**
1890*** promo
1891Banned due to being less std::-compliant than `base::span`. Keep using
1892`base::span`.
1893***
1894
1895### StatusOr <sup>[banned]</sup>
1896
1897```c++
1898absl::StatusOr<T>
1899```
1900
1901**Description:** An object that is either a usable value, or an error Status
1902explaining why such a value is not present.
1903
1904**Documentation:**
1905[statusor.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/status/statusor.h)
1906
1907**Notes:**
1908*** promo
Peter Kasting72d110f12024-02-06 19:45:261909Overlaps with `base::expected`.
Peter Kasting4f35bfc2022-10-18 18:39:121910***
1911
1912### String Formatting <sup>[banned]</sup>
1913
1914```c++
1915absl::StrFormat
1916```
1917
1918**Description:** A typesafe replacement for the family of printf() string
1919formatting routines.
1920
1921**Documentation:**
1922[String Formatting](https://abseil.io/docs/cpp/guides/format)
1923
1924**Notes:**
1925*** promo
Peter Kasting72d110f12024-02-06 19:45:261926Overlaps with `base::StringPrintf()`. See
Peter Kasting4f35bfc2022-10-18 18:39:121927[migration bug](https://bugs.chromium.org/p/chromium/issues/detail?id=1371963).
1928***
1929
1930### string_view <sup>[banned]</sup>
1931
1932```c++
1933absl::string_view
1934```
1935
1936**Description:** Early adaptation of C++17 `std::string_view`.
1937
1938**Documentation:** [absl::string_view](https://abseil.io/tips/1)
1939
1940**Notes:**
1941*** promo
David Benjaminea985a22023-04-18 22:05:011942Originally banned due to only working with 8-bit characters. Now it is
1943unnecessary because, in Chromium, it is the same type as `std::string_view`.
Hong Xu5e492d32023-07-27 21:38:461944Please use `std::string_view` instead.
Peter Kasting4f35bfc2022-10-18 18:39:121945***
1946
1947### Strings Library <sup>[banned]</sup>
1948
1949```c++
1950absl::StrSplit
1951absl::StrJoin
1952absl::StrCat
1953absl::StrAppend
1954absl::Substitute
1955absl::StrContains
1956```
1957
1958**Description:** Classes and utility functions for manipulating and comparing
1959strings.
1960
1961**Documentation:**
1962[String Utilities](https://abseil.io/docs/cpp/guides/strings)
1963
1964**Notes:**
1965*** promo
Peter Kasting72d110f12024-02-06 19:45:261966Overlaps with `base/strings`. We
Peter Kasting4f35bfc2022-10-18 18:39:121967[should re-evalute](https://bugs.chromium.org/p/chromium/issues/detail?id=1371966)
1968when we've
1969[migrated](https://bugs.chromium.org/p/chromium/issues/detail?id=691162) from
1970`base::StringPiece` to `std::string_view`.
1971***
1972
1973### Synchronization <sup>[banned]</sup>
1974
1975```c++
1976absl::Mutex
1977```
1978
1979**Description:** Primitives for managing tasks across different threads.
1980
1981**Documentation:**
1982[Synchronization](https://abseil.io/docs/cpp/guides/synchronization)
1983
1984**Notes:**
1985*** promo
Peter Kasting72d110f12024-02-06 19:45:261986Overlaps with `base/synchronization/`. We would love
Peter Kasting4f35bfc2022-10-18 18:39:121987[more testing](https://bugs.chromium.org/p/chromium/issues/detail?id=1371969) on
1988whether there are compelling reasons to prefer base, absl, or std
1989synchronization primitives; for now, use `base/synchronization/`.
1990***
1991
1992### Time library <sup>[banned]</sup>
1993
1994```c++
1995absl::Duration
1996absl::Time
1997absl::TimeZone
1998absl::CivilDay
1999```
2000
2001**Description:** Abstractions for holding time values, both in terms of
2002absolute time and civil time.
2003
2004**Documentation:** [Time](https://abseil.io/docs/cpp/guides/time)
2005
2006**Notes:**
2007*** promo
Peter Kasting72d110f12024-02-06 19:45:262008Overlaps with `base/time/`.
Peter Kasting4f35bfc2022-10-18 18:39:122009***
2010
Joe Masonfe4f2562021-09-15 15:23:132011## Abseil TBD Features {#absl-review}
2012
2013The following Abseil library features are not allowed in the Chromium codebase.
2014See the top of this page on how to propose moving a feature from this list into
2015the allowed or banned sections.
2016
Danil Chapovalova9f27032022-06-20 16:56:142017### AnyInvocable <sup>[tbd]</sup>
2018
2019```c++
2020absl::AnyInvocable
2021```
2022
2023**Description:** An equivalent of the C++23 std::move_only_function.
2024
2025**Documentation:**
2026* [any_invocable.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/any_invocable.h)
2027* [std::move_only_function](https://en.cppreference.com/w/cpp/utility/functional/move_only_function/move_only_function)
2028
2029**Notes:**
2030*** promo
2031Overlaps with `base::RepeatingCallback`, `base::OnceCallback`.
2032***
2033
danakja6f71cb12021-12-15 21:04:492034### Containers <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:132035
2036```c++
2037absl::flat_hash_map
2038absl::flat_hash_set
2039absl::node_hash_map
2040absl::node_hash_set
2041absl::btree_map
2042absl::btree_set
2043absl::btree_multimap
2044absl::btree_multiset
Joe Masonfe4f2562021-09-15 15:23:132045```
2046
2047**Description:** Alternatives to STL containers designed to be more efficient
2048in the general case.
2049
2050**Documentation:**
2051* [Containers](https://abseil.io/docs/cpp/guides/container)
2052* [Hash](https://abseil.io/docs/cpp/guides/hash)
2053
2054**Notes:**
2055*** promo
2056Supplements `base/containers/`.
Adam Riceaf2708e2023-07-20 14:53:162057
2058absl::InlinedVector is explicitly allowed, see the [discussion
2059thread](https://groups.google.com/a/chromium.org/g/cxx/c/jTfqVfU-Ka0/m/caaal90NCgAJ).
2060
Joe Masonfe4f2562021-09-15 15:23:132061***
2062
Mirko Bonadeide812cd2022-12-07 22:38:322063### CRC32C library <sup>[tbd]</sup>
2064
2065**Description:** API for computing CRC32C values as checksums for arbitrary
2066sequences of bytes provided as a string buffer.
2067
2068**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:082069[crc32.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/crc/crc32c.h)
Mirko Bonadeide812cd2022-12-07 22:38:322070
2071**Notes:**
2072*** promo
Peter Kasting72d110f12024-02-06 19:45:262073Overlaps with `third_party/crc32c`.
Mirko Bonadeide812cd2022-12-07 22:38:322074***
2075
Peter Kasting4f35bfc2022-10-18 18:39:122076### Log macros and related classes <sup>[tbd]</sup>
Danil Chapovalov6719fb12022-08-31 13:52:492077
2078```c++
2079LOG(INFO) << message;
2080CHECK(condition);
2081absl::AddLogSink(&custom_sink_to_capture_absl_logs);
2082```
2083
2084**Description:** Macros and related classes to perform debug loggings
2085
2086**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:082087[log.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/log/log.h)
2088[check.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/log/check.h)
Danil Chapovalov6719fb12022-08-31 13:52:492089
2090**Notes:**
2091*** promo
Peter Kasting72d110f12024-02-06 19:45:262092Overlaps with `base/logging.h`.
Danil Chapovalov6719fb12022-08-31 13:52:492093***
Danil Chapovalov79a40942023-06-21 18:35:302094
Mirko Bonadeidc928a162023-11-20 08:24:232095### NoDestructor <sup>[tbd]</sup>
2096
2097```c++
2098// Global or namespace scope.
2099ABSL_CONST_INIT absl::NoDestructor<MyRegistry> reg{"foo", "bar", 8008};
2100
2101// Function scope.
2102const std::string& MyString() {
2103 static const absl::NoDestructor<std::string> x("foo");
2104 return *x;
2105}
2106```
2107
2108**Description:** `absl::NoDestructor<T>` is a wrapper around an object of
2109type T that behaves as an object of type T but never calls T's destructor.
2110
2111**Documentation:**
2112[no_destructor.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/base/no_desctructor.h)
2113
2114**Notes:**
2115*** promo
2116Overlaps with `base::NoDestructor`.
2117***
2118
Danil Chapovalov79a40942023-06-21 18:35:302119### Nullability annotations <sup>[tbd]</sup>
2120
2121```c++
2122void PaySalary(absl::NotNull<Employee *> employee) {
2123 pay(*employee); // OK to dereference
2124}
2125```
2126
2127**Description:** Annotations to more clearly specify contracts
2128
2129**Documentation:**
2130[nullability.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/base/nullability.h)
2131
2132**Notes:**
2133*** promo
2134These nullability annotations are primarily a human readable signal about the
2135intended contract of the pointer. They are not *types* and do not currently
2136provide any correctness guarantees.
2137***
Danil Chapovalov0e8ed132023-09-28 08:05:392138
2139### Overload <sup>[tbd]</sup>
2140
2141```c++
2142std::variant<int, std::string, double> v(int{1});
2143assert(std::visit(absl::Overload(
2144 [](int) -> absl::string_view { return "int"; },
2145 [](const std::string&) -> absl::string_view {
2146 return "string";
2147 },
2148 [](double) -> absl::string_view { return "double"; }),
2149 v) == "int");
2150```
2151
2152**Description:** Returns a functor that provides overloads based on the functors passed to it
2153
2154**Documentation:**
Adam Ricebf82f342023-09-29 15:07:312155[overload.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/overload.h)
Danil Chapovalov0e8ed132023-09-28 08:05:392156
2157**Notes:**
2158*** promo
Peter Kasting72d110f12024-02-06 19:45:262159Overlaps with `base::Overloaded`.
Danil Chapovalov0e8ed132023-09-28 08:05:392160***