blob: a7969b6e4bab954717bc2caf37527567cc26f22f [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_
Peter Kasting813a3f12024-11-21 01:02:5636* **C++23:** _Not yet supported_
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 Kasting5fdcd782025-01-13 14:57:07260### std::aligned_{storage,union} <sup>[banned]</sup>
261
262```c++
263std::aligned_storage<sizeof(T), alignof<T>>::type buf;
264```
265
266**Description:** Creates aligned, uninitialized storage to later hold one or
267more objects.
268
269**Documentation:**
270[`std::aligned_storage`](https://en.cppreference.com/w/cpp/types/aligned_storage)
271
272**Notes:**
273*** promo
274Deprecated in C++23. Generally, use `alignas(T) char buf[sizeof(T)];`. Aligned
275unions can be handled similarly, using the max alignment and size of the union
276members, either passed via a pack or computed inline.
277***
278
Peter Kastinge2c5ee82023-02-15 17:23:08279### std::bind <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13280
281```c++
Peter Kastinge2c5ee82023-02-15 17:23:08282auto x = std::bind(function, args, ...);
Joe Masonfe4f2562021-09-15 15:23:13283```
284
Peter Kastinge2c5ee82023-02-15 17:23:08285**Description:** Declares a function object bound to certain arguments.
Joe Masonfe4f2562021-09-15 15:23:13286
287**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08288[`std::bind`](https://en.cppreference.com/w/cpp/utility/functional/bind)
Joe Masonfe4f2562021-09-15 15:23:13289
290**Notes:**
291*** promo
Peter Kastinge2c5ee82023-02-15 17:23:08292Use `base::Bind` instead. Compared to `std::bind`, `base::Bind` helps prevent
293lifetime issues by preventing binding of capturing lambdas and by forcing
294callers to declare raw pointers as `Unretained`.
Joe Masonfe4f2562021-09-15 15:23:13295
Peter Kastinge2c5ee82023-02-15 17:23:08296[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/SoEj7oIDNuA)
Joe Masonfe4f2562021-09-15 15:23:13297***
298
Peter Kastinge2c5ee82023-02-15 17:23:08299### std::function <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13300
301```c++
Peter Kastinge2c5ee82023-02-15 17:23:08302std::function x = [] { return 10; };
303std::function y = std::bind(foo, args);
Joe Masonfe4f2562021-09-15 15:23:13304```
305
Peter Kastinge2c5ee82023-02-15 17:23:08306**Description:** Wraps a standard polymorphic function.
Joe Masonfe4f2562021-09-15 15:23:13307
308**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08309[`std::function`](https://en.cppreference.com/w/cpp/utility/functional/function)
310
311**Notes:**
312*** promo
Marijn Kruisselbrinke453e56c2023-12-05 22:35:13313Use `base::{Once,Repeating}Callback` or `base::FunctionRef` instead. Compared
314to `std::function`, `base::{Once,Repeating}Callback` directly supports
315Chromium's refcounting classes and weak pointers and deals with additional
316thread safety concerns.
Peter Kastinge2c5ee82023-02-15 17:23:08317
318[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/SoEj7oIDNuA)
319***
320
321### std::shared_ptr <sup>[banned]</sup>
322
323```c++
324std::shared_ptr<int> x = std::make_shared<int>(10);
325```
326
327**Description:** Allows shared ownership of a pointer through reference counts.
328
329**Documentation:**
330[`std::shared_ptr`](https://en.cppreference.com/w/cpp/memory/shared_ptr)
331
332**Notes:**
333*** promo
334Unlike `base::RefCounted`, uses extrinsic rather than intrinsic reference
335counting. Could plausibly be used in Chromium, but would require significant
336migration.
337
338[Google Style Guide](https://google.github.io/styleguide/cppguide.html#Ownership_and_Smart_Pointers),
Peter Kasting72d110f12024-02-06 19:45:26339[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/aT2wsBLKvzI)
Peter Kastinge2c5ee82023-02-15 17:23:08340***
341
342### std::{sto{i,l,ul,ll,ull,f,d,ld},to_string} <sup>[banned]</sup>
343
344```c++
345int x = std::stoi("10");
346```
347
348**Description:** Converts strings to/from numbers.
349
350**Documentation:**
351[`std::stoi`, `std::stol`, `std::stoll`](https://en.cppreference.com/w/cpp/string/basic_string/stol),
352[`std::stoul`, `std::stoull`](https://en.cppreference.com/w/cpp/string/basic_string/stoul),
353[`std::stof`, `std::stod`, `std::stold`](https://en.cppreference.com/w/cpp/string/basic_string/stof),
354[`std::to_string`](https://en.cppreference.com/w/cpp/string/basic_string/to_string)
Joe Masonfe4f2562021-09-15 15:23:13355
356**Notes:**
357*** promo
358The string-to-number conversions rely on exceptions to communicate failure,
359while the number-to-string conversions have performance concerns and depend on
Peter Kastinge2c5ee82023-02-15 17:23:08360the locale. Use `base/strings/string_number_conversions.h` instead.
Joe Masonfe4f2562021-09-15 15:23:13361***
362
Peter Kastinge2c5ee82023-02-15 17:23:08363### std::weak_ptr <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13364
Peter Kastinge2c5ee82023-02-15 17:23:08365```c++
366std::weak_ptr<int> x = my_shared_x;
367```
368
369**Description:** Allows a weak reference to a `std::shared_ptr`.
370
371**Documentation:**
372[`std::weak_ptr`](https://en.cppreference.com/w/cpp/memory/weak_ptr)
373
374**Notes:**
375*** promo
376Banned because `std::shared_ptr` is banned. Use `base::WeakPtr` instead.
Joe Masonfe4f2562021-09-15 15:23:13377***
378
Peter Kastinge2c5ee82023-02-15 17:23:08379### Thread Support Library <sup>[banned]</sup>
380
381```c++
382#include <barrier> // C++20
383#include <condition_variable>
384#include <future>
385#include <latch> // C++20
386#include <mutex>
387#include <semaphore> // C++20
388#include <stop_token> // C++20
389#include <thread>
390```
391
Joe Masonfe4f2562021-09-15 15:23:13392**Description:** Provides a standard multithreading library using `std::thread`
393and associates
394
395**Documentation:**
396[Thread support library](https://en.cppreference.com/w/cpp/thread)
397
398**Notes:**
399*** promo
Peter Kasting72d110f12024-02-06 19:45:26400Overlaps with `base/synchronization`. `base::Thread` is tightly coupled to
401`base::MessageLoop` which would make it hard to replace. We should investigate
402using standard mutexes, or `std::unique_lock`, etc. to replace our
Peter Kastinge2c5ee82023-02-15 17:23:08403locking/synchronization classes.
Joe Masonfe4f2562021-09-15 15:23:13404***
405
Peter Kasting72d110f12024-02-06 19:45:26406## C++17 Banned Language Features {#core-blocklist-17}
Peter Kasting1865f2772021-12-23 21:23:58407
Peter Kasting72d110f12024-02-06 19:45:26408The following C++17 language features are not allowed in the Chromium codebase.
Peter Kasting1865f2772021-12-23 21:23:58409
Peter Kasting72d110f12024-02-06 19:45:26410### UTF-8 character literals <sup>[banned]</sup>
Peter Kasting1865f2772021-12-23 21:23:58411
412```c++
Peter Kasting72d110f12024-02-06 19:45:26413char x = u8'x'; // C++17
414char8_t x = u8'x'; // C++20
Peter Kasting1865f2772021-12-23 21:23:58415```
416
Peter Kasting72d110f12024-02-06 19:45:26417**Description:** A character literal that begins with `u8` is a character
418literal of type `char` (C++17) or `char8_t` (C++20). The value of a UTF-8
419character literal is equal to its ISO 10646 code point value.
Peter Kasting1865f2772021-12-23 21:23:58420
421**Documentation:**
Peter Kasting72d110f12024-02-06 19:45:26422[Character literal](https://en.cppreference.com/w/cpp/language/character_literal)
Peter Kasting1865f2772021-12-23 21:23:58423
424**Notes:**
425*** promo
Peter Kasting72d110f12024-02-06 19:45:26426Banned because `char8_t` is banned. Use an unprefixed character or string
427literal; it should be encoded in the binary as UTF-8 on all supported platforms.
Peter Kasting6d77e9d2023-02-09 21:58:18428***
429
430## C++17 Banned Library Features {#library-blocklist-17}
431
432The following C++17 library features are not allowed in the Chromium codebase.
433
Peter Kasting72d110f12024-02-06 19:45:26434### Mathematical special functions <sup>[banned]</sup>
435
436```c++
437std::assoc_laguerre()
438std::assoc_legendre()
439std::beta()
440std::comp_ellint_1()
441std::comp_ellint_2()
442std::comp_ellint_3()
443std::cyl_bessel_i()
444std::cyl_bessel_j()
445std::cyl_bessel_k()
446std::cyl_neumann()
447std::ellint_1()
448std::ellint_2()
449std::ellint_3()
450std::expint()
451std::hermite()
452std::legendre()
453std::laguerre()
454std::riemann_zeta()
455std::sph_bessel()
456std::sph_legendre()
457std::sph_neumann()
458```
459
460**Description:** A variety of mathematical functions.
461
462**Documentation:**
463[Mathematical special functions](https://en.cppreference.com/w/cpp/numeric/special_functions)
464
465**Notes:**
466*** promo
467Banned due to
468[lack of libc++ support](https://libcxx.llvm.org/Status/Cxx17.html).
469***
470
471### Parallel algorithms <sup>[banned]</sup>
472
473```c++
474auto it = std::find(std::execution::par, std::begin(vec), std::end(vec), 2);
475```
476
477**Description:** Many of the STL algorithms, such as the `copy`, `find` and
478`sort` methods, now support the parallel execution policies: `seq`, `par`, and
479`par_unseq` which translate to "sequentially", "parallel" and
480"parallel unsequenced".
481
482**Documentation:**
483[`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)
484
485**Notes:**
486*** promo
487Banned because
488[libc++ support is incomplete](https://libcxx.llvm.org/Status/PSTL.html) and the
489interaction of its threading implementation with Chrome's is unclear. Prefer to
490explicitly parallelize long-running algorithms using Chrome's threading APIs, so
491the same scheduler controls, shutdown policies, tracing, etc. apply as in any
492other multithreaded code.
493***
494
Peter Kasting6d77e9d2023-02-09 21:58:18495### std::aligned_alloc <sup>[banned]</sup>
496
497```c++
498int* p2 = static_cast<int*>(std::aligned_alloc(1024, 1024));
499```
500
501**Description:** Allocates uninitialized storage with the specified alignment.
502
503**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08504[`std::aligned_alloc`](https://en.cppreference.com/w/cpp/memory/c/aligned_alloc)
Peter Kasting6d77e9d2023-02-09 21:58:18505
506**Notes:**
507*** promo
508[Will be allowed soon](https://crbug.com/1412818); for now, use
509`base::AlignedAlloc`.
510***
511
512### std::any <sup>[banned]</sup>
513
514```c++
515std::any x = 5;
516```
517
518**Description:** A type-safe container for single values of any type.
519
520**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08521[`std::any`](https://en.cppreference.com/w/cpp/utility/any)
Peter Kasting6d77e9d2023-02-09 21:58:18522
523**Notes:**
524*** promo
Peter Kasting72d110f12024-02-06 19:45:26525Banned since workaround for lack of RTTI
526[isn't compatible with the component build](https://crbug.com/1096380). See also
527`absl::any`.
Peter Kasting6d77e9d2023-02-09 21:58:18528
Peter Kasting72d110f12024-02-06 19:45:26529[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/00cpZ07nye4)
530***
531
532### std::byte <sup>[banned]</sup>
533
534```c++
535std::byte b = 0xFF;
536int i = std::to_integer<int>(b); // 0xFF
537```
538
539**Description:** The contents of a single memory unit. `std::byte` has the same
540size and aliasing rules as `unsigned char`, but does not semantically represent
541a character or arithmetic value, and does not expose operators other than
542bitwise ops.
543
544**Documentation:**
545[`std::byte`](https://en.cppreference.com/w/cpp/types/byte)
546
547**Notes:**
548*** promo
549Banned due to low marginal utility in practice, high conversion costs, and
550programmer confusion about "byte" vs. "octet". Use `uint8_t` for the common case
551of "8-bit unsigned value", and `char` for the atypical case of code that works
552with memory without regard to its contents' values or semantics (e.g allocator
553implementations).
554
555[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/bBY0gZa1Otk)
Peter Kasting6d77e9d2023-02-09 21:58:18556***
557
Peter Kasting6d77e9d2023-02-09 21:58:18558### std::filesystem <sup>[banned]</sup>
559
560```c++
561#include <filesystem>
562```
563
564**Description:** A standard way to manipulate files, directories, and paths in a
565filesystem.
566
567**Documentation:**
568[Filesystem library](https://en.cppreference.com/w/cpp/filesystem)
569
570**Notes:**
571*** promo
572Banned by the [Google Style Guide](https://google.github.io/styleguide/cppguide.html#Other_Features).
573***
574
Peter Kasting72d110f12024-02-06 19:45:26575### std::{from,to}_chars <sup>[banned]</sup>
576
577```c++
578std::from_chars(str.data(), str.data() + str.size(), result);
579std::to_chars(str.data(), str.data() + str.size(), 42);
580```
581
582**Description:** Locale-independent, non-allocating, non-throwing functions to
583convert values from/to character strings, designed for use in high-throughput
584contexts.
585
586**Documentation:**
587[`std::from_chars`](https://en.cppreference.com/w/cpp/utility/from_chars)
588[`std::to_chars`](https://en.cppreference.com/w/cpp/utility/to_chars),
589
590**Notes:**
591*** promo
592Overlaps with utilities in `base/strings/string_number_conversions.h`, which are
593easier to use correctly.
594***
595
David Benjamind9d21aa12023-10-02 23:29:57596### std::in_place{_type,_index}[_t] <sup>[banned]</sup>
Avi Drissmanbc6545f42022-05-03 17:47:38597
598```c++
Avi Drissmanbc6545f42022-05-03 17:47:38599std::variant<int, float> v{std::in_place_type<int>, 1.4};
600```
601
David Benjamind9d21aa12023-10-02 23:29:57602**Description:** `std::in_place_type` and `std::in_place_index` are
603disambiguation tags for `std::variant` and `std::any` to indicate that the
604object should be constructed in-place.
Avi Drissmanbc6545f42022-05-03 17:47:38605
606**Documentation:**
David Benjamind9d21aa12023-10-02 23:29:57607[`std::in_place_type`](https://en.cppreference.com/w/cpp/utility/in_place)
Avi Drissmanbc6545f42022-05-03 17:47:38608
609**Notes:**
610*** promo
David Benjamind9d21aa12023-10-02 23:29:57611Banned for now because `std::variant` and `std::any` are banned. Because
612`absl::variant` is used instead, and it requires `absl::in_place_type`, use
613`absl::in_place_type` for non-Abseil Chromium
Peter Kasting72d110f12024-02-06 19:45:26614code.
615
616[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ZspmuJPpv6s)
617***
618
619### std::{pmr::memory_resource,polymorphic_allocator} <sup>[banned]</sup>
620
621```c++
622#include <memory_resource>
623```
624
625**Description:** Manages memory allocations using runtime polymorphism.
626
627**Documentation:**
628[`std::pmr::memory_resource`](https://en.cppreference.com/w/cpp/memory/memory_resource),
629[`std::pmr::polymorphic_allocator`](https://en.cppreference.com/w/cpp/memory/polymorphic_allocator)
630
631**Notes:**
632*** promo
633Banned because Chromium does not customize allocators
634([PartitionAlloc](https://chromium.googlesource.com/chromium/src/+/main/base/allocator/partition_allocator/PartitionAlloc.md)
635is used globally).
636***
637
638### std::timespec_get <sup>[banned]</sup>
639
640```c++
641std::timespec ts;
642std::timespec_get(&ts, TIME_UTC);
643```
644
645**Description:** Gets the current calendar time in the given time base.
646
647**Documentation:**
648[`std::timespec_get`](https://en.cppreference.com/w/cpp/chrono/c/timespec_get)
649
650**Notes:**
651*** promo
652Banned due to unclear, implementation-defined behavior. On POSIX, use
653`base::TimeDelta::ToTimeSpec()`; this could be supported on other platforms if
654desirable.
Avi Drissmanbc6545f42022-05-03 17:47:38655***
656
Peter Kasting6d77e9d2023-02-09 21:58:18657### std::uncaught_exceptions <sup>[banned]</sup>
658
659```c++
660int count = std::uncaught_exceptions();
661```
662
663**Description:** Determines whether there are live exception objects.
664
665**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08666[`std::uncaught_exceptions`](https://en.cppreference.com/w/cpp/error/uncaught_exception)
Peter Kasting6d77e9d2023-02-09 21:58:18667
668**Notes:**
669*** promo
670Banned because exceptions are banned.
671***
672
673### std::variant <sup>[banned]</sup>
674
675```c++
676std::variant<int, double> v = 12;
677```
678
679**Description:** The class template `std::variant` represents a type-safe
680`union`. An instance of `std::variant` at any given time holds a value of one of
681its alternative types (it's also possible for it to be valueless).
682
683**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08684[`std::variant`](https://en.cppreference.com/w/cpp/utility/variant)
Peter Kasting6d77e9d2023-02-09 21:58:18685
686**Notes:**
687*** promo
Peter Kastinge2c5ee82023-02-15 17:23:08688[Will be allowed soon](https://crbug.com/1373620); for now, use `absl::variant`.
Peter Kasting6d77e9d2023-02-09 21:58:18689***
690
691### Transparent std::owner_less <sup>[banned]</sup>
692
693```c++
694std::map<std::weak_ptr<T>, U, std::owner_less<>>
695```
696
697**Description:** Function object providing mixed-type owner-based ordering of
698shared and weak pointers, regardless of the type of the pointee.
699
700**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08701[`std::owner_less`](https://en.cppreference.com/w/cpp/memory/owner_less)
Peter Kasting6d77e9d2023-02-09 21:58:18702
703**Notes:**
704*** promo
705Banned since `std::shared_ptr` and `std::weak_ptr` are banned.
706***
707
708### weak_from_this <sup>[banned]</sup>
709
710```c++
711auto weak_ptr = weak_from_this();
712```
713
714**Description:** Returns a `std::weak_ptr<T>` that tracks ownership of `*this`
715by all existing `std::shared_ptr`s that refer to `*this`.
716
717**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08718[`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:18719
720**Notes:**
721*** promo
722Banned since `std::shared_ptr` and `std::weak_ptr` are banned.
Avi Drissman37b7d512022-04-01 22:01:40723***
724
Arthur Sonzognid8517c92023-03-03 09:41:45725## C++20 Allowed Language Features {#core-allowlist-20}
726
Peter Kastingf86817c2023-11-13 18:00:11727The following C++20 language features are allowed in the Chromium codebase.
728
729### Abbreviated function templates <sup>[allowed]</sup>
730
731```c++
732// template <typename T>
733// void f1(T x);
734void f1(auto x);
735
736// template <C T> // `C` is a concept
737// void f2(T x);
738void f2(C auto x);
739
740// template <typename T, C U> // `C` is a concept
741// void f3(T x, U y);
742template <typename T>
743void f3(T x, C auto y);
744
745// template<typename... Ts>
746// void f4(Ts... xs);
747void f4(auto... xs);
748```
749
750**Description:** Function params of type `auto` become syntactic sugar for
751declaring a template type for each such parameter.
752
753**Documentation:**
754[Abbreviated function template](https://en.cppreference.com/w/cpp/language/function_template#Abbreviated_function_template)
755
756**Notes:**
757*** promo
758[Migration bug](https://crbug.com/1414526)
759***
760
761### consteval <sup>[allowed]</sup>
762
763```c++
764consteval int sqr(int n) { return n * n; }
765constexpr int kHundred = sqr(10); // OK
766constexpr int quad(int n) { return sqr(sqr(n)); } // ERROR, might be runtime
767```
768
769**Description:** Specified that a function may only be used in a compile-time
770context.
771
772**Documentation:**
773[`consteval` specifier](https://en.cppreference.com/w/cpp/language/consteval)
774
775**Notes:**
776*** promo
777None
778***
779
780### Constraints and concepts <sup>[allowed]</sup>
781
782```c++
783// `Hashable` is a concept satisfied by any type `T` for which the expression
784// `std::hash<T>{}(a)` compiles and produces a value convertible to `size_t`.
785template<typename T>
786concept Hashable = requires(T a)
787{
788 { std::hash<T>{}(a) } -> std::convertible_to<size_t>;
789};
790template <Hashable T> // Only instantiable for `T`s that satisfy `Hashable`.
791void f(T) { ... }
792```
793
794**Description:** Allows bundling sets of requirements together as named
795concepts, then enforcing them on template arguments.
796
797**Documentation:**
798[Constraints and concepts](https://en.cppreference.com/w/cpp/language/constraints)
799
800**Notes:**
801*** promo
802[Migration bug](https://crbug.com/1414528)
803***
804
805### Default comparisons <sup>[allowed]</sup>
806
807```c++
Roland Bock5dbdff62024-01-20 09:33:53808class S : public T {
809 // Non-member equality operator with access to private members.
danakj4b9193e2024-02-02 17:31:33810 // Compares `T` bases, then `x`, then `y`, short-circuiting when
811 // it finds inequality.
Roland Bock5dbdff62024-01-20 09:33:53812 friend bool operator==(const S&, const S&) = default;
813
danakj4b9193e2024-02-02 17:31:33814 // Non-member ordering operator with access to private members.
815 // Compares `T` bases, then `x`, then `y`, short-circuiting when
816 // it finds an ordering difference.
817 friend auto operator<=>(const S&, const S&) = default;
818
Peter Kastingf86817c2023-11-13 18:00:11819 int x;
820 bool y;
821};
822```
823
Roland Bock5dbdff62024-01-20 09:33:53824**Description:** Requests that the compiler generate the implementation of
825any comparison operator, including `<=>`. Prefer non-member comparison
826operators. When defaulting `<=>`, also explicitly default `==`. Together these
827are sufficient to allow any comparison as long as callers do not need to take
828the address of any non-declared operator.
Peter Kastingf86817c2023-11-13 18:00:11829
830**Documentation:**
831[Default comparisons](https://en.cppreference.com/w/cpp/language/default_comparisons)
832
833**Notes:**
834*** promo
danakj4b9193e2024-02-02 17:31:33835Unlike constructors/destructors, our compiler extensions do not require these
836to be written out-of-line in the .cc file. Feel free to write `= default`
837directly in the header, as this is much simpler to write.
838
839- [Migration bug](https://crbug.com/1414530)
David Bokanc246fc92024-02-28 17:55:47840- [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/h4lVi2jHU-0/m/X0q_Bh2IAAAJ)
danakj4b9193e2024-02-02 17:31:33841
Peter Kastingf86817c2023-11-13 18:00:11842***
843
Arthur Sonzognid8517c92023-03-03 09:41:45844### Designated initializers <sup>[allowed]</sup>
845
846```c++
847struct S { int x = 1; int y = 2; }
848S s{ .y = 3 }; // OK, s.x == 1, s.y == 3
849```
850
851**Description:** Allows explicit initialization of subsets of aggregate members
852at construction.
853
854**Documentation:**
855[Designated initializers](https://en.cppreference.com/w/cpp/language/aggregate_initialization#Designated_initializers)
856
857**Notes:**
858*** promo
859None
860***
861
Peter Kastingf86817c2023-11-13 18:00:11862### __has_cpp_attribute <sup>[allowed]</sup>
863
864```c++
865#if __has_cpp_attribute(assume) // Toolchain supports C++23 `[[assume]]`.
866...
867#endif
868```
869
870**Description:** Checks whether the toolchain supports a particular standard
871attribute.
872
873**Documentation:**
874[Feature testing](https://en.cppreference.com/w/cpp/feature_test)
875
876**Notes:**
877*** promo
878None
879***
880
881### constinit <sup>[allowed]</sup>
882
883```c++
884constinit int x = 3;
885void foo() {
886 ++x;
887}
888```
889
890**Description:** Ensures that a variable can be compile-time initialized. This
891is like a milder form of `constexpr` that does not force variables to be const
892or have constant destruction.
893
894**Documentation:**
895[`constinit` specifier](https://en.cppreference.com/w/cpp/language/constinit)
896
897**Notes:**
898*** promo
899[Migration bug](https://crbug.com/1414612)
900***
901
902### Initializers for bit-field members <sup>[allowed]</sup>
903
904```c++
905struct S {
906 uint32_t x : 27 = 2;
907};
908```
909
910**Description:** Allows specifying the default initial value of a bit-field
911member, as can already be done for other member types.
912
913**Documentation:**
914[Bit-field](https://en.cppreference.com/w/cpp/language/bit_field)
915
916**Notes:**
917*** promo
918None
919***
920
921### Lambda captures with initializers that are pack expansions <sup>[allowed]</sup>
922
923```c++
924template <typename... Args>
925void foo(Args... args) {
926 const auto l = [...n = args] { (x(n), ...); };
927}
928```
929
930**Description:** Allows initializing a capture with a pack expansion.
931
932**Documentation:**
933[Lambda capture](https://en.cppreference.com/w/cpp/language/lambda#Lambda_capture)
934
935**Notes:**
936*** promo
937None
938***
939
940### Language feature-test macros <sup>[allowed]</sup>
941
942```c++
943#if !defined(__cpp_modules) || (__cpp_modules < 201907L)
944... // Toolchain does not support modules
945#endif
946```
947
948**Description:** Provides a standardized way to test the toolchain's
949implementation of a particular language feature.
950
951**Documentation:**
952[Feature testing](https://en.cppreference.com/w/cpp/feature_test)
953
954**Notes:**
955*** promo
956None
957***
958
Peter Kasting580af31d2024-08-06 02:50:59959### [[likely]], [[unlikely]] <sup>[allowed]</sup>
960
961```c++
962if (n > 0) [[likely]] {
963 return 1;
964}
965```
966
967**Description:** Tells the optimizer that a particular codepath is more or less
968likely than an alternative.
969
970**Documentation:**
971[C++ attribute: `likely`, `unlikely`](https://en.cppreference.com/w/cpp/language/attributes/likely)
972
973**Notes:**
974*** promo
Peter Kasting2c40e492024-10-18 00:27:19975[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/bk9YC5qSDF8)
Peter Kasting580af31d2024-08-06 02:50:59976***
977
Peter Kastingf86817c2023-11-13 18:00:11978### Range-for statements with initializer <sup>[allowed]</sup>
979
980```c++
981T foo();
982...
983for (auto& x : foo().items()) { ... } // UAF before C++23!
984for (T thing = foo(); auto& x : thing.items()) { ... } // OK
985```
986
987**Description:** Like C++17's selection statements with initializer.
988Particularly useful before C++23, since temporaries inside range-expressions are
989not lifetime-extended until the end of the loop before C++23.
990
991**Documentation:**
992[Range-based `for` loop](https://en.cppreference.com/w/cpp/language/range-for)
993
994**Notes:**
995*** promo
996[Migration bug](https://crbug.com/1414531)
997***
998
999### Three-way comparison ("spaceship") operator <sup>[allowed]</sup>
1000
1001```c++
1002// `ordering` is an instance of `std::strong_odering` or `std::partial_ordering`
1003// that describes how `a` and `b` are related.
1004const auto ordering = a <=> b;
1005if (ordering < 0) { ... } // `a` < `b`
1006else if (ordering > 0) { ... } // `a` > `b`
1007else { ... } // `a` == `b`
1008```
1009
1010**Description:** Compares two objects in a fashion similar to `strcmp`. Perhaps
1011most useful when defined as an overload in a class, in which case it can replace
1012definitions of other inequalities. See also "Default comparisons".
1013
1014**Documentation:**
1015[Three-way comparison](https://en.cppreference.com/w/cpp/language/operator_comparison#Three-way_comparison)
1016
1017**Notes:**
1018*** promo
1019[Migration bug](https://crbug.com/1414530)
1020***
1021
Peter Kasting2c40e492024-10-18 00:27:191022### using enum declarations <sup>[allowed]</sup>
1023
1024```c++
1025enum class E { kA = 1 };
1026void f() {
1027 using enum E;
1028 auto a = kA;
1029}
1030```
1031
1032**Description:** Introduces enumerator element names into the current scope.
1033
1034**Documentation:**
1035[`using enum` declaration](https://en.cppreference.com/w/cpp/language/enum#using_enum_declaration)
1036
1037**Notes:**
1038*** promo
1039Usage is subject to the Google Style
1040[guidelines on aliases](https://google.github.io/styleguide/cppguide.html#Aliases).
1041
1042[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/Y0lf-DSOR3A)
1043***
1044
Peter Kastingf86817c2023-11-13 18:00:111045## C++20 Allowed Library Features {#library-allowlist-20}
1046
1047The following C++20 library features are allowed in the Chromium codebase.
1048
1049### &lt;bit&gt; <sup>[allowed]</sup>
1050
1051```c++
1052#include <bit>
1053```
1054
1055**Description:** Provides various byte- and bit-twiddling functions, e.g.
1056counting leading zeros.
1057
1058**Documentation:**
1059[Standard library header `<bit>`](https://en.cppreference.com/w/cpp/header/bit)
1060
1061**Notes:**
1062*** promo
1063[Migration bug](https://crbug.com/1414634)
1064***
1065
1066### &lt;compare&gt; <sup>[allowed]</sup>
1067
1068```c++
1069#include <compare>
1070```
1071
1072**Description:** Concepts and classes used to implement three-way comparison
1073("spaceship", `<=>`) support.
1074
1075**Documentation:**
1076[Standard library header `<compare>`](https://en.cppreference.com/w/cpp/header/compare)
1077
1078**Notes:**
1079*** promo
1080None
1081***
1082
1083### &lt;concepts&gt; <sup>[allowed]</sup>
1084
1085```c++
1086#include <concepts>
1087```
1088
1089**Description:** Various useful concepts, many of which replace pre-concept
1090machinery in `<type_traits>`.
1091
1092**Documentation:**
1093[Standard library header `<concepts>`](https://en.cppreference.com/w/cpp/header/concepts)
1094
1095**Notes:**
1096*** promo
1097None
1098***
1099
Daniel Cheng89719222024-07-04 04:59:291100### Range algorithms <sup>[allowed]</sup>
1101
1102```c++
1103constexpr int kArr[] = {2, 4, 6, 8, 10, 12};
1104constexpr auto is_even = [] (auto x) { return x % 2 == 0; };
1105static_assert(std::ranges::all_of(kArr, is_even));
1106```
1107
1108**Description:** Provides versions of most algorithms that accept either an
1109iterator-sentinel pair or a single range argument.
1110
1111**Documentation:**
1112[Ranges algorithms](https://en.cppreference.com/w/cpp/algorithm/ranges)
1113
1114**Notes:**
1115*** promo
1116Supersedes `//base`'s backports in `//base/ranges/algorithm.h`.
1117
1118[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ZnIbkfJ0Glw)
1119***
1120
1121### Range access, range primitives, dangling iterator handling, and range concepts <sup>[allowed]</sup>
1122
1123```c++
1124// Range access:
1125constexpr int kArr[] = {2, 4, 6, 8, 10, 12};
1126static_assert(std::ranges::size(kArr) == 6);
1127
1128// Range primitives:
1129static_assert(
1130 std::same_as<std::ranges::iterator_t<decltype(kArr)>, const int*>);
1131
1132// Range concepts:
1133static_assert(std::ranges::contiguous_range<decltype(kArr)>);
1134```
1135
1136**Description:** Various helper functions and types for working with ranges.
1137
1138**Documentation:**
1139[Ranges library](https://en.cppreference.com/w/cpp/ranges)
1140
1141**Notes:**
1142*** promo
1143Supersedes `//base`'s backports in `//base//ranges/ranges.h`.
1144
1145[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ZnIbkfJ0Glw)
1146***
1147
Peter Kastingf86817c2023-11-13 18:00:111148### Library feature-test macros and &lt;version&gt; <sup>[allowed]</sup>
1149
1150```c++
1151#if !defined(__cpp_lib_atomic_value_initialization) || \
1152 (__cpp_lib_atomic_value_initialization < 201911L)
1153... // `std::atomic` is not value-initialized by default.
1154#endif
1155```
1156
1157**Description:** Provides a standardized way to test the toolchain's
1158implementation of a particular library feature.
1159
1160**Documentation:**
1161[Feature testing](https://en.cppreference.com/w/cpp/feature_test)
1162
1163**Notes:**
1164*** promo
1165None
1166***
1167
1168### &lt;numbers&gt; <sup>[allowed]</sup>
1169
1170```c++
1171#include <numbers>
1172```
1173
1174**Description:** Provides compile-time constants for many common mathematical
1175values, e.g. pi and e.
1176
1177**Documentation:**
1178[Mathematical constants](https://en.cppreference.com/w/cpp/numeric/constants)
1179
1180**Notes:**
1181*** promo
1182[Migration bug](https://crbug.com/1414635)
1183***
1184
1185### std::assume_aligned <sup>[allowed]</sup>
1186
1187```c++
1188void f(int* p) {
1189 int* aligned = std::assume_aligned<256>(p);
1190 ...
1191```
1192
1193**Description:** Informs the compiler that a pointer points to an address
1194aligned to at least some particular power of 2.
1195
1196**Documentation:**
1197[`std::assume_aligned`](https://en.cppreference.com/w/cpp/memory/assume_aligned)
1198
1199**Notes:**
1200*** promo
Peter Kastingb2887c32024-01-08 21:41:041201None
Peter Kastingf86817c2023-11-13 18:00:111202***
1203
1204### std::erase[_if] for containers <sup>[allowed]</sup>
1205
1206```c++
1207std::vector<int> numbers = ...;
1208std::erase_if(numbers, [](int x) { return x % 2 == 0; });
1209```
1210
1211**Description:** Erases from a container by value comparison or predicate,
1212avoiding the need to use the `erase(remove(...` paradigm.
1213
1214**Documentation:**
1215[`std::erase`, `std::erase_if` (`std::vector`)](https://en.cppreference.com/w/cpp/container/vector/erase2)
1216
1217**Notes:**
1218*** promo
1219[Migration bug](https://crbug.com/1414639)
1220***
1221
Peter Kastingfc838e82024-09-04 13:56:461222### std::hardware_{con,de}structive_interference_size <sup>[allowed]</sup>
1223
1224```c++
1225struct SharedData {
1226 ReadOnlyFrequentlyUsed data;
1227 alignas(std::hardware_destructive_interference_size) std::atomic<size_t> counter;
1228};
1229```
1230
1231**Description:** The `std::hardware_destructive_interference_size` constant is
1232useful to avoid false sharing (destructive interference) between variables that
1233would otherwise occupy the same cacheline. In contrast,
1234`std::hardware_constructive_interference_size` is helpful to promote true
1235sharing (constructive interference), e.g. to support better locality for
1236non-contended data.
1237
1238**Documentation:**
1239[`std::hardware_destructive_interference_size`](https://en.cppreference.com/w/cpp/thread/hardware_destructive_interference_size),
1240[`std::hardware_constructive_interference_size`](https://en.cppreference.com/w/cpp/thread/hardware_destructive_interference_size)
1241
1242**Notes:**
1243*** promo
1244[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/cwktrFxxUY4)
1245***
1246
Peter Kastingf86817c2023-11-13 18:00:111247### std::is_[un]bounded_array <sup>[allowed]</sup>
1248
1249```c++
1250template <typename T>
1251static constexpr bool kBoundedArray = std::is_bounded_array_v<T>;
1252```
1253
1254**Description:** Checks if a type is an array type with a known or unknown
1255bound.
1256
1257**Documentation:**
1258[`std::is_bounded_array`](https://en.cppreference.com/w/cpp/types/is_bounded_array),
1259[`std::is_unbounded_array`](https://en.cppreference.com/w/cpp/types/is_unbounded_array)
1260
1261**Notes:**
1262*** promo
1263None
1264***
1265
1266### std::lerp <sup>[allowed]</sup>
1267
1268```c++
1269double val = std::lerp(start, end, t);
1270```
1271
1272**Description:** Linearly interpolates (or extrapolates) between two values.
1273
1274**Documentation:**
1275[`std::lerp`](https://en.cppreference.com/w/cpp/numeric/lerp)
1276
1277**Notes:**
1278*** promo
1279[Migration bug](https://crbug.com/1414537)
1280***
1281
1282### std::make_obj_using_allocator etc. <sup>[allowed]</sup>
1283
1284```c++
1285auto obj = std::make_obj_using_allocator<Obj>(alloc, ...);
1286```
1287
1288**Description:** Constructs an object using
1289[uses-allocator construction](https://en.cppreference.com/w/cpp/memory/uses_allocator).
1290
1291**Documentation:**
1292[`std::make_obj_using_allocator`](https://en.cppreference.com/w/cpp/memory/make_obj_using_allocator)
1293
1294**Notes:**
1295*** promo
1296None
1297***
1298
1299### std::make_unique_for_overwrite <sup>[allowed]</sup>
1300
1301```c++
1302auto ptr = std::make_unique_for_overwrite<int>(); // `*ptr` is uninitialized
1303```
1304
1305**Description:** Like calling `std::unique_ptr<T>(new T)` instead of the more
1306typical `std::unique_ptr<T>(new T(...))`.
1307
1308**Documentation:**
1309[`std::make_unique`, `std::make_unique_for_overwrite`](https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique)
1310
1311**Notes:**
1312*** promo
1313None
1314***
1315
1316### std::midpoint <sup>[allowed]</sup>
1317
1318```c++
1319int center = std::midpoint(top, bottom);
1320```
1321
1322**Description:** Finds the midpoint between its two arguments, avoiding any
1323possible overflow. For integral inputs, rounds towards the first argument.
1324
1325**Documentation:**
1326[`std::midpoint`](https://en.cppreference.com/w/cpp/numeric/midpoint)
1327
1328**Notes:**
1329*** promo
1330[Migration bug](https://crbug.com/1414539)
1331***
1332
Peter Kasting8e2424c2024-10-22 16:21:551333### std::ranges::subrange <sup>[allowed]</sup>
1334
1335```c++
1336void transform(const std::multimap<int, char>& map, int key) {
1337 auto [first, last] = map.equal_range(key);
1338 for (const auto& [_, value] : std::ranges::subrange(first, last)) {
1339 ...
1340```
1341
1342**Description:** Creates a view from an iterator and a sentinel. Useful for
1343treating non-contiguous storage (e.g. a `std::map`) as a range.
1344
1345**Documentation:**
1346[`std::ranges::subrange`](https://en.cppreference.com/w/cpp/ranges/subrange)
1347
1348**Notes:**
1349*** promo
1350Prefer `base::span` if working with explicitly contiguous data, such as in a
1351`std::vector`. Use `std::ranges::subrange` when data is non-contiguous, or when
1352it's an implementation detail that the data is contiguous (e.g.
1353`base::flat_map`).
1354
1355[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/5VeU5GkPUYI)
1356***
1357
Peter Kastingf86817c2023-11-13 18:00:111358### std::remove_cvref[_t] <sup>[allowed]</sup>
1359
1360```c++
1361template <typename T,
1362 typename = std::enable_if_t<std::is_same_v<std::remove_cvref_t<T>,
1363 int>>>
1364void foo(T t);
1365```
1366
1367**Description:** Provides a way to remove const, volatile, and reference
1368qualifiers from a type.
1369
1370**Documentation:**
1371[`std::remove_cvref`](https://en.cppreference.com/w/cpp/types/remove_cvref)
1372
1373**Notes:**
1374*** promo
Peter Kastingb2887c32024-01-08 21:41:041375None
Peter Kastingf86817c2023-11-13 18:00:111376***
1377
1378### std::ssize <sup>[allowed]</sup>
1379
1380```c++
1381str.replace(it, it + std::ssize(substr), 1, 'x');
1382```
1383
1384**Description:** Returns the size of an object as a signed type.
1385
1386**Documentation:**
1387[`std::size`, `std::ssize`](https://en.cppreference.com/w/cpp/iterator/size)
1388
1389**Notes:**
1390*** promo
1391[Migration bug](https://crbug.com/1414543)
1392***
1393
1394### std::string::(starts,ends)_with <sup>[allowed]</sup>
1395
1396```c++
1397const std::string str = "Foo bar";
1398const bool is_true = str.ends_with("bar");
1399```
1400
1401**Description:** Tests whether a string starts or ends with a particular
1402character or string.
1403
1404**Documentation:**
1405[`std::basic_string<CharT,Traits,Allocator>::starts_with`](https://en.cppreference.com/w/cpp/string/basic_string/starts_with),
1406[`std::basic_string<CharT,Traits,Allocator>::ends_with`](https://en.cppreference.com/w/cpp/string/basic_string/ends_with)
1407
1408**Notes:**
1409*** promo
1410[Migration bug](https://crbug.com/1414647)
1411***
1412
Peter Kastingf86817c2023-11-13 18:00:111413## C++20 Banned Language Features {#core-blocklist-20}
1414
1415The following C++20 language features are not allowed in the Chromium codebase.
1416
Peter Kasting72d110f12024-02-06 19:45:261417### char8_t <sup>[banned]</sup>
1418
1419```c++
1420char8_t c = u8'x';
1421```
1422
1423**Description:** A single UTF-8 code unit. Similar to `unsigned char`, but
1424considered a distinct type.
1425
1426**Documentation:**
1427[Fundamental types](https://en.cppreference.com/w/cpp/language/types#char8_t)
1428
1429**Notes:**
1430*** promo
1431Use `char` and unprefixed character literals. Non-UTF-8 encodings are rare
1432enough in Chromium that the value of distinguishing them at the type level is
1433low, and `char8_t*` is not interconvertible with `char*` (what ~all Chromium,
1434STL, and platform-specific APIs use), so using `u8` prefixes would obligate us
1435to insert casts everywhere. If you want to declare at a type level that a block
1436of data is string-like and not an arbitrary binary blob, prefer
1437`std::string[_view]` over `char*`.
1438***
1439
Peter Kastingf86817c2023-11-13 18:00:111440### Modules <sup>[banned]</sup>
1441
1442```c++
1443export module helloworld; // module declaration
1444
1445import <iostream>; // import declaration
1446
1447export void hello() { // export declaration
1448 std::cout << "Hello world!\n";
1449}
1450```
1451
1452**Description:** Modules provide an alternative to many uses of headers which
1453allows for faster compilation, better tooling support, and reduction of problems
1454like "include what you use".
1455
1456**Documentation:**
1457[Modules](https://en.cppreference.com/w/cpp/language/modules)
1458
1459**Notes:**
1460*** promo
1461Not yet sufficiently supported in Clang and GN. Re-evaluate when support
1462improves.
1463***
1464
Peter Kasting8bc046d22023-11-14 00:38:031465### [[no_unique_address]] <sup>[banned]</sup>
1466
1467```c++
1468struct Empty {};
1469struct X {
1470 int i;
1471 [[no_unique_address]] Empty e;
1472};
1473```
1474
1475**Description:** Allows a data member to be overlapped with other members.
1476
1477**Documentation:**
1478[C++ attribute: `no_unique_address`](https://en.cppreference.com/w/cpp/language/attributes/no_unique_address)
1479
1480**Notes:**
1481*** promo
1482Has no effect on Windows, for compatibility with Microsoft's ABI. Use
1483`NO_UNIQUE_ADDRESS` from `base/compiler_specific.h` instead. Do not use (either
1484form) on members of unions due to
1485[potential memory safety problems](https://github.com/llvm/llvm-project/issues/60711).
Peter Kasting8bc046d22023-11-14 00:38:031486***
1487
Peter Kastingf86817c2023-11-13 18:00:111488## C++20 Banned Library Features {#library-blocklist-20}
1489
1490The following C++20 library features are not allowed in the Chromium codebase.
1491
1492### std::atomic_ref <sup>[banned]</sup>
1493
1494```c++
1495struct S { int a; int b; };
1496S not_atomic;
1497std::atomic_ref<S> is_atomic(not_atomic);
1498```
1499
1500**Description:** Allows atomic access to objects that might not themselves be
1501atomic types. While any atomic_ref to an object exists, the object must be
1502accessed exclusively through atomic_ref instances.
1503
1504**Documentation:**
1505[`std::atomic_ref`](https://en.cppreference.com/w/cpp/atomic/atomic_ref)
1506
1507**Notes:**
1508*** promo
1509Banned due to being [unimplemented in libc++](https://reviews.llvm.org/D72240).
1510
1511[Migration bug](https://crbug.com/1422701) (once this is allowed)
1512***
1513
1514### std::bind_front <sup>[banned]</sup>
1515
1516```c++
1517int minus(int a, int b);
1518auto fifty_minus_x = std::bind_front(minus, 50);
1519int forty = fifty_minus_x(10);
1520```
1521
1522**Description:** An updated version of `std::bind` with fewer gotchas, similar
1523to `absl::bind_front`.
1524
1525**Documentation:**
1526[`std::bind_front`, `std::bind_back`](https://en.cppreference.com/w/cpp/utility/functional/bind_front)
1527
1528**Notes:**
1529*** promo
1530Overlaps with `base::Bind`.
1531***
1532
Avi Drissman70cb7f72023-12-12 17:44:371533### std::bit_cast <sup>[banned]</sup>
1534
1535```c++
1536float quake_rsqrt(float number) {
1537 long i = std::bit_cast<long>(number);
1538 i = 0x5f3759df - (i >> 1); // wtf?
1539 float y = std::bit_cast<float>(i);
1540 return y * (1.5f - (0.5f * number * y * y));
1541}
1542```
1543
1544**Description:** Returns an value constructed with the same bits as an value of
1545a different type.
1546
1547**Documentation:**
1548[`std::bit_cast`](https://en.cppreference.com/w/cpp/numeric/bit_cast)
1549
1550**Notes:**
1551*** promo
1552The `std::` version of `bit_cast` allows casting of pointer and reference types,
1553which is both useless in that it doesn't avoid UB, and dangerous in that it
1554allows arbitrary casting away of modifiers like `const`. Instead of using
1555`bit_cast` on pointers, use standard C++ casts. For use on values, use
1556`base::bit_cast` which does not allow this unwanted usage.
1557***
1558
Peter Kastingf86817c2023-11-13 18:00:111559### std::{c8rtomb,mbrtoc8} <sup>[banned]</sup>
1560
1561```c++
1562std::u8string_view strv = u8"zß水🍌";
1563std::mbstate_t state;
1564char out[MB_LEN_MAX] = {0};
1565for (char8_t c : strv) {
1566 size_t rc = std::c8rtomb(out, c, &state);
1567 ...
1568```
1569
1570**Description:** Converts a code point between UTF-8 and a multibyte character
1571encoded using the current C locale.
1572
1573**Documentation:**
1574[`std::c8rtomb`](https://en.cppreference.com/w/cpp/string/multibyte/c8rtomb),
1575[`std::mbrtoc8`](https://en.cppreference.com/w/cpp/string/multibyte/mbrtoc8)
1576
1577**Notes:**
1578*** promo
1579Chromium functionality should not vary with the C locale.
1580***
1581
Peter Kasting8e2424c2024-10-22 16:21:551582### Range factories and range adaptors <sup>[banned]</sup>
Daniel Cheng89719222024-07-04 04:59:291583
1584```c++
Daniel Cheng89719222024-07-04 04:59:291585// Prints 1, 2, 3, 4, 5, 6.
1586for (auto i : std::ranges::iota_view(1, 7)) {
1587 std::cout << i << '\n';
1588}
Peter Kasting8e2424c2024-10-22 16:21:551589
1590constexpr int kArr[] = {6, 2, 8, 4, 4, 2};
1591constexpr auto plus_one = std::views::transform([](int n){ return n + 1; });
1592static_assert(std::ranges::equal(kArr | plus_one, {7, 3, 9, 5, 5, 3}));
Daniel Cheng89719222024-07-04 04:59:291593```
1594
1595**Description:** Lightweight objects that represent iterable sequences.
1596Provides facilities for lazy operations on ranges, along with composition into
1597pipelines.
1598
1599**Documentation:**
1600[Ranges library](https://en.cppreference.com/w/cpp/ranges)
1601
1602**Notes:**
1603*** promo
1604Banned in Chrome due to questions about the design, impact on build time, and
1605runtime performance.
1606
1607[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ZnIbkfJ0Glw)
1608***
1609
Peter Kasting8e2424c2024-10-22 16:21:551610### std::ranges::view_interface <sup>[banned]</sup>
1611
1612```c++
1613class MyView : public std::ranges::view_interface<MyView> { ... };
1614```
1615
1616**Description:** CRTP base class for implementing custom view objects.
1617
1618**Documentation:**
1619[`std::ranges::view_interface`](https://en.cppreference.com/w/cpp/ranges/view_interface)
1620
1621**Notes:**
1622*** promo
1623Banned in Chrome since range factories and adapters are banned, and this would
1624primarily allow authors to create similar functionality.
1625
1626[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ZnIbkfJ0Glw)
1627***
1628
Peter Kastinge73b89d2024-11-26 19:35:521629### &lt;span&gt; <sup>[banned]</sup>
1630
1631```c++
1632#include <span>
1633```
1634
1635**Description:** Utilities for non-owning views over a sequence of objects.
1636
1637**Documentation:**
1638[](https://en.cppreference.com/w/cpp/header/span)
1639
1640**Notes:**
1641*** promo
1642Superseded by `base::span`, which has a richer functionality set.
1643***
1644
Nick Diego Yamanee522ae82024-02-27 04:23:221645### std::to_address <sup>[banned]</sup>
1646
1647```c++
1648std::vector<int> numbers;
1649int* i = std::to_address(numbers.begin());
1650```
1651
1652**Description:** Converts a pointer-like object to a pointer, even if the
1653pointer does not refer to a constructed object (in which case an expression like
1654`&*p` is UB).
1655
1656**Documentation:**
1657[`std::to_address`](https://en.cppreference.com/w/cpp/memory/to_address)
1658
1659**Notes:**
1660*** promo
1661Banned because it is not guaranteed to be SFINAE-compatible. Use
1662base::to_address, which does guarantee this.
1663***
1664
Peter Kastingf86817c2023-11-13 18:00:111665### &lt;syncstream&gt; <sup>[banned]</sup>
1666
1667```c++
1668#include <syncstream>
1669```
1670
1671**Description:** Facilities for multithreaded access to streams.
1672
1673**Documentation:**
1674[Standard library header `<syncstream>`](https://en.cppreference.com/w/cpp/header/syncstream)
1675
1676**Notes:**
1677*** promo
1678Banned due to being unimplemented per
1679[the libc++ C++20 status page](https://libcxx.llvm.org/Status/Cxx20.html).
1680Reevaluate usefulness once implemented.
1681***
1682
1683## C++20 TBD Language Features {#core-review-20}
1684
1685The following C++20 language features are not allowed in the Chromium codebase.
1686See the top of this page on how to propose moving a feature from this list into
1687the allowed or banned sections.
1688
1689### Aggregate initialization using parentheses <sup>[tbd]</sup>
1690
1691```c++
1692struct B {
1693 int a;
1694 int&& r;
1695} b2(1, 1); // Warning: dangling reference
1696```
1697
1698**Description:** Allows initialization of aggregates using parentheses, not just
1699braces.
1700
1701**Documentation:**
1702[Aggregate initialization](https://en.cppreference.com/w/cpp/language/aggregate_initialization),
1703[Direct initialization](https://en.cppreference.com/w/cpp/language/direct_initialization)
1704
1705**Notes:**
1706*** promo
1707There are subtle but important differences between brace- and paren-init of
1708aggregates. The parenthesis style appears to have more pitfalls (allowing
1709narrowing conversions, not extending lifetimes of temporaries bound to
1710references).
1711***
1712
Peter Kastingf86817c2023-11-13 18:00:111713### Coroutines <sup>[tbd]</sup>
1714
1715```c++
1716co_return 1;
1717```
1718
1719**Description:** Allows writing functions that logically block while physically
1720returning control to a caller. This enables writing some kinds of async code in
1721simple, straight-line ways without storing state in members or binding
1722callbacks.
1723
1724**Documentation:**
1725[Coroutines](https://en.cppreference.com/w/cpp/language/coroutines)
1726
1727**Notes:**
1728*** promo
1729Requires significant support code and planning around API and migration.
1730
1731[Prototyping bug](https://crbug.com/1403840)
1732***
1733
Peter Kastingf86817c2023-11-13 18:00:111734## C++20 TBD Library Features {#library-review-20}
1735
1736The following C++20 library features are not allowed in the Chromium codebase.
1737See the top of this page on how to propose moving a feature from this list into
1738the allowed or banned sections.
1739
1740### &lt;coroutine&gt; <sup>[tbd]</sup>
1741
1742```c++
1743#include <coroutine>
1744```
1745
1746**Description:** Header which defines various core coroutine types.
1747
1748**Documentation:**
1749[Coroutine support](https://en.cppreference.com/w/cpp/coroutine)
1750
1751**Notes:**
1752*** promo
1753See notes on "Coroutines" above.
1754***
1755
1756### &lt;format&gt; <sup>[tbd]</sup>
1757
1758```c++
1759std::cout << std::format("Hello {}!\n", "world");
1760```
1761
1762**Description:** Utilities for producing formatted strings.
1763
1764**Documentation:**
1765[Formatting library](https://en.cppreference.com/w/cpp/utility/format)
1766
1767**Notes:**
1768*** promo
1769Has both pros and cons compared to `absl::StrFormat` (which we don't yet use).
1770Migration would be nontrivial.
1771***
1772
Peter Kastingf86817c2023-11-13 18:00:111773### &lt;source_location&gt; <sup>[tbd]</sup>
1774
1775```c++
1776#include <source_location>
1777```
1778
1779**Description:** Provides a class that can hold source code details such as
1780filenames, function names, and line numbers.
1781
1782**Documentation:**
1783[Standard library header `<source_location>`](https://en.cppreference.com/w/cpp/header/source_location)
1784
1785**Notes:**
1786*** promo
1787Seems to regress code size vs. `base::Location`.
1788***
1789
Peter Kastingf86817c2023-11-13 18:00:111790### std::u8string <sup>[tbd]</sup>
1791
1792```c++
1793std::u8string str = u8"Foo";
1794```
1795
1796**Description:** A string whose character type is `char8_t`, intended to hold
1797UTF-8-encoded text.
1798
1799**Documentation:**
1800[`std::basic_string`](https://en.cppreference.com/w/cpp/string/basic_string)
1801
1802**Notes:**
1803*** promo
1804See notes on `char8_t` above.
1805***
1806
Joe Masonfe4f2562021-09-15 15:23:131807## Abseil Banned Library Features {#absl-blocklist}
1808
1809The following Abseil library features are not allowed in the Chromium codebase.
1810
danakja6f71cb12021-12-15 21:04:491811### Any <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:131812
1813```c++
1814absl::any a = int{5};
1815EXPECT_THAT(absl::any_cast<int>(&a), Pointee(5));
1816EXPECT_EQ(absl::any_cast<size_t>(&a), nullptr);
1817```
1818
1819**Description:** Early adaptation of C++17 `std::any`.
1820
1821**Documentation:** [std::any](https://en.cppreference.com/w/cpp/utility/any)
1822
1823**Notes:**
1824*** promo
Peter Kasting72d110f12024-02-06 19:45:261825Banned since workaround for lack of RTTI
1826[isn't compatible with the component build](https://crbug.com/1096380). See also
1827`std::any`.
Joe Masonfe4f2562021-09-15 15:23:131828***
1829
Peter Kasting4b18d0c2024-09-18 00:56:111830### Attributes <sup>[banned]</sup>
1831
1832```c++
1833T* data() ABSL_ATTRIBUTE_LIFETIME_BOUND { return data_; }
1834ABSL_ATTRIBUTE_NO_TAIL_CALL ReturnType Loop();
1835struct S { bool b; int32_t i; } ABSL_ATTRIBUTE_PACKED;
1836```
1837
1838**Description:** Cross-platform macros to expose compiler-specific
1839functionality.
1840
1841**Documentation:** [attributes.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/base/attributes.h)
1842
1843**Notes:**
1844*** promo
1845Long names discourage use. Use standardized attributes over macros where
1846possible, and otherwise prefer shorter alternatives in
1847`base/compiler_specific.h`.
1848
1849[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/lVQOJTng1RU)
1850***
1851
Peter Kasting4f35bfc2022-10-18 18:39:121852### bind_front <sup>[banned]</sup>
1853
1854```c++
1855absl::bind_front
1856```
1857
Peter Kasting3b77a0c2024-08-22 00:22:261858**Description:** Binds the first N arguments of an invocable object and stores
1859them by value.
Peter Kasting4f35bfc2022-10-18 18:39:121860
1861**Documentation:**
1862* [bind_front.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/bind_front.h)
1863* [Avoid std::bind](https://abseil.io/tips/108)
1864
1865**Notes:**
1866*** promo
Peter Kasting72d110f12024-02-06 19:45:261867Overlaps with `base::Bind`.
Peter Kasting4f35bfc2022-10-18 18:39:121868***
1869
danakja6f71cb12021-12-15 21:04:491870### Command line flags <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:131871
1872```c++
1873ABSL_FLAG(bool, logs, false, "print logs to stderr");
1874app --logs=true;
1875```
1876
1877**Description:** Allows programmatic access to flag values passed on the
1878command-line to binaries.
1879
1880**Documentation:** [Flags Library](https://abseil.io/docs/cpp/guides/flags)
1881
1882**Notes:**
1883*** promo
Peter Kasting72d110f12024-02-06 19:45:261884Banned since workaround for lack of RTTI
1885[isn't compatible with the component build](https://crbug.com/1096380). Use
1886`base::CommandLine` instead.
Joe Masonfe4f2562021-09-15 15:23:131887***
1888
Peter Kasting4f35bfc2022-10-18 18:39:121889### Container utilities <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:131890
1891```c++
Peter Kasting4f35bfc2022-10-18 18:39:121892auto it = absl::c_find(container, value);
Joe Masonfe4f2562021-09-15 15:23:131893```
1894
Peter Kasting4f35bfc2022-10-18 18:39:121895**Description:** Container-based versions of algorithmic functions within C++
1896standard library.
Joe Masonfe4f2562021-09-15 15:23:131897
Peter Kasting4f35bfc2022-10-18 18:39:121898**Documentation:**
1899[container.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/algorithm/container.h)
Joe Masonfe4f2562021-09-15 15:23:131900
1901**Notes:**
1902*** promo
Peter Kasting72d110f12024-02-06 19:45:261903Overlaps with `base/ranges/algorithm.h`.
Joe Masonfe4f2562021-09-15 15:23:131904***
1905
Peter Kasting431239a2023-09-29 03:11:441906### FixedArray <sup>[banned]</sup>
1907
1908```c++
1909absl::FixedArray<MyObj> objs_;
1910```
1911
1912**Description:** A fixed size array like `std::array`, but with size determined
1913at runtime instead of compile time.
1914
1915**Documentation:**
1916[fixed_array.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/container/fixed_array.h)
1917
1918**Notes:**
1919*** promo
1920Direct construction is banned due to the risk of UB with uninitialized
1921trivially-default-constructible types. Instead use `base/types/fixed_array.h`,
1922which is a light-weight wrapper that deletes the problematic constructor.
Joe Mason6a6f2582024-01-30 20:26:431923***
Peter Kasting431239a2023-09-29 03:11:441924
Daniel Cheng2248b332022-07-27 06:16:591925### FunctionRef <sup>[banned]</sup>
1926
1927```c++
1928absl::FunctionRef
1929```
1930
1931**Description:** Type for holding a non-owning reference to an object of any
1932invocable type.
1933
1934**Documentation:**
1935[function_ref.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/function_ref.h)
1936
1937**Notes:**
1938*** promo
1939- `absl::FunctionRef` is banned due to allowing implicit conversions between
1940 function signatures in potentially surprising ways. For example, a callable
1941 with the signature `int()` will bind to `absl::FunctionRef<void()>`: the
1942 return value from the callable will be silently discarded.
1943- In Chromium, use `base::FunctionRef` instead.
1944- Unlike `base::OnceCallback` and `base::RepeatingCallback`, `base::FunctionRef`
1945 supports capturing lambdas.
1946- Useful when passing an invocable object to a function that synchronously calls
1947 the invocable object, e.g. `ForEachFrame(base::FunctionRef<void(Frame&)>)`.
1948 This can often result in clearer code than code that is templated to accept
1949 lambdas, e.g. with `template <typename Invocable> void
1950 ForEachFrame(Invocable invocable)`, it is much less obvious what arguments
1951 will be passed to `invocable`.
1952- For now, `base::OnceCallback` and `base::RepeatingCallback` intentionally
1953 disallow conversions to `base::FunctionRef`, under the theory that the
1954 callback should be a capturing lambda instead. Attempting to use this
1955 conversion will trigger a `static_assert` requesting additional feedback for
1956 use cases where this conversion would be valuable.
1957- *Important:* `base::FunctionRef` must not outlive the function call. Like
Helmut Januschka1dce9dc2024-06-11 13:05:351958 `std::string_view`, `base::FunctionRef` is a *non-owning* reference. Using a
Daniel Cheng2248b332022-07-27 06:16:591959 `base::FunctionRef` as a return value or class field is dangerous and likely
1960 to result in lifetime bugs.
Peter Kasting72d110f12024-02-06 19:45:261961
1962[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/JVN4E4IIYA0)
Daniel Cheng2248b332022-07-27 06:16:591963***
1964
Peter Kasting3b77a0c2024-08-22 00:22:261965### Optional <sup>[banned]</sup>
1966
1967```c++
1968absl::optional<int> Func(bool b) {
1969 return b ? absl::make_optional(1) : abl::nullopt;
1970}
1971```
1972
1973**Description:** Early adaptation of C++17 `std::optional`.
1974
1975**Documentation:** [std::optional](https://en.cppreference.com/w/cpp/utility/optional)
1976
1977**Notes:**
1978*** promo
1979Superseded by `std::optional`. Use `std::optional` instead.
1980***
1981
Peter Kasting4f35bfc2022-10-18 18:39:121982### Random <sup>[banned]</sup>
1983
1984```c++
1985absl::BitGen bitgen;
1986size_t index = absl::Uniform(bitgen, 0u, elems.size());
1987```
1988
1989**Description:** Functions and utilities for generating pseudorandom data.
1990
1991**Documentation:** [Random library](https://abseil.io/docs/cpp/guides/random)
1992
1993**Notes:**
1994*** promo
1995Banned because most uses of random values in Chromium should be using a
1996cryptographically secure generator. Use `base/rand_util.h` instead.
1997***
1998
1999### Span <sup>[banned]</sup>
2000
2001```c++
2002absl::Span
2003```
2004
2005**Description:** Early adaptation of C++20 `std::span`.
2006
2007**Documentation:** [Using absl::Span](https://abseil.io/tips/93)
2008
2009**Notes:**
2010*** promo
2011Banned due to being less std::-compliant than `base::span`. Keep using
2012`base::span`.
2013***
2014
2015### StatusOr <sup>[banned]</sup>
2016
2017```c++
2018absl::StatusOr<T>
2019```
2020
2021**Description:** An object that is either a usable value, or an error Status
2022explaining why such a value is not present.
2023
2024**Documentation:**
2025[statusor.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/status/statusor.h)
2026
2027**Notes:**
2028*** promo
Peter Kasting72d110f12024-02-06 19:45:262029Overlaps with `base::expected`.
Peter Kasting4f35bfc2022-10-18 18:39:122030***
2031
Peter Kasting4f35bfc2022-10-18 18:39:122032### string_view <sup>[banned]</sup>
2033
2034```c++
2035absl::string_view
2036```
2037
2038**Description:** Early adaptation of C++17 `std::string_view`.
2039
2040**Documentation:** [absl::string_view](https://abseil.io/tips/1)
2041
2042**Notes:**
2043*** promo
David Benjaminea985a22023-04-18 22:05:012044Originally banned due to only working with 8-bit characters. Now it is
2045unnecessary because, in Chromium, it is the same type as `std::string_view`.
Hong Xu5e492d32023-07-27 21:38:462046Please use `std::string_view` instead.
Peter Kasting4f35bfc2022-10-18 18:39:122047***
2048
2049### Strings Library <sup>[banned]</sup>
2050
2051```c++
2052absl::StrSplit
2053absl::StrJoin
2054absl::StrCat
2055absl::StrAppend
2056absl::Substitute
2057absl::StrContains
2058```
2059
2060**Description:** Classes and utility functions for manipulating and comparing
2061strings.
2062
2063**Documentation:**
2064[String Utilities](https://abseil.io/docs/cpp/guides/strings)
2065
2066**Notes:**
2067*** promo
Peter Kasting72d110f12024-02-06 19:45:262068Overlaps with `base/strings`. We
Peter Kasting4f35bfc2022-10-18 18:39:122069[should re-evalute](https://bugs.chromium.org/p/chromium/issues/detail?id=1371966)
2070when we've
2071[migrated](https://bugs.chromium.org/p/chromium/issues/detail?id=691162) from
Peter Kasting0efc9042024-09-17 15:48:432072`base::StringPiece` to `std::string_view`. Also note that `absl::StrFormat()` is
2073not considered part of this group, and is explicitly allowed.
Peter Kasting4f35bfc2022-10-18 18:39:122074***
2075
2076### Synchronization <sup>[banned]</sup>
2077
2078```c++
2079absl::Mutex
2080```
2081
2082**Description:** Primitives for managing tasks across different threads.
2083
2084**Documentation:**
2085[Synchronization](https://abseil.io/docs/cpp/guides/synchronization)
2086
2087**Notes:**
2088*** promo
Peter Kasting72d110f12024-02-06 19:45:262089Overlaps with `base/synchronization/`. We would love
Peter Kasting4f35bfc2022-10-18 18:39:122090[more testing](https://bugs.chromium.org/p/chromium/issues/detail?id=1371969) on
2091whether there are compelling reasons to prefer base, absl, or std
2092synchronization primitives; for now, use `base/synchronization/`.
2093***
2094
2095### Time library <sup>[banned]</sup>
2096
2097```c++
2098absl::Duration
2099absl::Time
2100absl::TimeZone
2101absl::CivilDay
2102```
2103
2104**Description:** Abstractions for holding time values, both in terms of
2105absolute time and civil time.
2106
2107**Documentation:** [Time](https://abseil.io/docs/cpp/guides/time)
2108
2109**Notes:**
2110*** promo
Peter Kasting72d110f12024-02-06 19:45:262111Overlaps with `base/time/`.
Peter Kasting4f35bfc2022-10-18 18:39:122112***
2113
Joe Masonfe4f2562021-09-15 15:23:132114## Abseil TBD Features {#absl-review}
2115
2116The following Abseil library features are not allowed in the Chromium codebase.
2117See the top of this page on how to propose moving a feature from this list into
2118the allowed or banned sections.
2119
Danil Chapovalova9f27032022-06-20 16:56:142120### AnyInvocable <sup>[tbd]</sup>
2121
2122```c++
2123absl::AnyInvocable
2124```
2125
2126**Description:** An equivalent of the C++23 std::move_only_function.
2127
2128**Documentation:**
2129* [any_invocable.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/any_invocable.h)
2130* [std::move_only_function](https://en.cppreference.com/w/cpp/utility/functional/move_only_function/move_only_function)
2131
2132**Notes:**
2133*** promo
2134Overlaps with `base::RepeatingCallback`, `base::OnceCallback`.
2135***
2136
danakja6f71cb12021-12-15 21:04:492137### Containers <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:132138
2139```c++
2140absl::flat_hash_map
2141absl::flat_hash_set
2142absl::node_hash_map
2143absl::node_hash_set
2144absl::btree_map
2145absl::btree_set
2146absl::btree_multimap
2147absl::btree_multiset
Joe Masonfe4f2562021-09-15 15:23:132148```
2149
2150**Description:** Alternatives to STL containers designed to be more efficient
2151in the general case.
2152
2153**Documentation:**
2154* [Containers](https://abseil.io/docs/cpp/guides/container)
2155* [Hash](https://abseil.io/docs/cpp/guides/hash)
2156
2157**Notes:**
2158*** promo
2159Supplements `base/containers/`.
Adam Riceaf2708e2023-07-20 14:53:162160
2161absl::InlinedVector is explicitly allowed, see the [discussion
2162thread](https://groups.google.com/a/chromium.org/g/cxx/c/jTfqVfU-Ka0/m/caaal90NCgAJ).
2163
Joe Masonfe4f2562021-09-15 15:23:132164***
2165
Mirko Bonadeide812cd2022-12-07 22:38:322166### CRC32C library <sup>[tbd]</sup>
2167
2168**Description:** API for computing CRC32C values as checksums for arbitrary
2169sequences of bytes provided as a string buffer.
2170
2171**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:082172[crc32.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/crc/crc32c.h)
Mirko Bonadeide812cd2022-12-07 22:38:322173
2174**Notes:**
2175*** promo
Peter Kasting72d110f12024-02-06 19:45:262176Overlaps with `third_party/crc32c`.
Mirko Bonadeide812cd2022-12-07 22:38:322177***
2178
Peter Kasting4f35bfc2022-10-18 18:39:122179### Log macros and related classes <sup>[tbd]</sup>
Danil Chapovalov6719fb12022-08-31 13:52:492180
2181```c++
2182LOG(INFO) << message;
2183CHECK(condition);
2184absl::AddLogSink(&custom_sink_to_capture_absl_logs);
2185```
2186
2187**Description:** Macros and related classes to perform debug loggings
2188
2189**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:082190[log.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/log/log.h)
2191[check.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/log/check.h)
Danil Chapovalov6719fb12022-08-31 13:52:492192
2193**Notes:**
2194*** promo
Peter Kasting72d110f12024-02-06 19:45:262195Overlaps with `base/logging.h`.
Danil Chapovalov6719fb12022-08-31 13:52:492196***
Danil Chapovalov79a40942023-06-21 18:35:302197
Mirko Bonadeidc928a162023-11-20 08:24:232198### NoDestructor <sup>[tbd]</sup>
2199
2200```c++
2201// Global or namespace scope.
2202ABSL_CONST_INIT absl::NoDestructor<MyRegistry> reg{"foo", "bar", 8008};
2203
2204// Function scope.
2205const std::string& MyString() {
2206 static const absl::NoDestructor<std::string> x("foo");
2207 return *x;
2208}
2209```
2210
2211**Description:** `absl::NoDestructor<T>` is a wrapper around an object of
2212type T that behaves as an object of type T but never calls T's destructor.
2213
2214**Documentation:**
2215[no_destructor.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/base/no_desctructor.h)
2216
2217**Notes:**
2218*** promo
2219Overlaps with `base::NoDestructor`.
2220***
2221
Danil Chapovalov79a40942023-06-21 18:35:302222### Nullability annotations <sup>[tbd]</sup>
2223
2224```c++
2225void PaySalary(absl::NotNull<Employee *> employee) {
2226 pay(*employee); // OK to dereference
2227}
2228```
2229
2230**Description:** Annotations to more clearly specify contracts
2231
2232**Documentation:**
2233[nullability.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/base/nullability.h)
2234
2235**Notes:**
2236*** promo
2237These nullability annotations are primarily a human readable signal about the
2238intended contract of the pointer. They are not *types* and do not currently
2239provide any correctness guarantees.
2240***
Danil Chapovalov0e8ed132023-09-28 08:05:392241
2242### Overload <sup>[tbd]</sup>
2243
2244```c++
2245std::variant<int, std::string, double> v(int{1});
2246assert(std::visit(absl::Overload(
2247 [](int) -> absl::string_view { return "int"; },
2248 [](const std::string&) -> absl::string_view {
2249 return "string";
2250 },
2251 [](double) -> absl::string_view { return "double"; }),
2252 v) == "int");
2253```
2254
2255**Description:** Returns a functor that provides overloads based on the functors passed to it
2256
2257**Documentation:**
Adam Ricebf82f342023-09-29 15:07:312258[overload.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/overload.h)
Danil Chapovalov0e8ed132023-09-28 08:05:392259
2260**Notes:**
2261*** promo
Peter Kasting72d110f12024-02-06 19:45:262262Overlaps with `base::Overloaded`.
Danil Chapovalov0e8ed132023-09-28 08:05:392263***