blob: 5c8136d1461871b1fa3673ebb7edd52642c38f3d [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
1068### Library feature-test macros and &lt;version&gt; <sup>[allowed]</sup>
1069
1070```c++
1071#if !defined(__cpp_lib_atomic_value_initialization) || \
1072 (__cpp_lib_atomic_value_initialization < 201911L)
1073... // `std::atomic` is not value-initialized by default.
1074#endif
1075```
1076
1077**Description:** Provides a standardized way to test the toolchain's
1078implementation of a particular library feature.
1079
1080**Documentation:**
1081[Feature testing](https://en.cppreference.com/w/cpp/feature_test)
1082
1083**Notes:**
1084*** promo
1085None
1086***
1087
1088### &lt;numbers&gt; <sup>[allowed]</sup>
1089
1090```c++
1091#include <numbers>
1092```
1093
1094**Description:** Provides compile-time constants for many common mathematical
1095values, e.g. pi and e.
1096
1097**Documentation:**
1098[Mathematical constants](https://en.cppreference.com/w/cpp/numeric/constants)
1099
1100**Notes:**
1101*** promo
1102[Migration bug](https://crbug.com/1414635)
1103***
1104
1105### std::assume_aligned <sup>[allowed]</sup>
1106
1107```c++
1108void f(int* p) {
1109 int* aligned = std::assume_aligned<256>(p);
1110 ...
1111```
1112
1113**Description:** Informs the compiler that a pointer points to an address
1114aligned to at least some particular power of 2.
1115
1116**Documentation:**
1117[`std::assume_aligned`](https://en.cppreference.com/w/cpp/memory/assume_aligned)
1118
1119**Notes:**
1120*** promo
Peter Kastingb2887c32024-01-08 21:41:041121None
Peter Kastingf86817c2023-11-13 18:00:111122***
1123
1124### std::erase[_if] for containers <sup>[allowed]</sup>
1125
1126```c++
1127std::vector<int> numbers = ...;
1128std::erase_if(numbers, [](int x) { return x % 2 == 0; });
1129```
1130
1131**Description:** Erases from a container by value comparison or predicate,
1132avoiding the need to use the `erase(remove(...` paradigm.
1133
1134**Documentation:**
1135[`std::erase`, `std::erase_if` (`std::vector`)](https://en.cppreference.com/w/cpp/container/vector/erase2)
1136
1137**Notes:**
1138*** promo
1139[Migration bug](https://crbug.com/1414639)
1140***
1141
1142### std::is_[un]bounded_array <sup>[allowed]</sup>
1143
1144```c++
1145template <typename T>
1146static constexpr bool kBoundedArray = std::is_bounded_array_v<T>;
1147```
1148
1149**Description:** Checks if a type is an array type with a known or unknown
1150bound.
1151
1152**Documentation:**
1153[`std::is_bounded_array`](https://en.cppreference.com/w/cpp/types/is_bounded_array),
1154[`std::is_unbounded_array`](https://en.cppreference.com/w/cpp/types/is_unbounded_array)
1155
1156**Notes:**
1157*** promo
1158None
1159***
1160
1161### std::lerp <sup>[allowed]</sup>
1162
1163```c++
1164double val = std::lerp(start, end, t);
1165```
1166
1167**Description:** Linearly interpolates (or extrapolates) between two values.
1168
1169**Documentation:**
1170[`std::lerp`](https://en.cppreference.com/w/cpp/numeric/lerp)
1171
1172**Notes:**
1173*** promo
1174[Migration bug](https://crbug.com/1414537)
1175***
1176
1177### std::make_obj_using_allocator etc. <sup>[allowed]</sup>
1178
1179```c++
1180auto obj = std::make_obj_using_allocator<Obj>(alloc, ...);
1181```
1182
1183**Description:** Constructs an object using
1184[uses-allocator construction](https://en.cppreference.com/w/cpp/memory/uses_allocator).
1185
1186**Documentation:**
1187[`std::make_obj_using_allocator`](https://en.cppreference.com/w/cpp/memory/make_obj_using_allocator)
1188
1189**Notes:**
1190*** promo
1191None
1192***
1193
1194### std::make_unique_for_overwrite <sup>[allowed]</sup>
1195
1196```c++
1197auto ptr = std::make_unique_for_overwrite<int>(); // `*ptr` is uninitialized
1198```
1199
1200**Description:** Like calling `std::unique_ptr<T>(new T)` instead of the more
1201typical `std::unique_ptr<T>(new T(...))`.
1202
1203**Documentation:**
1204[`std::make_unique`, `std::make_unique_for_overwrite`](https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique)
1205
1206**Notes:**
1207*** promo
1208None
1209***
1210
1211### std::midpoint <sup>[allowed]</sup>
1212
1213```c++
1214int center = std::midpoint(top, bottom);
1215```
1216
1217**Description:** Finds the midpoint between its two arguments, avoiding any
1218possible overflow. For integral inputs, rounds towards the first argument.
1219
1220**Documentation:**
1221[`std::midpoint`](https://en.cppreference.com/w/cpp/numeric/midpoint)
1222
1223**Notes:**
1224*** promo
1225[Migration bug](https://crbug.com/1414539)
1226***
1227
1228### std::remove_cvref[_t] <sup>[allowed]</sup>
1229
1230```c++
1231template <typename T,
1232 typename = std::enable_if_t<std::is_same_v<std::remove_cvref_t<T>,
1233 int>>>
1234void foo(T t);
1235```
1236
1237**Description:** Provides a way to remove const, volatile, and reference
1238qualifiers from a type.
1239
1240**Documentation:**
1241[`std::remove_cvref`](https://en.cppreference.com/w/cpp/types/remove_cvref)
1242
1243**Notes:**
1244*** promo
Peter Kastingb2887c32024-01-08 21:41:041245None
Peter Kastingf86817c2023-11-13 18:00:111246***
1247
1248### std::ssize <sup>[allowed]</sup>
1249
1250```c++
1251str.replace(it, it + std::ssize(substr), 1, 'x');
1252```
1253
1254**Description:** Returns the size of an object as a signed type.
1255
1256**Documentation:**
1257[`std::size`, `std::ssize`](https://en.cppreference.com/w/cpp/iterator/size)
1258
1259**Notes:**
1260*** promo
1261[Migration bug](https://crbug.com/1414543)
1262***
1263
1264### std::string::(starts,ends)_with <sup>[allowed]</sup>
1265
1266```c++
1267const std::string str = "Foo bar";
1268const bool is_true = str.ends_with("bar");
1269```
1270
1271**Description:** Tests whether a string starts or ends with a particular
1272character or string.
1273
1274**Documentation:**
1275[`std::basic_string<CharT,Traits,Allocator>::starts_with`](https://en.cppreference.com/w/cpp/string/basic_string/starts_with),
1276[`std::basic_string<CharT,Traits,Allocator>::ends_with`](https://en.cppreference.com/w/cpp/string/basic_string/ends_with)
1277
1278**Notes:**
1279*** promo
1280[Migration bug](https://crbug.com/1414647)
1281***
1282
Peter Kastingf86817c2023-11-13 18:00:111283## C++20 Banned Language Features {#core-blocklist-20}
1284
1285The following C++20 language features are not allowed in the Chromium codebase.
1286
Peter Kasting72d110f12024-02-06 19:45:261287### char8_t <sup>[banned]</sup>
1288
1289```c++
1290char8_t c = u8'x';
1291```
1292
1293**Description:** A single UTF-8 code unit. Similar to `unsigned char`, but
1294considered a distinct type.
1295
1296**Documentation:**
1297[Fundamental types](https://en.cppreference.com/w/cpp/language/types#char8_t)
1298
1299**Notes:**
1300*** promo
1301Use `char` and unprefixed character literals. Non-UTF-8 encodings are rare
1302enough in Chromium that the value of distinguishing them at the type level is
1303low, and `char8_t*` is not interconvertible with `char*` (what ~all Chromium,
1304STL, and platform-specific APIs use), so using `u8` prefixes would obligate us
1305to insert casts everywhere. If you want to declare at a type level that a block
1306of data is string-like and not an arbitrary binary blob, prefer
1307`std::string[_view]` over `char*`.
1308***
1309
Peter Kastingf86817c2023-11-13 18:00:111310### Modules <sup>[banned]</sup>
1311
1312```c++
1313export module helloworld; // module declaration
1314
1315import <iostream>; // import declaration
1316
1317export void hello() { // export declaration
1318 std::cout << "Hello world!\n";
1319}
1320```
1321
1322**Description:** Modules provide an alternative to many uses of headers which
1323allows for faster compilation, better tooling support, and reduction of problems
1324like "include what you use".
1325
1326**Documentation:**
1327[Modules](https://en.cppreference.com/w/cpp/language/modules)
1328
1329**Notes:**
1330*** promo
1331Not yet sufficiently supported in Clang and GN. Re-evaluate when support
1332improves.
1333***
1334
Peter Kasting8bc046d22023-11-14 00:38:031335### [[no_unique_address]] <sup>[banned]</sup>
1336
1337```c++
1338struct Empty {};
1339struct X {
1340 int i;
1341 [[no_unique_address]] Empty e;
1342};
1343```
1344
1345**Description:** Allows a data member to be overlapped with other members.
1346
1347**Documentation:**
1348[C++ attribute: `no_unique_address`](https://en.cppreference.com/w/cpp/language/attributes/no_unique_address)
1349
1350**Notes:**
1351*** promo
1352Has no effect on Windows, for compatibility with Microsoft's ABI. Use
1353`NO_UNIQUE_ADDRESS` from `base/compiler_specific.h` instead. Do not use (either
1354form) on members of unions due to
1355[potential memory safety problems](https://github.com/llvm/llvm-project/issues/60711).
Peter Kasting8bc046d22023-11-14 00:38:031356***
1357
Peter Kastingf86817c2023-11-13 18:00:111358## C++20 Banned Library Features {#library-blocklist-20}
1359
1360The following C++20 library features are not allowed in the Chromium codebase.
1361
1362### std::atomic_ref <sup>[banned]</sup>
1363
1364```c++
1365struct S { int a; int b; };
1366S not_atomic;
1367std::atomic_ref<S> is_atomic(not_atomic);
1368```
1369
1370**Description:** Allows atomic access to objects that might not themselves be
1371atomic types. While any atomic_ref to an object exists, the object must be
1372accessed exclusively through atomic_ref instances.
1373
1374**Documentation:**
1375[`std::atomic_ref`](https://en.cppreference.com/w/cpp/atomic/atomic_ref)
1376
1377**Notes:**
1378*** promo
1379Banned due to being [unimplemented in libc++](https://reviews.llvm.org/D72240).
1380
1381[Migration bug](https://crbug.com/1422701) (once this is allowed)
1382***
1383
1384### std::bind_front <sup>[banned]</sup>
1385
1386```c++
1387int minus(int a, int b);
1388auto fifty_minus_x = std::bind_front(minus, 50);
1389int forty = fifty_minus_x(10);
1390```
1391
1392**Description:** An updated version of `std::bind` with fewer gotchas, similar
1393to `absl::bind_front`.
1394
1395**Documentation:**
1396[`std::bind_front`, `std::bind_back`](https://en.cppreference.com/w/cpp/utility/functional/bind_front)
1397
1398**Notes:**
1399*** promo
1400Overlaps with `base::Bind`.
1401***
1402
Avi Drissman70cb7f72023-12-12 17:44:371403### std::bit_cast <sup>[banned]</sup>
1404
1405```c++
1406float quake_rsqrt(float number) {
1407 long i = std::bit_cast<long>(number);
1408 i = 0x5f3759df - (i >> 1); // wtf?
1409 float y = std::bit_cast<float>(i);
1410 return y * (1.5f - (0.5f * number * y * y));
1411}
1412```
1413
1414**Description:** Returns an value constructed with the same bits as an value of
1415a different type.
1416
1417**Documentation:**
1418[`std::bit_cast`](https://en.cppreference.com/w/cpp/numeric/bit_cast)
1419
1420**Notes:**
1421*** promo
1422The `std::` version of `bit_cast` allows casting of pointer and reference types,
1423which is both useless in that it doesn't avoid UB, and dangerous in that it
1424allows arbitrary casting away of modifiers like `const`. Instead of using
1425`bit_cast` on pointers, use standard C++ casts. For use on values, use
1426`base::bit_cast` which does not allow this unwanted usage.
1427***
1428
Peter Kastingf86817c2023-11-13 18:00:111429### std::{c8rtomb,mbrtoc8} <sup>[banned]</sup>
1430
1431```c++
1432std::u8string_view strv = u8"zß水🍌";
1433std::mbstate_t state;
1434char out[MB_LEN_MAX] = {0};
1435for (char8_t c : strv) {
1436 size_t rc = std::c8rtomb(out, c, &state);
1437 ...
1438```
1439
1440**Description:** Converts a code point between UTF-8 and a multibyte character
1441encoded using the current C locale.
1442
1443**Documentation:**
1444[`std::c8rtomb`](https://en.cppreference.com/w/cpp/string/multibyte/c8rtomb),
1445[`std::mbrtoc8`](https://en.cppreference.com/w/cpp/string/multibyte/mbrtoc8)
1446
1447**Notes:**
1448*** promo
1449Chromium functionality should not vary with the C locale.
1450***
1451
Nick Diego Yamanee522ae82024-02-27 04:23:221452### std::to_address <sup>[banned]</sup>
1453
1454```c++
1455std::vector<int> numbers;
1456int* i = std::to_address(numbers.begin());
1457```
1458
1459**Description:** Converts a pointer-like object to a pointer, even if the
1460pointer does not refer to a constructed object (in which case an expression like
1461`&*p` is UB).
1462
1463**Documentation:**
1464[`std::to_address`](https://en.cppreference.com/w/cpp/memory/to_address)
1465
1466**Notes:**
1467*** promo
1468Banned because it is not guaranteed to be SFINAE-compatible. Use
1469base::to_address, which does guarantee this.
1470***
1471
Peter Kastingf86817c2023-11-13 18:00:111472### &lt;syncstream&gt; <sup>[banned]</sup>
1473
1474```c++
1475#include <syncstream>
1476```
1477
1478**Description:** Facilities for multithreaded access to streams.
1479
1480**Documentation:**
1481[Standard library header `<syncstream>`](https://en.cppreference.com/w/cpp/header/syncstream)
1482
1483**Notes:**
1484*** promo
1485Banned due to being unimplemented per
1486[the libc++ C++20 status page](https://libcxx.llvm.org/Status/Cxx20.html).
1487Reevaluate usefulness once implemented.
1488***
1489
1490## C++20 TBD Language Features {#core-review-20}
1491
1492The following C++20 language features are not allowed in the Chromium codebase.
1493See the top of this page on how to propose moving a feature from this list into
1494the allowed or banned sections.
1495
1496### Aggregate initialization using parentheses <sup>[tbd]</sup>
1497
1498```c++
1499struct B {
1500 int a;
1501 int&& r;
1502} b2(1, 1); // Warning: dangling reference
1503```
1504
1505**Description:** Allows initialization of aggregates using parentheses, not just
1506braces.
1507
1508**Documentation:**
1509[Aggregate initialization](https://en.cppreference.com/w/cpp/language/aggregate_initialization),
1510[Direct initialization](https://en.cppreference.com/w/cpp/language/direct_initialization)
1511
1512**Notes:**
1513*** promo
1514There are subtle but important differences between brace- and paren-init of
1515aggregates. The parenthesis style appears to have more pitfalls (allowing
1516narrowing conversions, not extending lifetimes of temporaries bound to
1517references).
1518***
1519
Peter Kastingf86817c2023-11-13 18:00:111520### Coroutines <sup>[tbd]</sup>
1521
1522```c++
1523co_return 1;
1524```
1525
1526**Description:** Allows writing functions that logically block while physically
1527returning control to a caller. This enables writing some kinds of async code in
1528simple, straight-line ways without storing state in members or binding
1529callbacks.
1530
1531**Documentation:**
1532[Coroutines](https://en.cppreference.com/w/cpp/language/coroutines)
1533
1534**Notes:**
1535*** promo
1536Requires significant support code and planning around API and migration.
1537
1538[Prototyping bug](https://crbug.com/1403840)
1539***
1540
1541### [[likely]], [[unlikely]] <sup>[tbd]</sup>
1542
1543```c++
1544if (n > 0) [[likely]] {
1545 return 1;
1546}
1547```
1548
1549**Description:** Tells the optimizer that a particular codepath is more or less
1550likely than an alternative.
1551
1552**Documentation:**
1553[C++ attribute: `likely`, `unlikely`](https://en.cppreference.com/w/cpp/language/attributes/likely)
1554
1555**Notes:**
1556*** promo
1557[Will be allowed soon](https://crbug.com/1414620); for now, use `[UN]LIKELY`.
1558***
1559
1560## C++20 TBD Library Features {#library-review-20}
1561
1562The following C++20 library features are not allowed in the Chromium codebase.
1563See the top of this page on how to propose moving a feature from this list into
1564the allowed or banned sections.
1565
1566### &lt;coroutine&gt; <sup>[tbd]</sup>
1567
1568```c++
1569#include <coroutine>
1570```
1571
1572**Description:** Header which defines various core coroutine types.
1573
1574**Documentation:**
1575[Coroutine support](https://en.cppreference.com/w/cpp/coroutine)
1576
1577**Notes:**
1578*** promo
1579See notes on "Coroutines" above.
1580***
1581
1582### &lt;format&gt; <sup>[tbd]</sup>
1583
1584```c++
1585std::cout << std::format("Hello {}!\n", "world");
1586```
1587
1588**Description:** Utilities for producing formatted strings.
1589
1590**Documentation:**
1591[Formatting library](https://en.cppreference.com/w/cpp/utility/format)
1592
1593**Notes:**
1594*** promo
1595Has both pros and cons compared to `absl::StrFormat` (which we don't yet use).
1596Migration would be nontrivial.
1597***
1598
1599### &lt;ranges&gt; <sup>[tbd]</sup>
1600
1601```c++
1602constexpr int arr[] = {6, 2, 8, 4, 4, 2};
1603constexpr auto plus_one = std::views::transform([](int n){ return n + 1; });
1604static_assert(std::ranges::equal(arr | plus_one, {7, 3, 9, 5, 5, 3}));
1605```
1606
1607**Description:** Generalizes algorithms using range views, which are lightweight
1608objects that represent iterable sequences. Provides facilities for eager and
1609lazy operations on ranges, along with composition into pipelines.
1610
1611**Documentation:**
1612[Ranges library](https://en.cppreference.com/w/cpp/ranges)
1613
1614**Notes:**
1615*** promo
1616Significant concerns expressed internally. We should consider whether there are
1617clearly-safe pieces to allow (e.g. to replace `base/ranges/algorithm.h`) and
1618engage with the internal library team.
1619***
1620
1621### &lt;source_location&gt; <sup>[tbd]</sup>
1622
1623```c++
1624#include <source_location>
1625```
1626
1627**Description:** Provides a class that can hold source code details such as
1628filenames, function names, and line numbers.
1629
1630**Documentation:**
1631[Standard library header `<source_location>`](https://en.cppreference.com/w/cpp/header/source_location)
1632
1633**Notes:**
1634*** promo
1635Seems to regress code size vs. `base::Location`.
1636***
1637
1638### &lt;span&gt; <sup>[tbd]</sup>
1639
1640```c++
1641#include <span>
1642```
1643
1644**Description:** Utilities for non-owning views over a sequence of objects.
1645
1646**Documentation:**
1647[](https://en.cppreference.com/w/cpp/header/span)
1648
1649**Notes:**
1650*** promo
1651Use `base::span` for now.
1652
1653[Migration bug](https://crbug.com/1414652)
1654***
1655
1656### std::u8string <sup>[tbd]</sup>
1657
1658```c++
1659std::u8string str = u8"Foo";
1660```
1661
1662**Description:** A string whose character type is `char8_t`, intended to hold
1663UTF-8-encoded text.
1664
1665**Documentation:**
1666[`std::basic_string`](https://en.cppreference.com/w/cpp/string/basic_string)
1667
1668**Notes:**
1669*** promo
1670See notes on `char8_t` above.
1671***
1672
Joe Masonfe4f2562021-09-15 15:23:131673## Abseil Banned Library Features {#absl-blocklist}
1674
1675The following Abseil library features are not allowed in the Chromium codebase.
1676
danakja6f71cb12021-12-15 21:04:491677### Any <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:131678
1679```c++
1680absl::any a = int{5};
1681EXPECT_THAT(absl::any_cast<int>(&a), Pointee(5));
1682EXPECT_EQ(absl::any_cast<size_t>(&a), nullptr);
1683```
1684
1685**Description:** Early adaptation of C++17 `std::any`.
1686
1687**Documentation:** [std::any](https://en.cppreference.com/w/cpp/utility/any)
1688
1689**Notes:**
1690*** promo
Peter Kasting72d110f12024-02-06 19:45:261691Banned since workaround for lack of RTTI
1692[isn't compatible with the component build](https://crbug.com/1096380). See also
1693`std::any`.
Joe Masonfe4f2562021-09-15 15:23:131694***
1695
Peter Kasting4f35bfc2022-10-18 18:39:121696### bind_front <sup>[banned]</sup>
1697
1698```c++
1699absl::bind_front
1700```
1701
1702**Description:** Binds the first N arguments of an invocable object and stores them by value.
1703
1704**Documentation:**
1705* [bind_front.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/bind_front.h)
1706* [Avoid std::bind](https://abseil.io/tips/108)
1707
1708**Notes:**
1709*** promo
Peter Kasting72d110f12024-02-06 19:45:261710Overlaps with `base::Bind`.
Peter Kasting4f35bfc2022-10-18 18:39:121711***
1712
danakja6f71cb12021-12-15 21:04:491713### Command line flags <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:131714
1715```c++
1716ABSL_FLAG(bool, logs, false, "print logs to stderr");
1717app --logs=true;
1718```
1719
1720**Description:** Allows programmatic access to flag values passed on the
1721command-line to binaries.
1722
1723**Documentation:** [Flags Library](https://abseil.io/docs/cpp/guides/flags)
1724
1725**Notes:**
1726*** promo
Peter Kasting72d110f12024-02-06 19:45:261727Banned since workaround for lack of RTTI
1728[isn't compatible with the component build](https://crbug.com/1096380). Use
1729`base::CommandLine` instead.
Joe Masonfe4f2562021-09-15 15:23:131730***
1731
Peter Kasting4f35bfc2022-10-18 18:39:121732### Container utilities <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:131733
1734```c++
Peter Kasting4f35bfc2022-10-18 18:39:121735auto it = absl::c_find(container, value);
Joe Masonfe4f2562021-09-15 15:23:131736```
1737
Peter Kasting4f35bfc2022-10-18 18:39:121738**Description:** Container-based versions of algorithmic functions within C++
1739standard library.
Joe Masonfe4f2562021-09-15 15:23:131740
Peter Kasting4f35bfc2022-10-18 18:39:121741**Documentation:**
1742[container.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/algorithm/container.h)
Joe Masonfe4f2562021-09-15 15:23:131743
1744**Notes:**
1745*** promo
Peter Kasting72d110f12024-02-06 19:45:261746Overlaps with `base/ranges/algorithm.h`.
Joe Masonfe4f2562021-09-15 15:23:131747***
1748
Peter Kasting431239a2023-09-29 03:11:441749### FixedArray <sup>[banned]</sup>
1750
1751```c++
1752absl::FixedArray<MyObj> objs_;
1753```
1754
1755**Description:** A fixed size array like `std::array`, but with size determined
1756at runtime instead of compile time.
1757
1758**Documentation:**
1759[fixed_array.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/container/fixed_array.h)
1760
1761**Notes:**
1762*** promo
1763Direct construction is banned due to the risk of UB with uninitialized
1764trivially-default-constructible types. Instead use `base/types/fixed_array.h`,
1765which is a light-weight wrapper that deletes the problematic constructor.
Joe Mason6a6f2582024-01-30 20:26:431766***
Peter Kasting431239a2023-09-29 03:11:441767
Daniel Cheng2248b332022-07-27 06:16:591768### FunctionRef <sup>[banned]</sup>
1769
1770```c++
1771absl::FunctionRef
1772```
1773
1774**Description:** Type for holding a non-owning reference to an object of any
1775invocable type.
1776
1777**Documentation:**
1778[function_ref.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/function_ref.h)
1779
1780**Notes:**
1781*** promo
1782- `absl::FunctionRef` is banned due to allowing implicit conversions between
1783 function signatures in potentially surprising ways. For example, a callable
1784 with the signature `int()` will bind to `absl::FunctionRef<void()>`: the
1785 return value from the callable will be silently discarded.
1786- In Chromium, use `base::FunctionRef` instead.
1787- Unlike `base::OnceCallback` and `base::RepeatingCallback`, `base::FunctionRef`
1788 supports capturing lambdas.
1789- Useful when passing an invocable object to a function that synchronously calls
1790 the invocable object, e.g. `ForEachFrame(base::FunctionRef<void(Frame&)>)`.
1791 This can often result in clearer code than code that is templated to accept
1792 lambdas, e.g. with `template <typename Invocable> void
1793 ForEachFrame(Invocable invocable)`, it is much less obvious what arguments
1794 will be passed to `invocable`.
1795- For now, `base::OnceCallback` and `base::RepeatingCallback` intentionally
1796 disallow conversions to `base::FunctionRef`, under the theory that the
1797 callback should be a capturing lambda instead. Attempting to use this
1798 conversion will trigger a `static_assert` requesting additional feedback for
1799 use cases where this conversion would be valuable.
1800- *Important:* `base::FunctionRef` must not outlive the function call. Like
Helmut Januschka1dce9dc2024-06-11 13:05:351801 `std::string_view`, `base::FunctionRef` is a *non-owning* reference. Using a
Daniel Cheng2248b332022-07-27 06:16:591802 `base::FunctionRef` as a return value or class field is dangerous and likely
1803 to result in lifetime bugs.
Peter Kasting72d110f12024-02-06 19:45:261804
1805[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/JVN4E4IIYA0)
Daniel Cheng2248b332022-07-27 06:16:591806***
1807
Peter Kasting4f35bfc2022-10-18 18:39:121808### Random <sup>[banned]</sup>
1809
1810```c++
1811absl::BitGen bitgen;
1812size_t index = absl::Uniform(bitgen, 0u, elems.size());
1813```
1814
1815**Description:** Functions and utilities for generating pseudorandom data.
1816
1817**Documentation:** [Random library](https://abseil.io/docs/cpp/guides/random)
1818
1819**Notes:**
1820*** promo
1821Banned because most uses of random values in Chromium should be using a
1822cryptographically secure generator. Use `base/rand_util.h` instead.
1823***
1824
1825### Span <sup>[banned]</sup>
1826
1827```c++
1828absl::Span
1829```
1830
1831**Description:** Early adaptation of C++20 `std::span`.
1832
1833**Documentation:** [Using absl::Span](https://abseil.io/tips/93)
1834
1835**Notes:**
1836*** promo
1837Banned due to being less std::-compliant than `base::span`. Keep using
1838`base::span`.
1839***
1840
1841### StatusOr <sup>[banned]</sup>
1842
1843```c++
1844absl::StatusOr<T>
1845```
1846
1847**Description:** An object that is either a usable value, or an error Status
1848explaining why such a value is not present.
1849
1850**Documentation:**
1851[statusor.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/status/statusor.h)
1852
1853**Notes:**
1854*** promo
Peter Kasting72d110f12024-02-06 19:45:261855Overlaps with `base::expected`.
Peter Kasting4f35bfc2022-10-18 18:39:121856***
1857
1858### String Formatting <sup>[banned]</sup>
1859
1860```c++
1861absl::StrFormat
1862```
1863
1864**Description:** A typesafe replacement for the family of printf() string
1865formatting routines.
1866
1867**Documentation:**
1868[String Formatting](https://abseil.io/docs/cpp/guides/format)
1869
1870**Notes:**
1871*** promo
Peter Kasting72d110f12024-02-06 19:45:261872Overlaps with `base::StringPrintf()`. See
Peter Kasting4f35bfc2022-10-18 18:39:121873[migration bug](https://bugs.chromium.org/p/chromium/issues/detail?id=1371963).
1874***
1875
1876### string_view <sup>[banned]</sup>
1877
1878```c++
1879absl::string_view
1880```
1881
1882**Description:** Early adaptation of C++17 `std::string_view`.
1883
1884**Documentation:** [absl::string_view](https://abseil.io/tips/1)
1885
1886**Notes:**
1887*** promo
David Benjaminea985a22023-04-18 22:05:011888Originally banned due to only working with 8-bit characters. Now it is
1889unnecessary because, in Chromium, it is the same type as `std::string_view`.
Hong Xu5e492d32023-07-27 21:38:461890Please use `std::string_view` instead.
Peter Kasting4f35bfc2022-10-18 18:39:121891***
1892
1893### Strings Library <sup>[banned]</sup>
1894
1895```c++
1896absl::StrSplit
1897absl::StrJoin
1898absl::StrCat
1899absl::StrAppend
1900absl::Substitute
1901absl::StrContains
1902```
1903
1904**Description:** Classes and utility functions for manipulating and comparing
1905strings.
1906
1907**Documentation:**
1908[String Utilities](https://abseil.io/docs/cpp/guides/strings)
1909
1910**Notes:**
1911*** promo
Peter Kasting72d110f12024-02-06 19:45:261912Overlaps with `base/strings`. We
Peter Kasting4f35bfc2022-10-18 18:39:121913[should re-evalute](https://bugs.chromium.org/p/chromium/issues/detail?id=1371966)
1914when we've
1915[migrated](https://bugs.chromium.org/p/chromium/issues/detail?id=691162) from
1916`base::StringPiece` to `std::string_view`.
1917***
1918
1919### Synchronization <sup>[banned]</sup>
1920
1921```c++
1922absl::Mutex
1923```
1924
1925**Description:** Primitives for managing tasks across different threads.
1926
1927**Documentation:**
1928[Synchronization](https://abseil.io/docs/cpp/guides/synchronization)
1929
1930**Notes:**
1931*** promo
Peter Kasting72d110f12024-02-06 19:45:261932Overlaps with `base/synchronization/`. We would love
Peter Kasting4f35bfc2022-10-18 18:39:121933[more testing](https://bugs.chromium.org/p/chromium/issues/detail?id=1371969) on
1934whether there are compelling reasons to prefer base, absl, or std
1935synchronization primitives; for now, use `base/synchronization/`.
1936***
1937
1938### Time library <sup>[banned]</sup>
1939
1940```c++
1941absl::Duration
1942absl::Time
1943absl::TimeZone
1944absl::CivilDay
1945```
1946
1947**Description:** Abstractions for holding time values, both in terms of
1948absolute time and civil time.
1949
1950**Documentation:** [Time](https://abseil.io/docs/cpp/guides/time)
1951
1952**Notes:**
1953*** promo
Peter Kasting72d110f12024-02-06 19:45:261954Overlaps with `base/time/`.
Peter Kasting4f35bfc2022-10-18 18:39:121955***
1956
Joe Masonfe4f2562021-09-15 15:23:131957## Abseil TBD Features {#absl-review}
1958
1959The following Abseil library features are not allowed in the Chromium codebase.
1960See the top of this page on how to propose moving a feature from this list into
1961the allowed or banned sections.
1962
Danil Chapovalova9f27032022-06-20 16:56:141963### AnyInvocable <sup>[tbd]</sup>
1964
1965```c++
1966absl::AnyInvocable
1967```
1968
1969**Description:** An equivalent of the C++23 std::move_only_function.
1970
1971**Documentation:**
1972* [any_invocable.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/any_invocable.h)
1973* [std::move_only_function](https://en.cppreference.com/w/cpp/utility/functional/move_only_function/move_only_function)
1974
1975**Notes:**
1976*** promo
1977Overlaps with `base::RepeatingCallback`, `base::OnceCallback`.
1978***
1979
danakja6f71cb12021-12-15 21:04:491980### Containers <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:131981
1982```c++
1983absl::flat_hash_map
1984absl::flat_hash_set
1985absl::node_hash_map
1986absl::node_hash_set
1987absl::btree_map
1988absl::btree_set
1989absl::btree_multimap
1990absl::btree_multiset
Joe Masonfe4f2562021-09-15 15:23:131991```
1992
1993**Description:** Alternatives to STL containers designed to be more efficient
1994in the general case.
1995
1996**Documentation:**
1997* [Containers](https://abseil.io/docs/cpp/guides/container)
1998* [Hash](https://abseil.io/docs/cpp/guides/hash)
1999
2000**Notes:**
2001*** promo
2002Supplements `base/containers/`.
Adam Riceaf2708e2023-07-20 14:53:162003
2004absl::InlinedVector is explicitly allowed, see the [discussion
2005thread](https://groups.google.com/a/chromium.org/g/cxx/c/jTfqVfU-Ka0/m/caaal90NCgAJ).
2006
Joe Masonfe4f2562021-09-15 15:23:132007***
2008
Mirko Bonadeide812cd2022-12-07 22:38:322009### CRC32C library <sup>[tbd]</sup>
2010
2011**Description:** API for computing CRC32C values as checksums for arbitrary
2012sequences of bytes provided as a string buffer.
2013
2014**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:082015[crc32.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/crc/crc32c.h)
Mirko Bonadeide812cd2022-12-07 22:38:322016
2017**Notes:**
2018*** promo
Peter Kasting72d110f12024-02-06 19:45:262019Overlaps with `third_party/crc32c`.
Mirko Bonadeide812cd2022-12-07 22:38:322020***
2021
Peter Kasting4f35bfc2022-10-18 18:39:122022### Log macros and related classes <sup>[tbd]</sup>
Danil Chapovalov6719fb12022-08-31 13:52:492023
2024```c++
2025LOG(INFO) << message;
2026CHECK(condition);
2027absl::AddLogSink(&custom_sink_to_capture_absl_logs);
2028```
2029
2030**Description:** Macros and related classes to perform debug loggings
2031
2032**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:082033[log.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/log/log.h)
2034[check.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/log/check.h)
Danil Chapovalov6719fb12022-08-31 13:52:492035
2036**Notes:**
2037*** promo
Peter Kasting72d110f12024-02-06 19:45:262038Overlaps with `base/logging.h`.
Danil Chapovalov6719fb12022-08-31 13:52:492039***
Danil Chapovalov79a40942023-06-21 18:35:302040
Mirko Bonadeidc928a162023-11-20 08:24:232041### NoDestructor <sup>[tbd]</sup>
2042
2043```c++
2044// Global or namespace scope.
2045ABSL_CONST_INIT absl::NoDestructor<MyRegistry> reg{"foo", "bar", 8008};
2046
2047// Function scope.
2048const std::string& MyString() {
2049 static const absl::NoDestructor<std::string> x("foo");
2050 return *x;
2051}
2052```
2053
2054**Description:** `absl::NoDestructor<T>` is a wrapper around an object of
2055type T that behaves as an object of type T but never calls T's destructor.
2056
2057**Documentation:**
2058[no_destructor.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/base/no_desctructor.h)
2059
2060**Notes:**
2061*** promo
2062Overlaps with `base::NoDestructor`.
2063***
2064
Danil Chapovalov79a40942023-06-21 18:35:302065### Nullability annotations <sup>[tbd]</sup>
2066
2067```c++
2068void PaySalary(absl::NotNull<Employee *> employee) {
2069 pay(*employee); // OK to dereference
2070}
2071```
2072
2073**Description:** Annotations to more clearly specify contracts
2074
2075**Documentation:**
2076[nullability.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/base/nullability.h)
2077
2078**Notes:**
2079*** promo
2080These nullability annotations are primarily a human readable signal about the
2081intended contract of the pointer. They are not *types* and do not currently
2082provide any correctness guarantees.
2083***
Danil Chapovalov0e8ed132023-09-28 08:05:392084
2085### Overload <sup>[tbd]</sup>
2086
2087```c++
2088std::variant<int, std::string, double> v(int{1});
2089assert(std::visit(absl::Overload(
2090 [](int) -> absl::string_view { return "int"; },
2091 [](const std::string&) -> absl::string_view {
2092 return "string";
2093 },
2094 [](double) -> absl::string_view { return "double"; }),
2095 v) == "int");
2096```
2097
2098**Description:** Returns a functor that provides overloads based on the functors passed to it
2099
2100**Documentation:**
Adam Ricebf82f342023-09-29 15:07:312101[overload.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/overload.h)
Danil Chapovalov0e8ed132023-09-28 08:05:392102
2103**Notes:**
2104*** promo
Peter Kasting72d110f12024-02-06 19:45:262105Overlaps with `base::Overloaded`.
Danil Chapovalov0e8ed132023-09-28 08:05:392106***