blob: 6c246c73f9c9925bca66f2c02d26afb0f9345d94 [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_
Peter Kasting7fb5fbf2025-01-30 18:45:2537* **Abseil:** _Default allowed; see banned features below_
Joe Masonfe4f2562021-09-15 15:23:1338
Joe Mason6a6f2582024-01-30 20:26:4339## Banned features and third-party code
40
41Third-party libraries may generally use banned features internally, although features
42with poor compiler support or poor security properties may make the library
43unsuitable to use with Chromium.
44
45Chromium code that calls functions exported from a third-party library may use
46banned library types that are required by the interface, as long as:
47
48 * The disallowed type is used only at the interface, and converted to and from
49 an equivalent allowed type as soon as practical on the Chromium side.
50 * The feature is not banned due to security issues or lack of compiler support.
51 If it is, discuss with
52 [[email protected]](https://groups.google.com/a/chromium.org/forum/#!forum/cxx)
53 to find a workaround.
54
Joe Masonfe4f2562021-09-15 15:23:1355[TOC]
56
Peter Kasting1865f2772021-12-23 21:23:5857## C++11 Banned Language Features {#core-blocklist-11}
Joe Masonfe4f2562021-09-15 15:23:1358
59The following C++11 language features are not allowed in the Chromium codebase.
60
danakja6f71cb12021-12-15 21:04:4961### Inline Namespaces <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:1362
63```c++
64inline namespace foo { ... }
65```
66
67**Description:** Allows better versioning of namespaces.
68
69**Documentation:**
70[Inline namespaces](https://en.cppreference.com/w/cpp/language/namespace#Inline_namespaces)
71
72**Notes:**
73*** promo
74Banned in the
75[Google Style Guide](https://google.github.io/styleguide/cppguide.html#Namespaces).
76Unclear how it will work with components.
77***
78
danakja6f71cb12021-12-15 21:04:4979### long long Type <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:1380
81```c++
82long long var = value;
83```
84
85**Description:** An integer of at least 64 bits.
86
87**Documentation:**
88[Fundamental types](https://en.cppreference.com/w/cpp/language/types)
89
90**Notes:**
91*** promo
Peter Kastinge2c5ee82023-02-15 17:23:0892Use a `<stdint.h>` type if you need a 64-bit number.
Peter Kasting72d110f12024-02-06 19:45:2693
Joe Masonfe4f2562021-09-15 15:23:1394[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/chromium-dev/RxugZ-pIDxk)
95***
96
danakja6f71cb12021-12-15 21:04:4997### User-Defined Literals <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:1398
99```c++
Peter Kastinge2c5ee82023-02-15 17:23:08100DistanceType var = 12_km;
Joe Masonfe4f2562021-09-15 15:23:13101```
102
103**Description:** Allows user-defined literal expressions.
104
105**Documentation:**
106[User-defined literals](https://en.cppreference.com/w/cpp/language/user_literal)
107
108**Notes:**
109*** promo
110Banned in the
111[Google Style Guide](https://google.github.io/styleguide/cppguide.html#Operator_Overloading).
112***
113
Peter Kasting1865f2772021-12-23 21:23:58114## C++11 Banned Library Features {#library-blocklist-11}
Joe Masonfe4f2562021-09-15 15:23:13115
116The following C++11 library features are not allowed in the Chromium codebase.
117
Peter Kasting6f79b202023-08-09 21:25:41118### &lt;cctype&gt;, &lt;ctype.h&gt;, &lt;cwctype&gt;, &lt;wctype.h&gt; <sup>[banned]</sup>
119
120```c++
121#include <cctype>
122#include <cwctype>
123#include <ctype.h>
124#include <wctype.h>
125```
126
127**Description:** Provides utilities for ASCII characters.
128
129**Documentation:**
130[Standard library header `<cctype>`](https://en.cppreference.com/w/cpp/header/cctype),
131[Standard library header `<cwctype>`](https://en.cppreference.com/w/cpp/header/cwctype)
132
133**Notes:**
134*** promo
135Banned due to dependence on the C locale as well as UB when arguments don't fit
136in an `unsigned char`/`wchar_t`. Use similarly-named replacements in
137[third_party/abseil-cpp/absl/strings/ascii.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/strings/ascii.h)
138instead.
139***
140
Peter Kastinge2c5ee82023-02-15 17:23:08141### &lt;cfenv&gt;, &lt;fenv.h&gt; <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13142
143```c++
144#include <cfenv>
145#include <fenv.h>
146```
147
148**Description:** Provides floating point status flags and control modes for
Peter Kastinge2c5ee82023-02-15 17:23:08149C-compatible code.
Joe Masonfe4f2562021-09-15 15:23:13150
151**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08152[Standard library header `<cfenv>`](https://en.cppreference.com/w/cpp/header/cfenv)
Joe Masonfe4f2562021-09-15 15:23:13153
154**Notes:**
155*** promo
156Banned by the
157[Google Style Guide](https://google.github.io/styleguide/cppguide.html#C++11)
158due to concerns about compiler support.
159***
160
Peter Kastinge2c5ee82023-02-15 17:23:08161### &lt;chrono&gt; <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13162
163```c++
164#include <chrono>
165```
166
Peter Kastinge2c5ee82023-02-15 17:23:08167**Description:** A standard date and time library.
Joe Masonfe4f2562021-09-15 15:23:13168
169**Documentation:**
170[Date and time utilities](https://en.cppreference.com/w/cpp/chrono)
171
172**Notes:**
173*** promo
Peter Kasting72d110f12024-02-06 19:45:26174Overlaps with `base/time`.
Joe Masonfe4f2562021-09-15 15:23:13175***
176
Peter Kastinge2c5ee82023-02-15 17:23:08177### &lt;exception&gt; <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13178
179```c++
180#include <exception>
181```
182
Peter Kastinge2c5ee82023-02-15 17:23:08183**Description:** Exception throwing and handling.
Joe Masonfe4f2562021-09-15 15:23:13184
185**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08186[Standard library header `<exception>`](https://en.cppreference.com/w/cpp/header/exception)
Joe Masonfe4f2562021-09-15 15:23:13187
188**Notes:**
189*** promo
190Exceptions are banned by the
191[Google Style Guide](https://google.github.io/styleguide/cppguide.html#Exceptions)
192and disabled in Chromium compiles. However, the `noexcept` specifier is
193explicitly allowed.
Peter Kastinge2c5ee82023-02-15 17:23:08194
Joe Masonfe4f2562021-09-15 15:23:13195[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/chromium-dev/8i4tMqNpHhg)
196***
197
Peter Kastingc7460d982023-03-14 21:01:42198### Engines And Generators From &lt;random&gt; <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13199
200```c++
Peter Kastingc7460d982023-03-14 21:01:42201std::mt19937 generator;
Joe Masonfe4f2562021-09-15 15:23:13202```
203
Peter Kastingc7460d982023-03-14 21:01:42204**Description:** Methods of generating random numbers.
Joe Masonfe4f2562021-09-15 15:23:13205
206**Documentation:**
207[Pseudo-random number generation](https://en.cppreference.com/w/cpp/numeric/random)
208
209**Notes:**
210*** promo
Peter Kastingc7460d982023-03-14 21:01:42211Do not use any random number engines or generators from `<random>`. Instead, use
212`base::RandomBitGenerator`. (You may use the distributions from `<random>`.)
Peter Kastinge2c5ee82023-02-15 17:23:08213
Joe Masonfe4f2562021-09-15 15:23:13214[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/16Xmw05C-Y0)
215***
216
Peter Kastinge2c5ee82023-02-15 17:23:08217### &lt;ratio&gt; <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13218
219```c++
Peter Kastinge2c5ee82023-02-15 17:23:08220#include <ratio>
Joe Masonfe4f2562021-09-15 15:23:13221```
222
Peter Kastinge2c5ee82023-02-15 17:23:08223**Description:** Provides compile-time rational numbers.
Joe Masonfe4f2562021-09-15 15:23:13224
225**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08226[`std::ratio`](https://en.cppreference.com/w/cpp/numeric/ratio/ratio)
Joe Masonfe4f2562021-09-15 15:23:13227
228**Notes:**
229*** promo
230Banned by the
231[Google Style Guide](https://google.github.io/styleguide/cppguide.html#C++11)
232due to concerns that this is tied to a more template-heavy interface style.
233***
234
Peter Kastinge2c5ee82023-02-15 17:23:08235### &lt;regex&gt; <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13236
237```c++
238#include <regex>
239```
240
Peter Kastinge2c5ee82023-02-15 17:23:08241**Description:** A standard regular expressions library.
Joe Masonfe4f2562021-09-15 15:23:13242
243**Documentation:**
244[Regular expressions library](https://en.cppreference.com/w/cpp/regex)
245
246**Notes:**
247*** promo
248Overlaps with many regular expression libraries in Chromium. When in doubt, use
Peter Kastinge2c5ee82023-02-15 17:23:08249`third_party/re2`.
Joe Masonfe4f2562021-09-15 15:23:13250***
251
Peter Kasting5fdcd782025-01-13 14:57:07252### std::aligned_{storage,union} <sup>[banned]</sup>
253
254```c++
255std::aligned_storage<sizeof(T), alignof<T>>::type buf;
256```
257
258**Description:** Creates aligned, uninitialized storage to later hold one or
259more objects.
260
261**Documentation:**
262[`std::aligned_storage`](https://en.cppreference.com/w/cpp/types/aligned_storage)
263
264**Notes:**
265*** promo
266Deprecated in C++23. Generally, use `alignas(T) char buf[sizeof(T)];`. Aligned
267unions can be handled similarly, using the max alignment and size of the union
268members, either passed via a pack or computed inline.
269***
270
Peter Kastinge2c5ee82023-02-15 17:23:08271### std::bind <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13272
273```c++
Peter Kastinge2c5ee82023-02-15 17:23:08274auto x = std::bind(function, args, ...);
Joe Masonfe4f2562021-09-15 15:23:13275```
276
Peter Kastinge2c5ee82023-02-15 17:23:08277**Description:** Declares a function object bound to certain arguments.
Joe Masonfe4f2562021-09-15 15:23:13278
279**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08280[`std::bind`](https://en.cppreference.com/w/cpp/utility/functional/bind)
Joe Masonfe4f2562021-09-15 15:23:13281
282**Notes:**
283*** promo
Peter Kastinge2c5ee82023-02-15 17:23:08284Use `base::Bind` instead. Compared to `std::bind`, `base::Bind` helps prevent
285lifetime issues by preventing binding of capturing lambdas and by forcing
286callers to declare raw pointers as `Unretained`.
Joe Masonfe4f2562021-09-15 15:23:13287
Peter Kastinge2c5ee82023-02-15 17:23:08288[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/SoEj7oIDNuA)
Joe Masonfe4f2562021-09-15 15:23:13289***
290
Peter Kastinge2c5ee82023-02-15 17:23:08291### std::function <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13292
293```c++
Peter Kastinge2c5ee82023-02-15 17:23:08294std::function x = [] { return 10; };
295std::function y = std::bind(foo, args);
Joe Masonfe4f2562021-09-15 15:23:13296```
297
Peter Kastinge2c5ee82023-02-15 17:23:08298**Description:** Wraps a standard polymorphic function.
Joe Masonfe4f2562021-09-15 15:23:13299
300**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08301[`std::function`](https://en.cppreference.com/w/cpp/utility/functional/function)
302
303**Notes:**
304*** promo
Marijn Kruisselbrinke453e56c2023-12-05 22:35:13305Use `base::{Once,Repeating}Callback` or `base::FunctionRef` instead. Compared
306to `std::function`, `base::{Once,Repeating}Callback` directly supports
307Chromium's refcounting classes and weak pointers and deals with additional
308thread safety concerns.
Peter Kastinge2c5ee82023-02-15 17:23:08309
310[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/SoEj7oIDNuA)
311***
312
313### std::shared_ptr <sup>[banned]</sup>
314
315```c++
316std::shared_ptr<int> x = std::make_shared<int>(10);
317```
318
319**Description:** Allows shared ownership of a pointer through reference counts.
320
321**Documentation:**
322[`std::shared_ptr`](https://en.cppreference.com/w/cpp/memory/shared_ptr)
323
324**Notes:**
325*** promo
326Unlike `base::RefCounted`, uses extrinsic rather than intrinsic reference
327counting. Could plausibly be used in Chromium, but would require significant
328migration.
329
330[Google Style Guide](https://google.github.io/styleguide/cppguide.html#Ownership_and_Smart_Pointers),
Peter Kasting72d110f12024-02-06 19:45:26331[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/aT2wsBLKvzI)
Peter Kastinge2c5ee82023-02-15 17:23:08332***
333
334### std::{sto{i,l,ul,ll,ull,f,d,ld},to_string} <sup>[banned]</sup>
335
336```c++
337int x = std::stoi("10");
338```
339
340**Description:** Converts strings to/from numbers.
341
342**Documentation:**
343[`std::stoi`, `std::stol`, `std::stoll`](https://en.cppreference.com/w/cpp/string/basic_string/stol),
344[`std::stoul`, `std::stoull`](https://en.cppreference.com/w/cpp/string/basic_string/stoul),
345[`std::stof`, `std::stod`, `std::stold`](https://en.cppreference.com/w/cpp/string/basic_string/stof),
346[`std::to_string`](https://en.cppreference.com/w/cpp/string/basic_string/to_string)
Joe Masonfe4f2562021-09-15 15:23:13347
348**Notes:**
349*** promo
350The string-to-number conversions rely on exceptions to communicate failure,
351while the number-to-string conversions have performance concerns and depend on
Peter Kastinge2c5ee82023-02-15 17:23:08352the locale. Use `base/strings/string_number_conversions.h` instead.
Joe Masonfe4f2562021-09-15 15:23:13353***
354
Peter Kastinge2c5ee82023-02-15 17:23:08355### std::weak_ptr <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13356
Peter Kastinge2c5ee82023-02-15 17:23:08357```c++
358std::weak_ptr<int> x = my_shared_x;
359```
360
361**Description:** Allows a weak reference to a `std::shared_ptr`.
362
363**Documentation:**
364[`std::weak_ptr`](https://en.cppreference.com/w/cpp/memory/weak_ptr)
365
366**Notes:**
367*** promo
368Banned because `std::shared_ptr` is banned. Use `base::WeakPtr` instead.
Joe Masonfe4f2562021-09-15 15:23:13369***
370
Peter Kastinge2c5ee82023-02-15 17:23:08371### Thread Support Library <sup>[banned]</sup>
372
373```c++
374#include <barrier> // C++20
375#include <condition_variable>
376#include <future>
377#include <latch> // C++20
378#include <mutex>
379#include <semaphore> // C++20
380#include <stop_token> // C++20
381#include <thread>
382```
383
Joe Masonfe4f2562021-09-15 15:23:13384**Description:** Provides a standard multithreading library using `std::thread`
385and associates
386
387**Documentation:**
388[Thread support library](https://en.cppreference.com/w/cpp/thread)
389
390**Notes:**
391*** promo
Peter Kasting72d110f12024-02-06 19:45:26392Overlaps with `base/synchronization`. `base::Thread` is tightly coupled to
393`base::MessageLoop` which would make it hard to replace. We should investigate
394using standard mutexes, or `std::unique_lock`, etc. to replace our
Peter Kastinge2c5ee82023-02-15 17:23:08395locking/synchronization classes.
Joe Masonfe4f2562021-09-15 15:23:13396***
397
Peter Kasting72d110f12024-02-06 19:45:26398## C++17 Banned Language Features {#core-blocklist-17}
Peter Kasting1865f2772021-12-23 21:23:58399
Peter Kasting72d110f12024-02-06 19:45:26400The following C++17 language features are not allowed in the Chromium codebase.
Peter Kasting1865f2772021-12-23 21:23:58401
Peter Kasting72d110f12024-02-06 19:45:26402### UTF-8 character literals <sup>[banned]</sup>
Peter Kasting1865f2772021-12-23 21:23:58403
404```c++
Peter Kasting72d110f12024-02-06 19:45:26405char x = u8'x'; // C++17
406char8_t x = u8'x'; // C++20
Peter Kasting1865f2772021-12-23 21:23:58407```
408
Peter Kasting72d110f12024-02-06 19:45:26409**Description:** A character literal that begins with `u8` is a character
410literal of type `char` (C++17) or `char8_t` (C++20). The value of a UTF-8
411character literal is equal to its ISO 10646 code point value.
Peter Kasting1865f2772021-12-23 21:23:58412
413**Documentation:**
Peter Kasting72d110f12024-02-06 19:45:26414[Character literal](https://en.cppreference.com/w/cpp/language/character_literal)
Peter Kasting1865f2772021-12-23 21:23:58415
416**Notes:**
417*** promo
Peter Kasting72d110f12024-02-06 19:45:26418Banned because `char8_t` is banned. Use an unprefixed character or string
419literal; it should be encoded in the binary as UTF-8 on all supported platforms.
Peter Kasting6d77e9d2023-02-09 21:58:18420***
421
422## C++17 Banned Library Features {#library-blocklist-17}
423
424The following C++17 library features are not allowed in the Chromium codebase.
425
Peter Kasting72d110f12024-02-06 19:45:26426### Mathematical special functions <sup>[banned]</sup>
427
428```c++
429std::assoc_laguerre()
430std::assoc_legendre()
431std::beta()
432std::comp_ellint_1()
433std::comp_ellint_2()
434std::comp_ellint_3()
435std::cyl_bessel_i()
436std::cyl_bessel_j()
437std::cyl_bessel_k()
438std::cyl_neumann()
439std::ellint_1()
440std::ellint_2()
441std::ellint_3()
442std::expint()
443std::hermite()
444std::legendre()
445std::laguerre()
446std::riemann_zeta()
447std::sph_bessel()
448std::sph_legendre()
449std::sph_neumann()
450```
451
452**Description:** A variety of mathematical functions.
453
454**Documentation:**
455[Mathematical special functions](https://en.cppreference.com/w/cpp/numeric/special_functions)
456
457**Notes:**
458*** promo
459Banned due to
460[lack of libc++ support](https://libcxx.llvm.org/Status/Cxx17.html).
461***
462
463### Parallel algorithms <sup>[banned]</sup>
464
465```c++
466auto it = std::find(std::execution::par, std::begin(vec), std::end(vec), 2);
467```
468
469**Description:** Many of the STL algorithms, such as the `copy`, `find` and
470`sort` methods, now support the parallel execution policies: `seq`, `par`, and
471`par_unseq` which translate to "sequentially", "parallel" and
472"parallel unsequenced".
473
474**Documentation:**
475[`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)
476
477**Notes:**
478*** promo
479Banned because
480[libc++ support is incomplete](https://libcxx.llvm.org/Status/PSTL.html) and the
481interaction of its threading implementation with Chrome's is unclear. Prefer to
482explicitly parallelize long-running algorithms using Chrome's threading APIs, so
483the same scheduler controls, shutdown policies, tracing, etc. apply as in any
484other multithreaded code.
485***
486
Peter Kasting6d77e9d2023-02-09 21:58:18487### std::aligned_alloc <sup>[banned]</sup>
488
489```c++
490int* p2 = static_cast<int*>(std::aligned_alloc(1024, 1024));
491```
492
493**Description:** Allocates uninitialized storage with the specified alignment.
494
495**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08496[`std::aligned_alloc`](https://en.cppreference.com/w/cpp/memory/c/aligned_alloc)
Peter Kasting6d77e9d2023-02-09 21:58:18497
498**Notes:**
499*** promo
500[Will be allowed soon](https://crbug.com/1412818); for now, use
501`base::AlignedAlloc`.
502***
503
504### std::any <sup>[banned]</sup>
505
506```c++
507std::any x = 5;
508```
509
510**Description:** A type-safe container for single values of any type.
511
512**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08513[`std::any`](https://en.cppreference.com/w/cpp/utility/any)
Peter Kasting6d77e9d2023-02-09 21:58:18514
515**Notes:**
516*** promo
Peter Kasting72d110f12024-02-06 19:45:26517Banned since workaround for lack of RTTI
518[isn't compatible with the component build](https://crbug.com/1096380). See also
519`absl::any`.
Peter Kasting6d77e9d2023-02-09 21:58:18520
Peter Kasting72d110f12024-02-06 19:45:26521[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/00cpZ07nye4)
522***
523
524### std::byte <sup>[banned]</sup>
525
526```c++
527std::byte b = 0xFF;
528int i = std::to_integer<int>(b); // 0xFF
529```
530
531**Description:** The contents of a single memory unit. `std::byte` has the same
532size and aliasing rules as `unsigned char`, but does not semantically represent
533a character or arithmetic value, and does not expose operators other than
534bitwise ops.
535
536**Documentation:**
537[`std::byte`](https://en.cppreference.com/w/cpp/types/byte)
538
539**Notes:**
540*** promo
541Banned due to low marginal utility in practice, high conversion costs, and
542programmer confusion about "byte" vs. "octet". Use `uint8_t` for the common case
543of "8-bit unsigned value", and `char` for the atypical case of code that works
544with memory without regard to its contents' values or semantics (e.g allocator
545implementations).
546
547[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/bBY0gZa1Otk)
Peter Kasting6d77e9d2023-02-09 21:58:18548***
549
Peter Kasting6d77e9d2023-02-09 21:58:18550### std::filesystem <sup>[banned]</sup>
551
552```c++
553#include <filesystem>
554```
555
556**Description:** A standard way to manipulate files, directories, and paths in a
557filesystem.
558
559**Documentation:**
560[Filesystem library](https://en.cppreference.com/w/cpp/filesystem)
561
562**Notes:**
563*** promo
564Banned by the [Google Style Guide](https://google.github.io/styleguide/cppguide.html#Other_Features).
565***
566
Peter Kasting72d110f12024-02-06 19:45:26567### std::{from,to}_chars <sup>[banned]</sup>
568
569```c++
570std::from_chars(str.data(), str.data() + str.size(), result);
571std::to_chars(str.data(), str.data() + str.size(), 42);
572```
573
574**Description:** Locale-independent, non-allocating, non-throwing functions to
575convert values from/to character strings, designed for use in high-throughput
576contexts.
577
578**Documentation:**
579[`std::from_chars`](https://en.cppreference.com/w/cpp/utility/from_chars)
580[`std::to_chars`](https://en.cppreference.com/w/cpp/utility/to_chars),
581
582**Notes:**
583*** promo
584Overlaps with utilities in `base/strings/string_number_conversions.h`, which are
585easier to use correctly.
586***
587
David Benjamind9d21aa12023-10-02 23:29:57588### std::in_place{_type,_index}[_t] <sup>[banned]</sup>
Avi Drissmanbc6545f42022-05-03 17:47:38589
590```c++
Avi Drissmanbc6545f42022-05-03 17:47:38591std::variant<int, float> v{std::in_place_type<int>, 1.4};
592```
593
David Benjamind9d21aa12023-10-02 23:29:57594**Description:** `std::in_place_type` and `std::in_place_index` are
595disambiguation tags for `std::variant` and `std::any` to indicate that the
596object should be constructed in-place.
Avi Drissmanbc6545f42022-05-03 17:47:38597
598**Documentation:**
David Benjamind9d21aa12023-10-02 23:29:57599[`std::in_place_type`](https://en.cppreference.com/w/cpp/utility/in_place)
Avi Drissmanbc6545f42022-05-03 17:47:38600
601**Notes:**
602*** promo
David Benjamind9d21aa12023-10-02 23:29:57603Banned for now because `std::variant` and `std::any` are banned. Because
604`absl::variant` is used instead, and it requires `absl::in_place_type`, use
605`absl::in_place_type` for non-Abseil Chromium
Peter Kasting72d110f12024-02-06 19:45:26606code.
607
608[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ZspmuJPpv6s)
609***
610
611### std::{pmr::memory_resource,polymorphic_allocator} <sup>[banned]</sup>
612
613```c++
614#include <memory_resource>
615```
616
617**Description:** Manages memory allocations using runtime polymorphism.
618
619**Documentation:**
620[`std::pmr::memory_resource`](https://en.cppreference.com/w/cpp/memory/memory_resource),
621[`std::pmr::polymorphic_allocator`](https://en.cppreference.com/w/cpp/memory/polymorphic_allocator)
622
623**Notes:**
624*** promo
625Banned because Chromium does not customize allocators
626([PartitionAlloc](https://chromium.googlesource.com/chromium/src/+/main/base/allocator/partition_allocator/PartitionAlloc.md)
627is used globally).
628***
629
630### std::timespec_get <sup>[banned]</sup>
631
632```c++
633std::timespec ts;
634std::timespec_get(&ts, TIME_UTC);
635```
636
637**Description:** Gets the current calendar time in the given time base.
638
639**Documentation:**
640[`std::timespec_get`](https://en.cppreference.com/w/cpp/chrono/c/timespec_get)
641
642**Notes:**
643*** promo
644Banned due to unclear, implementation-defined behavior. On POSIX, use
645`base::TimeDelta::ToTimeSpec()`; this could be supported on other platforms if
646desirable.
Avi Drissmanbc6545f42022-05-03 17:47:38647***
648
Peter Kasting6d77e9d2023-02-09 21:58:18649### std::uncaught_exceptions <sup>[banned]</sup>
650
651```c++
652int count = std::uncaught_exceptions();
653```
654
655**Description:** Determines whether there are live exception objects.
656
657**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08658[`std::uncaught_exceptions`](https://en.cppreference.com/w/cpp/error/uncaught_exception)
Peter Kasting6d77e9d2023-02-09 21:58:18659
660**Notes:**
661*** promo
662Banned because exceptions are banned.
663***
664
665### std::variant <sup>[banned]</sup>
666
667```c++
668std::variant<int, double> v = 12;
669```
670
671**Description:** The class template `std::variant` represents a type-safe
672`union`. An instance of `std::variant` at any given time holds a value of one of
673its alternative types (it's also possible for it to be valueless).
674
675**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08676[`std::variant`](https://en.cppreference.com/w/cpp/utility/variant)
Peter Kasting6d77e9d2023-02-09 21:58:18677
678**Notes:**
679*** promo
Peter Kastinge2c5ee82023-02-15 17:23:08680[Will be allowed soon](https://crbug.com/1373620); for now, use `absl::variant`.
Peter Kasting6d77e9d2023-02-09 21:58:18681***
682
683### Transparent std::owner_less <sup>[banned]</sup>
684
685```c++
686std::map<std::weak_ptr<T>, U, std::owner_less<>>
687```
688
689**Description:** Function object providing mixed-type owner-based ordering of
690shared and weak pointers, regardless of the type of the pointee.
691
692**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08693[`std::owner_less`](https://en.cppreference.com/w/cpp/memory/owner_less)
Peter Kasting6d77e9d2023-02-09 21:58:18694
695**Notes:**
696*** promo
697Banned since `std::shared_ptr` and `std::weak_ptr` are banned.
698***
699
700### weak_from_this <sup>[banned]</sup>
701
702```c++
703auto weak_ptr = weak_from_this();
704```
705
706**Description:** Returns a `std::weak_ptr<T>` that tracks ownership of `*this`
707by all existing `std::shared_ptr`s that refer to `*this`.
708
709**Documentation:**
Peter Kastinge2c5ee82023-02-15 17:23:08710[`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:18711
712**Notes:**
713*** promo
714Banned since `std::shared_ptr` and `std::weak_ptr` are banned.
Avi Drissman37b7d512022-04-01 22:01:40715***
716
Arthur Sonzognid8517c92023-03-03 09:41:45717## C++20 Allowed Language Features {#core-allowlist-20}
718
Peter Kastingf86817c2023-11-13 18:00:11719The following C++20 language features are allowed in the Chromium codebase.
720
721### Abbreviated function templates <sup>[allowed]</sup>
722
723```c++
724// template <typename T>
725// void f1(T x);
726void f1(auto x);
727
728// template <C T> // `C` is a concept
729// void f2(T x);
730void f2(C auto x);
731
732// template <typename T, C U> // `C` is a concept
733// void f3(T x, U y);
734template <typename T>
735void f3(T x, C auto y);
736
737// template<typename... Ts>
738// void f4(Ts... xs);
739void f4(auto... xs);
740```
741
742**Description:** Function params of type `auto` become syntactic sugar for
743declaring a template type for each such parameter.
744
745**Documentation:**
746[Abbreviated function template](https://en.cppreference.com/w/cpp/language/function_template#Abbreviated_function_template)
747
748**Notes:**
749*** promo
750[Migration bug](https://crbug.com/1414526)
751***
752
753### consteval <sup>[allowed]</sup>
754
755```c++
756consteval int sqr(int n) { return n * n; }
757constexpr int kHundred = sqr(10); // OK
758constexpr int quad(int n) { return sqr(sqr(n)); } // ERROR, might be runtime
759```
760
761**Description:** Specified that a function may only be used in a compile-time
762context.
763
764**Documentation:**
765[`consteval` specifier](https://en.cppreference.com/w/cpp/language/consteval)
766
767**Notes:**
768*** promo
769None
770***
771
772### Constraints and concepts <sup>[allowed]</sup>
773
774```c++
775// `Hashable` is a concept satisfied by any type `T` for which the expression
776// `std::hash<T>{}(a)` compiles and produces a value convertible to `size_t`.
777template<typename T>
778concept Hashable = requires(T a)
779{
780 { std::hash<T>{}(a) } -> std::convertible_to<size_t>;
781};
782template <Hashable T> // Only instantiable for `T`s that satisfy `Hashable`.
783void f(T) { ... }
784```
785
786**Description:** Allows bundling sets of requirements together as named
787concepts, then enforcing them on template arguments.
788
789**Documentation:**
790[Constraints and concepts](https://en.cppreference.com/w/cpp/language/constraints)
791
792**Notes:**
793*** promo
794[Migration bug](https://crbug.com/1414528)
795***
796
797### Default comparisons <sup>[allowed]</sup>
798
799```c++
Roland Bock5dbdff62024-01-20 09:33:53800class S : public T {
801 // Non-member equality operator with access to private members.
danakj4b9193e2024-02-02 17:31:33802 // Compares `T` bases, then `x`, then `y`, short-circuiting when
803 // it finds inequality.
Roland Bock5dbdff62024-01-20 09:33:53804 friend bool operator==(const S&, const S&) = default;
805
danakj4b9193e2024-02-02 17:31:33806 // Non-member ordering operator with access to private members.
807 // Compares `T` bases, then `x`, then `y`, short-circuiting when
808 // it finds an ordering difference.
809 friend auto operator<=>(const S&, const S&) = default;
810
Peter Kastingf86817c2023-11-13 18:00:11811 int x;
812 bool y;
813};
814```
815
Roland Bock5dbdff62024-01-20 09:33:53816**Description:** Requests that the compiler generate the implementation of
817any comparison operator, including `<=>`. Prefer non-member comparison
818operators. When defaulting `<=>`, also explicitly default `==`. Together these
819are sufficient to allow any comparison as long as callers do not need to take
820the address of any non-declared operator.
Peter Kastingf86817c2023-11-13 18:00:11821
822**Documentation:**
823[Default comparisons](https://en.cppreference.com/w/cpp/language/default_comparisons)
824
825**Notes:**
826*** promo
danakj4b9193e2024-02-02 17:31:33827Unlike constructors/destructors, our compiler extensions do not require these
828to be written out-of-line in the .cc file. Feel free to write `= default`
829directly in the header, as this is much simpler to write.
830
831- [Migration bug](https://crbug.com/1414530)
David Bokanc246fc92024-02-28 17:55:47832- [Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/h4lVi2jHU-0/m/X0q_Bh2IAAAJ)
danakj4b9193e2024-02-02 17:31:33833
Peter Kastingf86817c2023-11-13 18:00:11834***
835
Arthur Sonzognid8517c92023-03-03 09:41:45836### Designated initializers <sup>[allowed]</sup>
837
838```c++
839struct S { int x = 1; int y = 2; }
840S s{ .y = 3 }; // OK, s.x == 1, s.y == 3
841```
842
843**Description:** Allows explicit initialization of subsets of aggregate members
844at construction.
845
846**Documentation:**
847[Designated initializers](https://en.cppreference.com/w/cpp/language/aggregate_initialization#Designated_initializers)
848
849**Notes:**
850*** promo
851None
852***
853
Peter Kastingf86817c2023-11-13 18:00:11854### __has_cpp_attribute <sup>[allowed]</sup>
855
856```c++
857#if __has_cpp_attribute(assume) // Toolchain supports C++23 `[[assume]]`.
858...
859#endif
860```
861
862**Description:** Checks whether the toolchain supports a particular standard
863attribute.
864
865**Documentation:**
866[Feature testing](https://en.cppreference.com/w/cpp/feature_test)
867
868**Notes:**
869*** promo
870None
871***
872
873### constinit <sup>[allowed]</sup>
874
875```c++
876constinit int x = 3;
877void foo() {
878 ++x;
879}
880```
881
882**Description:** Ensures that a variable can be compile-time initialized. This
883is like a milder form of `constexpr` that does not force variables to be const
884or have constant destruction.
885
886**Documentation:**
887[`constinit` specifier](https://en.cppreference.com/w/cpp/language/constinit)
888
889**Notes:**
890*** promo
891[Migration bug](https://crbug.com/1414612)
892***
893
894### Initializers for bit-field members <sup>[allowed]</sup>
895
896```c++
897struct S {
898 uint32_t x : 27 = 2;
899};
900```
901
902**Description:** Allows specifying the default initial value of a bit-field
903member, as can already be done for other member types.
904
905**Documentation:**
906[Bit-field](https://en.cppreference.com/w/cpp/language/bit_field)
907
908**Notes:**
909*** promo
910None
911***
912
913### Lambda captures with initializers that are pack expansions <sup>[allowed]</sup>
914
915```c++
916template <typename... Args>
917void foo(Args... args) {
918 const auto l = [...n = args] { (x(n), ...); };
919}
920```
921
922**Description:** Allows initializing a capture with a pack expansion.
923
924**Documentation:**
925[Lambda capture](https://en.cppreference.com/w/cpp/language/lambda#Lambda_capture)
926
927**Notes:**
928*** promo
929None
930***
931
932### Language feature-test macros <sup>[allowed]</sup>
933
934```c++
935#if !defined(__cpp_modules) || (__cpp_modules < 201907L)
936... // Toolchain does not support modules
937#endif
938```
939
940**Description:** Provides a standardized way to test the toolchain's
941implementation of a particular language feature.
942
943**Documentation:**
944[Feature testing](https://en.cppreference.com/w/cpp/feature_test)
945
946**Notes:**
947*** promo
948None
949***
950
Peter Kasting580af31d2024-08-06 02:50:59951### [[likely]], [[unlikely]] <sup>[allowed]</sup>
952
953```c++
954if (n > 0) [[likely]] {
955 return 1;
956}
957```
958
959**Description:** Tells the optimizer that a particular codepath is more or less
960likely than an alternative.
961
962**Documentation:**
963[C++ attribute: `likely`, `unlikely`](https://en.cppreference.com/w/cpp/language/attributes/likely)
964
965**Notes:**
966*** promo
Peter Kasting2c40e492024-10-18 00:27:19967[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/bk9YC5qSDF8)
Peter Kasting580af31d2024-08-06 02:50:59968***
969
Peter Kastingf86817c2023-11-13 18:00:11970### Range-for statements with initializer <sup>[allowed]</sup>
971
972```c++
973T foo();
974...
975for (auto& x : foo().items()) { ... } // UAF before C++23!
976for (T thing = foo(); auto& x : thing.items()) { ... } // OK
977```
978
979**Description:** Like C++17's selection statements with initializer.
980Particularly useful before C++23, since temporaries inside range-expressions are
981not lifetime-extended until the end of the loop before C++23.
982
983**Documentation:**
984[Range-based `for` loop](https://en.cppreference.com/w/cpp/language/range-for)
985
986**Notes:**
987*** promo
988[Migration bug](https://crbug.com/1414531)
989***
990
991### Three-way comparison ("spaceship") operator <sup>[allowed]</sup>
992
993```c++
994// `ordering` is an instance of `std::strong_odering` or `std::partial_ordering`
995// that describes how `a` and `b` are related.
996const auto ordering = a <=> b;
997if (ordering < 0) { ... } // `a` < `b`
998else if (ordering > 0) { ... } // `a` > `b`
999else { ... } // `a` == `b`
1000```
1001
1002**Description:** Compares two objects in a fashion similar to `strcmp`. Perhaps
1003most useful when defined as an overload in a class, in which case it can replace
1004definitions of other inequalities. See also "Default comparisons".
1005
1006**Documentation:**
1007[Three-way comparison](https://en.cppreference.com/w/cpp/language/operator_comparison#Three-way_comparison)
1008
1009**Notes:**
1010*** promo
1011[Migration bug](https://crbug.com/1414530)
1012***
1013
Peter Kasting2c40e492024-10-18 00:27:191014### using enum declarations <sup>[allowed]</sup>
1015
1016```c++
1017enum class E { kA = 1 };
1018void f() {
1019 using enum E;
1020 auto a = kA;
1021}
1022```
1023
1024**Description:** Introduces enumerator element names into the current scope.
1025
1026**Documentation:**
1027[`using enum` declaration](https://en.cppreference.com/w/cpp/language/enum#using_enum_declaration)
1028
1029**Notes:**
1030*** promo
1031Usage is subject to the Google Style
1032[guidelines on aliases](https://google.github.io/styleguide/cppguide.html#Aliases).
1033
1034[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/Y0lf-DSOR3A)
1035***
1036
Peter Kastingf86817c2023-11-13 18:00:111037## C++20 Allowed Library Features {#library-allowlist-20}
1038
1039The following C++20 library features are allowed in the Chromium codebase.
1040
1041### &lt;bit&gt; <sup>[allowed]</sup>
1042
1043```c++
1044#include <bit>
1045```
1046
1047**Description:** Provides various byte- and bit-twiddling functions, e.g.
1048counting leading zeros.
1049
1050**Documentation:**
1051[Standard library header `<bit>`](https://en.cppreference.com/w/cpp/header/bit)
1052
1053**Notes:**
1054*** promo
1055[Migration bug](https://crbug.com/1414634)
1056***
1057
1058### &lt;compare&gt; <sup>[allowed]</sup>
1059
1060```c++
1061#include <compare>
1062```
1063
1064**Description:** Concepts and classes used to implement three-way comparison
1065("spaceship", `<=>`) support.
1066
1067**Documentation:**
1068[Standard library header `<compare>`](https://en.cppreference.com/w/cpp/header/compare)
1069
1070**Notes:**
1071*** promo
1072None
1073***
1074
1075### &lt;concepts&gt; <sup>[allowed]</sup>
1076
1077```c++
1078#include <concepts>
1079```
1080
1081**Description:** Various useful concepts, many of which replace pre-concept
1082machinery in `<type_traits>`.
1083
1084**Documentation:**
1085[Standard library header `<concepts>`](https://en.cppreference.com/w/cpp/header/concepts)
1086
1087**Notes:**
1088*** promo
1089None
1090***
1091
Daniel Cheng89719222024-07-04 04:59:291092### Range algorithms <sup>[allowed]</sup>
1093
1094```c++
1095constexpr int kArr[] = {2, 4, 6, 8, 10, 12};
1096constexpr auto is_even = [] (auto x) { return x % 2 == 0; };
1097static_assert(std::ranges::all_of(kArr, is_even));
1098```
1099
1100**Description:** Provides versions of most algorithms that accept either an
1101iterator-sentinel pair or a single range argument.
1102
1103**Documentation:**
1104[Ranges algorithms](https://en.cppreference.com/w/cpp/algorithm/ranges)
1105
1106**Notes:**
1107*** promo
Daniel Cheng89719222024-07-04 04:59:291108[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ZnIbkfJ0Glw)
1109***
1110
1111### Range access, range primitives, dangling iterator handling, and range concepts <sup>[allowed]</sup>
1112
1113```c++
1114// Range access:
1115constexpr int kArr[] = {2, 4, 6, 8, 10, 12};
1116static_assert(std::ranges::size(kArr) == 6);
1117
1118// Range primitives:
1119static_assert(
1120 std::same_as<std::ranges::iterator_t<decltype(kArr)>, const int*>);
1121
1122// Range concepts:
1123static_assert(std::ranges::contiguous_range<decltype(kArr)>);
1124```
1125
1126**Description:** Various helper functions and types for working with ranges.
1127
1128**Documentation:**
1129[Ranges library](https://en.cppreference.com/w/cpp/ranges)
1130
1131**Notes:**
1132*** promo
1133Supersedes `//base`'s backports in `//base//ranges/ranges.h`.
1134
1135[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ZnIbkfJ0Glw)
1136***
1137
Peter Kastingf86817c2023-11-13 18:00:111138### Library feature-test macros and &lt;version&gt; <sup>[allowed]</sup>
1139
1140```c++
1141#if !defined(__cpp_lib_atomic_value_initialization) || \
1142 (__cpp_lib_atomic_value_initialization < 201911L)
1143... // `std::atomic` is not value-initialized by default.
1144#endif
1145```
1146
1147**Description:** Provides a standardized way to test the toolchain's
1148implementation of a particular library feature.
1149
1150**Documentation:**
1151[Feature testing](https://en.cppreference.com/w/cpp/feature_test)
1152
1153**Notes:**
1154*** promo
1155None
1156***
1157
1158### &lt;numbers&gt; <sup>[allowed]</sup>
1159
1160```c++
1161#include <numbers>
1162```
1163
1164**Description:** Provides compile-time constants for many common mathematical
1165values, e.g. pi and e.
1166
1167**Documentation:**
1168[Mathematical constants](https://en.cppreference.com/w/cpp/numeric/constants)
1169
1170**Notes:**
1171*** promo
1172[Migration bug](https://crbug.com/1414635)
1173***
1174
1175### std::assume_aligned <sup>[allowed]</sup>
1176
1177```c++
1178void f(int* p) {
1179 int* aligned = std::assume_aligned<256>(p);
1180 ...
1181```
1182
1183**Description:** Informs the compiler that a pointer points to an address
1184aligned to at least some particular power of 2.
1185
1186**Documentation:**
1187[`std::assume_aligned`](https://en.cppreference.com/w/cpp/memory/assume_aligned)
1188
1189**Notes:**
1190*** promo
Peter Kastingb2887c32024-01-08 21:41:041191None
Peter Kastingf86817c2023-11-13 18:00:111192***
1193
1194### std::erase[_if] for containers <sup>[allowed]</sup>
1195
1196```c++
1197std::vector<int> numbers = ...;
1198std::erase_if(numbers, [](int x) { return x % 2 == 0; });
1199```
1200
1201**Description:** Erases from a container by value comparison or predicate,
1202avoiding the need to use the `erase(remove(...` paradigm.
1203
1204**Documentation:**
1205[`std::erase`, `std::erase_if` (`std::vector`)](https://en.cppreference.com/w/cpp/container/vector/erase2)
1206
1207**Notes:**
1208*** promo
1209[Migration bug](https://crbug.com/1414639)
1210***
1211
Peter Kastingfc838e82024-09-04 13:56:461212### std::hardware_{con,de}structive_interference_size <sup>[allowed]</sup>
1213
1214```c++
1215struct SharedData {
1216 ReadOnlyFrequentlyUsed data;
1217 alignas(std::hardware_destructive_interference_size) std::atomic<size_t> counter;
1218};
1219```
1220
1221**Description:** The `std::hardware_destructive_interference_size` constant is
1222useful to avoid false sharing (destructive interference) between variables that
1223would otherwise occupy the same cacheline. In contrast,
1224`std::hardware_constructive_interference_size` is helpful to promote true
1225sharing (constructive interference), e.g. to support better locality for
1226non-contended data.
1227
1228**Documentation:**
1229[`std::hardware_destructive_interference_size`](https://en.cppreference.com/w/cpp/thread/hardware_destructive_interference_size),
1230[`std::hardware_constructive_interference_size`](https://en.cppreference.com/w/cpp/thread/hardware_destructive_interference_size)
1231
1232**Notes:**
1233*** promo
1234[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/cwktrFxxUY4)
1235***
1236
Peter Kastingf86817c2023-11-13 18:00:111237### std::is_[un]bounded_array <sup>[allowed]</sup>
1238
1239```c++
1240template <typename T>
1241static constexpr bool kBoundedArray = std::is_bounded_array_v<T>;
1242```
1243
1244**Description:** Checks if a type is an array type with a known or unknown
1245bound.
1246
1247**Documentation:**
1248[`std::is_bounded_array`](https://en.cppreference.com/w/cpp/types/is_bounded_array),
1249[`std::is_unbounded_array`](https://en.cppreference.com/w/cpp/types/is_unbounded_array)
1250
1251**Notes:**
1252*** promo
1253None
1254***
1255
1256### std::lerp <sup>[allowed]</sup>
1257
1258```c++
1259double val = std::lerp(start, end, t);
1260```
1261
1262**Description:** Linearly interpolates (or extrapolates) between two values.
1263
1264**Documentation:**
1265[`std::lerp`](https://en.cppreference.com/w/cpp/numeric/lerp)
1266
1267**Notes:**
1268*** promo
1269[Migration bug](https://crbug.com/1414537)
1270***
1271
1272### std::make_obj_using_allocator etc. <sup>[allowed]</sup>
1273
1274```c++
1275auto obj = std::make_obj_using_allocator<Obj>(alloc, ...);
1276```
1277
1278**Description:** Constructs an object using
1279[uses-allocator construction](https://en.cppreference.com/w/cpp/memory/uses_allocator).
1280
1281**Documentation:**
1282[`std::make_obj_using_allocator`](https://en.cppreference.com/w/cpp/memory/make_obj_using_allocator)
1283
1284**Notes:**
1285*** promo
1286None
1287***
1288
1289### std::make_unique_for_overwrite <sup>[allowed]</sup>
1290
1291```c++
1292auto ptr = std::make_unique_for_overwrite<int>(); // `*ptr` is uninitialized
1293```
1294
1295**Description:** Like calling `std::unique_ptr<T>(new T)` instead of the more
1296typical `std::unique_ptr<T>(new T(...))`.
1297
1298**Documentation:**
1299[`std::make_unique`, `std::make_unique_for_overwrite`](https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique)
1300
1301**Notes:**
1302*** promo
1303None
1304***
1305
1306### std::midpoint <sup>[allowed]</sup>
1307
1308```c++
1309int center = std::midpoint(top, bottom);
1310```
1311
1312**Description:** Finds the midpoint between its two arguments, avoiding any
1313possible overflow. For integral inputs, rounds towards the first argument.
1314
1315**Documentation:**
1316[`std::midpoint`](https://en.cppreference.com/w/cpp/numeric/midpoint)
1317
1318**Notes:**
1319*** promo
1320[Migration bug](https://crbug.com/1414539)
1321***
1322
Peter Kasting8e2424c2024-10-22 16:21:551323### std::ranges::subrange <sup>[allowed]</sup>
1324
1325```c++
1326void transform(const std::multimap<int, char>& map, int key) {
1327 auto [first, last] = map.equal_range(key);
1328 for (const auto& [_, value] : std::ranges::subrange(first, last)) {
1329 ...
1330```
1331
1332**Description:** Creates a view from an iterator and a sentinel. Useful for
1333treating non-contiguous storage (e.g. a `std::map`) as a range.
1334
1335**Documentation:**
1336[`std::ranges::subrange`](https://en.cppreference.com/w/cpp/ranges/subrange)
1337
1338**Notes:**
1339*** promo
1340Prefer `base::span` if working with explicitly contiguous data, such as in a
1341`std::vector`. Use `std::ranges::subrange` when data is non-contiguous, or when
1342it's an implementation detail that the data is contiguous (e.g.
1343`base::flat_map`).
1344
1345[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/5VeU5GkPUYI)
1346***
1347
Peter Kastingf86817c2023-11-13 18:00:111348### std::remove_cvref[_t] <sup>[allowed]</sup>
1349
1350```c++
1351template <typename T,
1352 typename = std::enable_if_t<std::is_same_v<std::remove_cvref_t<T>,
1353 int>>>
1354void foo(T t);
1355```
1356
1357**Description:** Provides a way to remove const, volatile, and reference
1358qualifiers from a type.
1359
1360**Documentation:**
1361[`std::remove_cvref`](https://en.cppreference.com/w/cpp/types/remove_cvref)
1362
1363**Notes:**
1364*** promo
Peter Kastingb2887c32024-01-08 21:41:041365None
Peter Kastingf86817c2023-11-13 18:00:111366***
1367
1368### std::ssize <sup>[allowed]</sup>
1369
1370```c++
1371str.replace(it, it + std::ssize(substr), 1, 'x');
1372```
1373
1374**Description:** Returns the size of an object as a signed type.
1375
1376**Documentation:**
1377[`std::size`, `std::ssize`](https://en.cppreference.com/w/cpp/iterator/size)
1378
1379**Notes:**
1380*** promo
1381[Migration bug](https://crbug.com/1414543)
1382***
1383
1384### std::string::(starts,ends)_with <sup>[allowed]</sup>
1385
1386```c++
1387const std::string str = "Foo bar";
1388const bool is_true = str.ends_with("bar");
1389```
1390
1391**Description:** Tests whether a string starts or ends with a particular
1392character or string.
1393
1394**Documentation:**
1395[`std::basic_string<CharT,Traits,Allocator>::starts_with`](https://en.cppreference.com/w/cpp/string/basic_string/starts_with),
1396[`std::basic_string<CharT,Traits,Allocator>::ends_with`](https://en.cppreference.com/w/cpp/string/basic_string/ends_with)
1397
1398**Notes:**
1399*** promo
1400[Migration bug](https://crbug.com/1414647)
1401***
1402
Peter Kastingf86817c2023-11-13 18:00:111403## C++20 Banned Language Features {#core-blocklist-20}
1404
1405The following C++20 language features are not allowed in the Chromium codebase.
1406
Peter Kasting72d110f12024-02-06 19:45:261407### char8_t <sup>[banned]</sup>
1408
1409```c++
1410char8_t c = u8'x';
1411```
1412
1413**Description:** A single UTF-8 code unit. Similar to `unsigned char`, but
1414considered a distinct type.
1415
1416**Documentation:**
1417[Fundamental types](https://en.cppreference.com/w/cpp/language/types#char8_t)
1418
1419**Notes:**
1420*** promo
1421Use `char` and unprefixed character literals. Non-UTF-8 encodings are rare
1422enough in Chromium that the value of distinguishing them at the type level is
1423low, and `char8_t*` is not interconvertible with `char*` (what ~all Chromium,
1424STL, and platform-specific APIs use), so using `u8` prefixes would obligate us
1425to insert casts everywhere. If you want to declare at a type level that a block
1426of data is string-like and not an arbitrary binary blob, prefer
1427`std::string[_view]` over `char*`.
1428***
1429
Peter Kastingf86817c2023-11-13 18:00:111430### Modules <sup>[banned]</sup>
1431
1432```c++
1433export module helloworld; // module declaration
1434
1435import <iostream>; // import declaration
1436
1437export void hello() { // export declaration
1438 std::cout << "Hello world!\n";
1439}
1440```
1441
1442**Description:** Modules provide an alternative to many uses of headers which
1443allows for faster compilation, better tooling support, and reduction of problems
1444like "include what you use".
1445
1446**Documentation:**
1447[Modules](https://en.cppreference.com/w/cpp/language/modules)
1448
1449**Notes:**
1450*** promo
1451Not yet sufficiently supported in Clang and GN. Re-evaluate when support
1452improves.
1453***
1454
Peter Kasting8bc046d22023-11-14 00:38:031455### [[no_unique_address]] <sup>[banned]</sup>
1456
1457```c++
1458struct Empty {};
1459struct X {
1460 int i;
1461 [[no_unique_address]] Empty e;
1462};
1463```
1464
1465**Description:** Allows a data member to be overlapped with other members.
1466
1467**Documentation:**
1468[C++ attribute: `no_unique_address`](https://en.cppreference.com/w/cpp/language/attributes/no_unique_address)
1469
1470**Notes:**
1471*** promo
1472Has no effect on Windows, for compatibility with Microsoft's ABI. Use
1473`NO_UNIQUE_ADDRESS` from `base/compiler_specific.h` instead. Do not use (either
1474form) on members of unions due to
1475[potential memory safety problems](https://github.com/llvm/llvm-project/issues/60711).
Peter Kasting8bc046d22023-11-14 00:38:031476***
1477
Peter Kastingf86817c2023-11-13 18:00:111478## C++20 Banned Library Features {#library-blocklist-20}
1479
1480The following C++20 library features are not allowed in the Chromium codebase.
1481
1482### std::atomic_ref <sup>[banned]</sup>
1483
1484```c++
1485struct S { int a; int b; };
1486S not_atomic;
1487std::atomic_ref<S> is_atomic(not_atomic);
1488```
1489
1490**Description:** Allows atomic access to objects that might not themselves be
1491atomic types. While any atomic_ref to an object exists, the object must be
1492accessed exclusively through atomic_ref instances.
1493
1494**Documentation:**
1495[`std::atomic_ref`](https://en.cppreference.com/w/cpp/atomic/atomic_ref)
1496
1497**Notes:**
1498*** promo
1499Banned due to being [unimplemented in libc++](https://reviews.llvm.org/D72240).
1500
1501[Migration bug](https://crbug.com/1422701) (once this is allowed)
1502***
1503
1504### std::bind_front <sup>[banned]</sup>
1505
1506```c++
1507int minus(int a, int b);
1508auto fifty_minus_x = std::bind_front(minus, 50);
1509int forty = fifty_minus_x(10);
1510```
1511
1512**Description:** An updated version of `std::bind` with fewer gotchas, similar
1513to `absl::bind_front`.
1514
1515**Documentation:**
1516[`std::bind_front`, `std::bind_back`](https://en.cppreference.com/w/cpp/utility/functional/bind_front)
1517
1518**Notes:**
1519*** promo
1520Overlaps with `base::Bind`.
1521***
1522
Avi Drissman70cb7f72023-12-12 17:44:371523### std::bit_cast <sup>[banned]</sup>
1524
1525```c++
1526float quake_rsqrt(float number) {
1527 long i = std::bit_cast<long>(number);
1528 i = 0x5f3759df - (i >> 1); // wtf?
1529 float y = std::bit_cast<float>(i);
1530 return y * (1.5f - (0.5f * number * y * y));
1531}
1532```
1533
1534**Description:** Returns an value constructed with the same bits as an value of
1535a different type.
1536
1537**Documentation:**
1538[`std::bit_cast`](https://en.cppreference.com/w/cpp/numeric/bit_cast)
1539
1540**Notes:**
1541*** promo
1542The `std::` version of `bit_cast` allows casting of pointer and reference types,
1543which is both useless in that it doesn't avoid UB, and dangerous in that it
1544allows arbitrary casting away of modifiers like `const`. Instead of using
1545`bit_cast` on pointers, use standard C++ casts. For use on values, use
1546`base::bit_cast` which does not allow this unwanted usage.
1547***
1548
Peter Kastingf86817c2023-11-13 18:00:111549### std::{c8rtomb,mbrtoc8} <sup>[banned]</sup>
1550
1551```c++
1552std::u8string_view strv = u8"zß水🍌";
1553std::mbstate_t state;
1554char out[MB_LEN_MAX] = {0};
1555for (char8_t c : strv) {
1556 size_t rc = std::c8rtomb(out, c, &state);
1557 ...
1558```
1559
1560**Description:** Converts a code point between UTF-8 and a multibyte character
1561encoded using the current C locale.
1562
1563**Documentation:**
1564[`std::c8rtomb`](https://en.cppreference.com/w/cpp/string/multibyte/c8rtomb),
1565[`std::mbrtoc8`](https://en.cppreference.com/w/cpp/string/multibyte/mbrtoc8)
1566
1567**Notes:**
1568*** promo
1569Chromium functionality should not vary with the C locale.
1570***
1571
Peter Kasting8e2424c2024-10-22 16:21:551572### Range factories and range adaptors <sup>[banned]</sup>
Daniel Cheng89719222024-07-04 04:59:291573
1574```c++
Daniel Cheng89719222024-07-04 04:59:291575// Prints 1, 2, 3, 4, 5, 6.
1576for (auto i : std::ranges::iota_view(1, 7)) {
1577 std::cout << i << '\n';
1578}
Peter Kasting8e2424c2024-10-22 16:21:551579
1580constexpr int kArr[] = {6, 2, 8, 4, 4, 2};
1581constexpr auto plus_one = std::views::transform([](int n){ return n + 1; });
1582static_assert(std::ranges::equal(kArr | plus_one, {7, 3, 9, 5, 5, 3}));
Daniel Cheng89719222024-07-04 04:59:291583```
1584
1585**Description:** Lightweight objects that represent iterable sequences.
1586Provides facilities for lazy operations on ranges, along with composition into
1587pipelines.
1588
1589**Documentation:**
1590[Ranges library](https://en.cppreference.com/w/cpp/ranges)
1591
1592**Notes:**
1593*** promo
1594Banned in Chrome due to questions about the design, impact on build time, and
1595runtime performance.
1596
1597[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ZnIbkfJ0Glw)
1598***
1599
Peter Kasting8e2424c2024-10-22 16:21:551600### std::ranges::view_interface <sup>[banned]</sup>
1601
1602```c++
1603class MyView : public std::ranges::view_interface<MyView> { ... };
1604```
1605
1606**Description:** CRTP base class for implementing custom view objects.
1607
1608**Documentation:**
1609[`std::ranges::view_interface`](https://en.cppreference.com/w/cpp/ranges/view_interface)
1610
1611**Notes:**
1612*** promo
1613Banned in Chrome since range factories and adapters are banned, and this would
1614primarily allow authors to create similar functionality.
1615
1616[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ZnIbkfJ0Glw)
1617***
1618
Peter Kastinge73b89d2024-11-26 19:35:521619### &lt;span&gt; <sup>[banned]</sup>
1620
1621```c++
1622#include <span>
1623```
1624
1625**Description:** Utilities for non-owning views over a sequence of objects.
1626
1627**Documentation:**
1628[](https://en.cppreference.com/w/cpp/header/span)
1629
1630**Notes:**
1631*** promo
1632Superseded by `base::span`, which has a richer functionality set.
1633***
1634
Nick Diego Yamanee522ae82024-02-27 04:23:221635### std::to_address <sup>[banned]</sup>
1636
1637```c++
1638std::vector<int> numbers;
1639int* i = std::to_address(numbers.begin());
1640```
1641
1642**Description:** Converts a pointer-like object to a pointer, even if the
1643pointer does not refer to a constructed object (in which case an expression like
1644`&*p` is UB).
1645
1646**Documentation:**
1647[`std::to_address`](https://en.cppreference.com/w/cpp/memory/to_address)
1648
1649**Notes:**
1650*** promo
1651Banned because it is not guaranteed to be SFINAE-compatible. Use
1652base::to_address, which does guarantee this.
1653***
1654
Peter Kastingf86817c2023-11-13 18:00:111655### &lt;syncstream&gt; <sup>[banned]</sup>
1656
1657```c++
1658#include <syncstream>
1659```
1660
1661**Description:** Facilities for multithreaded access to streams.
1662
1663**Documentation:**
1664[Standard library header `<syncstream>`](https://en.cppreference.com/w/cpp/header/syncstream)
1665
1666**Notes:**
1667*** promo
1668Banned due to being unimplemented per
1669[the libc++ C++20 status page](https://libcxx.llvm.org/Status/Cxx20.html).
1670Reevaluate usefulness once implemented.
1671***
1672
1673## C++20 TBD Language Features {#core-review-20}
1674
1675The following C++20 language features are not allowed in the Chromium codebase.
1676See the top of this page on how to propose moving a feature from this list into
1677the allowed or banned sections.
1678
1679### Aggregate initialization using parentheses <sup>[tbd]</sup>
1680
1681```c++
1682struct B {
1683 int a;
1684 int&& r;
1685} b2(1, 1); // Warning: dangling reference
1686```
1687
1688**Description:** Allows initialization of aggregates using parentheses, not just
1689braces.
1690
1691**Documentation:**
1692[Aggregate initialization](https://en.cppreference.com/w/cpp/language/aggregate_initialization),
1693[Direct initialization](https://en.cppreference.com/w/cpp/language/direct_initialization)
1694
1695**Notes:**
1696*** promo
1697There are subtle but important differences between brace- and paren-init of
1698aggregates. The parenthesis style appears to have more pitfalls (allowing
1699narrowing conversions, not extending lifetimes of temporaries bound to
1700references).
1701***
1702
Peter Kastingf86817c2023-11-13 18:00:111703### Coroutines <sup>[tbd]</sup>
1704
1705```c++
1706co_return 1;
1707```
1708
1709**Description:** Allows writing functions that logically block while physically
1710returning control to a caller. This enables writing some kinds of async code in
1711simple, straight-line ways without storing state in members or binding
1712callbacks.
1713
1714**Documentation:**
1715[Coroutines](https://en.cppreference.com/w/cpp/language/coroutines)
1716
1717**Notes:**
1718*** promo
1719Requires significant support code and planning around API and migration.
1720
1721[Prototyping bug](https://crbug.com/1403840)
1722***
1723
Peter Kastingf86817c2023-11-13 18:00:111724## C++20 TBD Library Features {#library-review-20}
1725
1726The following C++20 library features are not allowed in the Chromium codebase.
1727See the top of this page on how to propose moving a feature from this list into
1728the allowed or banned sections.
1729
1730### &lt;coroutine&gt; <sup>[tbd]</sup>
1731
1732```c++
1733#include <coroutine>
1734```
1735
1736**Description:** Header which defines various core coroutine types.
1737
1738**Documentation:**
1739[Coroutine support](https://en.cppreference.com/w/cpp/coroutine)
1740
1741**Notes:**
1742*** promo
1743See notes on "Coroutines" above.
1744***
1745
1746### &lt;format&gt; <sup>[tbd]</sup>
1747
1748```c++
1749std::cout << std::format("Hello {}!\n", "world");
1750```
1751
1752**Description:** Utilities for producing formatted strings.
1753
1754**Documentation:**
1755[Formatting library](https://en.cppreference.com/w/cpp/utility/format)
1756
1757**Notes:**
1758*** promo
1759Has both pros and cons compared to `absl::StrFormat` (which we don't yet use).
1760Migration would be nontrivial.
1761***
1762
Peter Kastingf86817c2023-11-13 18:00:111763### &lt;source_location&gt; <sup>[tbd]</sup>
1764
1765```c++
1766#include <source_location>
1767```
1768
1769**Description:** Provides a class that can hold source code details such as
1770filenames, function names, and line numbers.
1771
1772**Documentation:**
1773[Standard library header `<source_location>`](https://en.cppreference.com/w/cpp/header/source_location)
1774
1775**Notes:**
1776*** promo
1777Seems to regress code size vs. `base::Location`.
1778***
1779
Peter Kastingf86817c2023-11-13 18:00:111780### std::u8string <sup>[tbd]</sup>
1781
1782```c++
1783std::u8string str = u8"Foo";
1784```
1785
1786**Description:** A string whose character type is `char8_t`, intended to hold
1787UTF-8-encoded text.
1788
1789**Documentation:**
1790[`std::basic_string`](https://en.cppreference.com/w/cpp/string/basic_string)
1791
1792**Notes:**
1793*** promo
1794See notes on `char8_t` above.
1795***
1796
Joe Masonfe4f2562021-09-15 15:23:131797## Abseil Banned Library Features {#absl-blocklist}
1798
1799The following Abseil library features are not allowed in the Chromium codebase.
1800
danakja6f71cb12021-12-15 21:04:491801### Any <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:131802
1803```c++
1804absl::any a = int{5};
1805EXPECT_THAT(absl::any_cast<int>(&a), Pointee(5));
1806EXPECT_EQ(absl::any_cast<size_t>(&a), nullptr);
1807```
1808
1809**Description:** Early adaptation of C++17 `std::any`.
1810
1811**Documentation:** [std::any](https://en.cppreference.com/w/cpp/utility/any)
1812
1813**Notes:**
1814*** promo
Peter Kasting72d110f12024-02-06 19:45:261815Banned since workaround for lack of RTTI
1816[isn't compatible with the component build](https://crbug.com/1096380). See also
1817`std::any`.
Joe Masonfe4f2562021-09-15 15:23:131818***
1819
Peter Kasting7fb5fbf2025-01-30 18:45:251820### AnyInvocable <sup>[banned]</sup>
1821
1822```c++
1823absl::AnyInvocable
1824```
1825
1826**Description:** An equivalent of the C++23 std::move_only_function.
1827
1828**Documentation:**
1829* [any_invocable.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/any_invocable.h)
1830* [std::move_only_function](https://en.cppreference.com/w/cpp/utility/functional/move_only_function/move_only_function)
1831
1832**Notes:**
1833*** promo
1834Banned due to overlap with `base::RepeatingCallback`, `base::OnceCallback`.
1835***
1836
Peter Kasting4b18d0c2024-09-18 00:56:111837### Attributes <sup>[banned]</sup>
1838
1839```c++
1840T* data() ABSL_ATTRIBUTE_LIFETIME_BOUND { return data_; }
1841ABSL_ATTRIBUTE_NO_TAIL_CALL ReturnType Loop();
1842struct S { bool b; int32_t i; } ABSL_ATTRIBUTE_PACKED;
1843```
1844
1845**Description:** Cross-platform macros to expose compiler-specific
1846functionality.
1847
1848**Documentation:** [attributes.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/base/attributes.h)
1849
1850**Notes:**
1851*** promo
1852Long names discourage use. Use standardized attributes over macros where
1853possible, and otherwise prefer shorter alternatives in
1854`base/compiler_specific.h`.
1855
1856[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/lVQOJTng1RU)
1857***
1858
John Admanski3bb2c1f2025-01-24 18:28:391859### btree_\* containers <sup>[banned]</sup>
1860
1861```c++
1862absl::btree_map
1863absl::btree_set
1864absl::btree_multimap
1865absl::btree_multiset
1866```
1867
1868**Description:** Alternatives to the tree-based standard library containers
1869designed to be more efficient in the general case.
1870
1871**Documentation:** [Containers](https://abseil.io/docs/cpp/guides/container)
1872
1873**Notes:**
1874*** promo
1875In theory these should be superior alternatives that could replace most uses of
1876`std::map` and company. In practice they have been found to introduce a
1877substantial code size increase. Until this problem can be resolved the use of
1878these containers is banned. Use the standard library containers instead.
1879***
1880
Peter Kasting4f35bfc2022-10-18 18:39:121881### bind_front <sup>[banned]</sup>
1882
1883```c++
1884absl::bind_front
1885```
1886
Peter Kasting3b77a0c2024-08-22 00:22:261887**Description:** Binds the first N arguments of an invocable object and stores
1888them by value.
Peter Kasting4f35bfc2022-10-18 18:39:121889
1890**Documentation:**
1891* [bind_front.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/bind_front.h)
1892* [Avoid std::bind](https://abseil.io/tips/108)
1893
1894**Notes:**
1895*** promo
Peter Kasting7fb5fbf2025-01-30 18:45:251896Banned due to overlap with `base::Bind`.
Peter Kasting4f35bfc2022-10-18 18:39:121897***
1898
danakja6f71cb12021-12-15 21:04:491899### Command line flags <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:131900
1901```c++
1902ABSL_FLAG(bool, logs, false, "print logs to stderr");
1903app --logs=true;
1904```
1905
1906**Description:** Allows programmatic access to flag values passed on the
1907command-line to binaries.
1908
1909**Documentation:** [Flags Library](https://abseil.io/docs/cpp/guides/flags)
1910
1911**Notes:**
1912*** promo
Peter Kasting72d110f12024-02-06 19:45:261913Banned since workaround for lack of RTTI
1914[isn't compatible with the component build](https://crbug.com/1096380). Use
1915`base::CommandLine` instead.
Joe Masonfe4f2562021-09-15 15:23:131916***
1917
Peter Kasting4f35bfc2022-10-18 18:39:121918### Container utilities <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:131919
1920```c++
Peter Kasting4f35bfc2022-10-18 18:39:121921auto it = absl::c_find(container, value);
Joe Masonfe4f2562021-09-15 15:23:131922```
1923
Peter Kasting4f35bfc2022-10-18 18:39:121924**Description:** Container-based versions of algorithmic functions within C++
1925standard library.
Joe Masonfe4f2562021-09-15 15:23:131926
Peter Kasting4f35bfc2022-10-18 18:39:121927**Documentation:**
1928[container.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/algorithm/container.h)
Joe Masonfe4f2562021-09-15 15:23:131929
1930**Notes:**
1931*** promo
Peter Kasting7fb5fbf2025-01-30 18:45:251932Superseded by algorithms in `std::ranges::`.
Joe Masonfe4f2562021-09-15 15:23:131933***
1934
Peter Kasting431239a2023-09-29 03:11:441935### FixedArray <sup>[banned]</sup>
1936
1937```c++
1938absl::FixedArray<MyObj> objs_;
1939```
1940
1941**Description:** A fixed size array like `std::array`, but with size determined
1942at runtime instead of compile time.
1943
1944**Documentation:**
1945[fixed_array.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/container/fixed_array.h)
1946
1947**Notes:**
1948*** promo
1949Direct construction is banned due to the risk of UB with uninitialized
1950trivially-default-constructible types. Instead use `base/types/fixed_array.h`,
1951which is a light-weight wrapper that deletes the problematic constructor.
Joe Mason6a6f2582024-01-30 20:26:431952***
Peter Kasting431239a2023-09-29 03:11:441953
Daniel Cheng2248b332022-07-27 06:16:591954### FunctionRef <sup>[banned]</sup>
1955
1956```c++
1957absl::FunctionRef
1958```
1959
1960**Description:** Type for holding a non-owning reference to an object of any
1961invocable type.
1962
1963**Documentation:**
1964[function_ref.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/function_ref.h)
1965
1966**Notes:**
1967*** promo
1968- `absl::FunctionRef` is banned due to allowing implicit conversions between
1969 function signatures in potentially surprising ways. For example, a callable
1970 with the signature `int()` will bind to `absl::FunctionRef<void()>`: the
1971 return value from the callable will be silently discarded.
1972- In Chromium, use `base::FunctionRef` instead.
1973- Unlike `base::OnceCallback` and `base::RepeatingCallback`, `base::FunctionRef`
1974 supports capturing lambdas.
1975- Useful when passing an invocable object to a function that synchronously calls
1976 the invocable object, e.g. `ForEachFrame(base::FunctionRef<void(Frame&)>)`.
1977 This can often result in clearer code than code that is templated to accept
1978 lambdas, e.g. with `template <typename Invocable> void
1979 ForEachFrame(Invocable invocable)`, it is much less obvious what arguments
1980 will be passed to `invocable`.
1981- For now, `base::OnceCallback` and `base::RepeatingCallback` intentionally
1982 disallow conversions to `base::FunctionRef`, under the theory that the
1983 callback should be a capturing lambda instead. Attempting to use this
1984 conversion will trigger a `static_assert` requesting additional feedback for
1985 use cases where this conversion would be valuable.
1986- *Important:* `base::FunctionRef` must not outlive the function call. Like
Helmut Januschka1dce9dc2024-06-11 13:05:351987 `std::string_view`, `base::FunctionRef` is a *non-owning* reference. Using a
Daniel Cheng2248b332022-07-27 06:16:591988 `base::FunctionRef` as a return value or class field is dangerous and likely
1989 to result in lifetime bugs.
Peter Kasting72d110f12024-02-06 19:45:261990
1991[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/JVN4E4IIYA0)
Daniel Cheng2248b332022-07-27 06:16:591992***
1993
Peter Kasting7fb5fbf2025-01-30 18:45:251994### Log macros and related classes <sup>[banned]</sup>
1995
1996```c++
1997LOG(INFO) << message;
1998CHECK(condition);
1999absl::AddLogSink(&custom_sink_to_capture_absl_logs);
2000```
2001
2002**Description:** Macros and related classes to perform debug loggings
2003
2004**Documentation:**
2005[log.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/log/log.h)
2006[check.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/log/check.h)
2007
2008**Notes:**
2009*** promo
2010Banned due to overlap with `base/logging.h`. We'd like to drop Chromium's
2011version and replace with the Abseil one, but no one has looked into how to
2012migrate and what impacts (e.g. build time) we'd incur. If you'd like to do this
2013work, please contact cxx@.
2014***
2015
2016### NoDestructor <sup>[banned]</sup>
2017
2018```c++
2019// Global or namespace scope.
2020ABSL_CONST_INIT absl::NoDestructor<MyRegistry> reg{"foo", "bar", 8008};
2021
2022// Function scope.
2023const std::string& MyString() {
2024 static const absl::NoDestructor<std::string> x("foo");
2025 return *x;
2026}
2027```
2028
2029**Description:** `absl::NoDestructor<T>` is a wrapper around an object of
2030type T that behaves as an object of type T but never calls T's destructor.
2031
2032**Documentation:**
2033[no_destructor.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/base/no_desctructor.h)
2034
2035**Notes:**
2036*** promo
2037Overlaps with `base::NoDestructor`. Banned pending rewriting friending of that
2038class into a form usable with this (see
2039[crbug.com/392931072](https://crbug.com/392931072)); at that point we can allow
2040this and migrate to it.
2041***
2042
2043### Nullability annotations <sup>[banned]</sup>
2044
2045```c++
2046void PaySalary(absl::NotNull<Employee *> employee) {
2047 pay(*employee); // OK to dereference
2048}
2049```
2050
2051**Description:** Annotations to more clearly specify contracts
2052
2053**Documentation:**
2054[nullability.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/base/nullability.h)
2055
2056**Notes:**
2057*** promo
2058Banned due to no feasible path to codebase-wide use and little mechanism for
2059enforcement.
2060***
2061
Peter Kasting3b77a0c2024-08-22 00:22:262062### Optional <sup>[banned]</sup>
2063
2064```c++
2065absl::optional<int> Func(bool b) {
2066 return b ? absl::make_optional(1) : abl::nullopt;
2067}
2068```
2069
2070**Description:** Early adaptation of C++17 `std::optional`.
2071
2072**Documentation:** [std::optional](https://en.cppreference.com/w/cpp/utility/optional)
2073
2074**Notes:**
2075*** promo
2076Superseded by `std::optional`. Use `std::optional` instead.
2077***
2078
Peter Kasting4f35bfc2022-10-18 18:39:122079### Random <sup>[banned]</sup>
2080
2081```c++
2082absl::BitGen bitgen;
2083size_t index = absl::Uniform(bitgen, 0u, elems.size());
2084```
2085
2086**Description:** Functions and utilities for generating pseudorandom data.
2087
2088**Documentation:** [Random library](https://abseil.io/docs/cpp/guides/random)
2089
2090**Notes:**
2091*** promo
2092Banned because most uses of random values in Chromium should be using a
2093cryptographically secure generator. Use `base/rand_util.h` instead.
2094***
2095
2096### Span <sup>[banned]</sup>
2097
2098```c++
2099absl::Span
2100```
2101
2102**Description:** Early adaptation of C++20 `std::span`.
2103
2104**Documentation:** [Using absl::Span](https://abseil.io/tips/93)
2105
2106**Notes:**
2107*** promo
2108Banned due to being less std::-compliant than `base::span`. Keep using
2109`base::span`.
2110***
2111
2112### StatusOr <sup>[banned]</sup>
2113
2114```c++
2115absl::StatusOr<T>
2116```
2117
2118**Description:** An object that is either a usable value, or an error Status
2119explaining why such a value is not present.
2120
2121**Documentation:**
2122[statusor.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/status/statusor.h)
2123
2124**Notes:**
2125*** promo
Peter Kasting72d110f12024-02-06 19:45:262126Overlaps with `base::expected`.
Peter Kasting4f35bfc2022-10-18 18:39:122127***
2128
Peter Kasting4f35bfc2022-10-18 18:39:122129### string_view <sup>[banned]</sup>
2130
2131```c++
2132absl::string_view
2133```
2134
2135**Description:** Early adaptation of C++17 `std::string_view`.
2136
2137**Documentation:** [absl::string_view](https://abseil.io/tips/1)
2138
2139**Notes:**
2140*** promo
David Benjaminea985a22023-04-18 22:05:012141Originally banned due to only working with 8-bit characters. Now it is
2142unnecessary because, in Chromium, it is the same type as `std::string_view`.
Hong Xu5e492d32023-07-27 21:38:462143Please use `std::string_view` instead.
Peter Kasting4f35bfc2022-10-18 18:39:122144***
2145
2146### Strings Library <sup>[banned]</sup>
2147
2148```c++
2149absl::StrSplit
2150absl::StrJoin
2151absl::StrCat
2152absl::StrAppend
2153absl::Substitute
2154absl::StrContains
2155```
2156
2157**Description:** Classes and utility functions for manipulating and comparing
2158strings.
2159
2160**Documentation:**
2161[String Utilities](https://abseil.io/docs/cpp/guides/strings)
2162
2163**Notes:**
2164*** promo
Peter Kasting72d110f12024-02-06 19:45:262165Overlaps with `base/strings`. We
Peter Kasting4f35bfc2022-10-18 18:39:122166[should re-evalute](https://bugs.chromium.org/p/chromium/issues/detail?id=1371966)
2167when we've
2168[migrated](https://bugs.chromium.org/p/chromium/issues/detail?id=691162) from
Peter Kasting0efc9042024-09-17 15:48:432169`base::StringPiece` to `std::string_view`. Also note that `absl::StrFormat()` is
2170not considered part of this group, and is explicitly allowed.
Peter Kasting4f35bfc2022-10-18 18:39:122171***
2172
2173### Synchronization <sup>[banned]</sup>
2174
2175```c++
2176absl::Mutex
2177```
2178
2179**Description:** Primitives for managing tasks across different threads.
2180
2181**Documentation:**
2182[Synchronization](https://abseil.io/docs/cpp/guides/synchronization)
2183
2184**Notes:**
2185*** promo
Peter Kasting72d110f12024-02-06 19:45:262186Overlaps with `base/synchronization/`. We would love
Peter Kasting4f35bfc2022-10-18 18:39:122187[more testing](https://bugs.chromium.org/p/chromium/issues/detail?id=1371969) on
2188whether there are compelling reasons to prefer base, absl, or std
2189synchronization primitives; for now, use `base/synchronization/`.
2190***
2191
2192### Time library <sup>[banned]</sup>
2193
2194```c++
2195absl::Duration
2196absl::Time
2197absl::TimeZone
2198absl::CivilDay
2199```
2200
2201**Description:** Abstractions for holding time values, both in terms of
2202absolute time and civil time.
2203
2204**Documentation:** [Time](https://abseil.io/docs/cpp/guides/time)
2205
2206**Notes:**
2207*** promo
Peter Kasting72d110f12024-02-06 19:45:262208Overlaps with `base/time/`.
Peter Kasting4f35bfc2022-10-18 18:39:122209***