blob: 5b212cb473b35f4fed12f00ec9193830277a2b81 [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
14toolchain support is sufficient, a standard is declared "initially supported",
15with new language/library features banned pending discussion.
16
17You can propose changing the status of a feature by sending an email to
18[[email protected]](https://groups.google.com/a/chromium.org/forum/#!forum/cxx).
19Include a short blurb on what the feature is and why you think it should or
20should not be allowed, along with links to any relevant previous discussion. If
21the list arrives at some consensus, send a codereview to change this file
22accordingly, linking to your discussion thread.
23
24If an item remains on the TBD list two years after initial support is added,
25style arbiters should explicitly move it to an appropriate allowlist or
26blocklist, allowing it if there are no obvious reasons to ban.
27
28The current status of existing standards and Abseil features is:
29
30* **C++11:** _Default allowed; see banned features below_
31* **C++14:** _Default allowed; see banned features below_
Peter Kasting1865f2772021-12-23 21:23:5832* **C++17:** Initially supported December 23, 2021; see allowed/banned/TBD
33 features below
34* **C++20:** _Not yet supported in Chromium_
35* **C++23:** _Not yet standardized_
Joe Masonfe4f2562021-09-15 15:23:1336* **Abseil:** Initially supported July 31, 2020; see allowed/banned/TBD
37 features below
38 * absl::StatusOr: Initially supported September 3, 2020
39 * absl::Cleanup: Initially supported February 4, 2021
40
41[TOC]
42
Peter Kasting1865f2772021-12-23 21:23:5843## C++11 Banned Language Features {#core-blocklist-11}
Joe Masonfe4f2562021-09-15 15:23:1344
45The following C++11 language features are not allowed in the Chromium codebase.
46
danakja6f71cb12021-12-15 21:04:4947### Inline Namespaces <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:1348
49```c++
50inline namespace foo { ... }
51```
52
53**Description:** Allows better versioning of namespaces.
54
55**Documentation:**
56[Inline namespaces](https://en.cppreference.com/w/cpp/language/namespace#Inline_namespaces)
57
58**Notes:**
59*** promo
60Banned in the
61[Google Style Guide](https://google.github.io/styleguide/cppguide.html#Namespaces).
62Unclear how it will work with components.
63***
64
danakja6f71cb12021-12-15 21:04:4965### long long Type <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:1366
67```c++
68long long var = value;
69```
70
71**Description:** An integer of at least 64 bits.
72
73**Documentation:**
74[Fundamental types](https://en.cppreference.com/w/cpp/language/types)
75
76**Notes:**
77*** promo
78Use a stdint.h type if you need a 64-bit number.
79[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/chromium-dev/RxugZ-pIDxk)
80***
81
danakja6f71cb12021-12-15 21:04:4982### User-Defined Literals <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:1383
84```c++
85type var = literal_value_type;
86```
87
88**Description:** Allows user-defined literal expressions.
89
90**Documentation:**
91[User-defined literals](https://en.cppreference.com/w/cpp/language/user_literal)
92
93**Notes:**
94*** promo
95Banned in the
96[Google Style Guide](https://google.github.io/styleguide/cppguide.html#Operator_Overloading).
97***
98
danakja6f71cb12021-12-15 21:04:4999### thread_local Storage Class <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13100
101```c++
102thread_local int foo = 1;
103```
104
105**Description:** Puts variables into thread local storage.
106
107**Documentation:**
108[Storage duration](https://en.cppreference.com/w/cpp/language/storage_duration)
109
110**Notes:**
111*** promo
112Some surprising effects on Mac
113([discussion](https://groups.google.com/a/chromium.org/forum/#!topic/chromium-dev/2msN8k3Xzgs),
114[fork](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/h7O5BdtWCZw)).
115Use `base::SequenceLocalStorageSlot` for sequence support, and
116`base::ThreadLocal`/`base::ThreadLocalStorage` otherwise.
117***
118
Peter Kasting1865f2772021-12-23 21:23:58119## C++11 Banned Library Features {#library-blocklist-11}
Joe Masonfe4f2562021-09-15 15:23:13120
121The following C++11 library features are not allowed in the Chromium codebase.
122
danakja6f71cb12021-12-15 21:04:49123### Bind Operations <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13124
125```c++
126std::bind(function, args, ...)
127```
128
129**Description:** Declares a function object bound to certain arguments
130
131**Documentation:**
132[std::bind](https://en.cppreference.com/w/cpp/utility/functional/bind)
133
134**Notes:**
135*** promo
136Use `base::Bind` instead. Compared to `std::bind`, `base::Bind` helps prevent
137lifetime issues by preventing binding of capturing lambdas and by forcing
138callers to declare raw pointers as `Unretained`.
139[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/SoEj7oIDNuA)
140***
141
danakja6f71cb12021-12-15 21:04:49142### C Floating-Point Environment <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13143
144```c++
145#include <cfenv>
146#include <fenv.h>
147```
148
149**Description:** Provides floating point status flags and control modes for
150C-compatible code
151
152**Documentation:**
153[Standard library header "cfenv"](https://en.cppreference.com/w/cpp/header/cfenv)
154
155**Notes:**
156*** promo
157Banned by the
158[Google Style Guide](https://google.github.io/styleguide/cppguide.html#C++11)
159due to concerns about compiler support.
160***
161
danakja6f71cb12021-12-15 21:04:49162### Date and time utilities <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13163
164```c++
165#include <chrono>
166```
167
168**Description:** A standard date and time library
169
170**Documentation:**
171[Date and time utilities](https://en.cppreference.com/w/cpp/chrono)
172
173**Notes:**
174*** promo
175Overlaps with `Time` APIs in `base/`. Keep using the `base/` classes.
176***
177
danakja6f71cb12021-12-15 21:04:49178### Exceptions <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13179
180```c++
181#include <exception>
182```
183
184**Description:** Enhancements to exception throwing and handling
185
186**Documentation:**
187[Standard library header "exception"](https://en.cppreference.com/w/cpp/header/exception)
188
189**Notes:**
190*** promo
191Exceptions are banned by the
192[Google Style Guide](https://google.github.io/styleguide/cppguide.html#Exceptions)
193and disabled in Chromium compiles. However, the `noexcept` specifier is
194explicitly allowed.
195[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/chromium-dev/8i4tMqNpHhg)
196***
197
danakja6f71cb12021-12-15 21:04:49198### Function Objects <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13199
200```c++
201std::function
202```
203
204**Description:** Wraps a standard polymorphic function
205
206**Documentation:**
207[std::function](https://en.cppreference.com/w/cpp/utility/functional/function)
208
209**Notes:**
210*** promo
211Use `base::{Once,Repeating}Callback` instead. Compared to `std::function`,
212`base::{Once,Repeating}Callback` directly supports Chromium's refcounting
213classes and weak pointers and deals with additional thread safety concerns.
214[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/SoEj7oIDNuA)
215***
216
danakja6f71cb12021-12-15 21:04:49217### Random Number Engines <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13218
219*** aside
220The random number engines defined in `<random>` (see separate item for random
221number distributions), e.g.: `linear_congruential_engine`,
222`mersenne_twister_engine`, `minstd_rand0`, `mt19937`, `ranlinux48`,
223`random_device`
224***
225
226**Description:** Random number generation algorithms and utilities.
227
228**Documentation:**
229[Pseudo-random number generation](https://en.cppreference.com/w/cpp/numeric/random)
230
231**Notes:**
232*** promo
233Do not use any random number engines from `<random>`. Instead, use
234`base::RandomBitGenerator`.
235[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/16Xmw05C-Y0)
236***
237
danakja6f71cb12021-12-15 21:04:49238### Ratio Template Class <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13239
240```c++
241std::ratio<numerator, denominator>
242```
243
244**Description:** Provides compile-time rational numbers
245
246**Documentation:**
247[std::ratio](https://en.cppreference.com/w/cpp/numeric/ratio/ratio)
248
249**Notes:**
250*** promo
251Banned by the
252[Google Style Guide](https://google.github.io/styleguide/cppguide.html#C++11)
253due to concerns that this is tied to a more template-heavy interface style.
254***
255
danakja6f71cb12021-12-15 21:04:49256### Regular Expressions <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13257
258```c++
259#include <regex>
260```
261
262**Description:** A standard regular expressions library
263
264**Documentation:**
265[Regular expressions library](https://en.cppreference.com/w/cpp/regex)
266
267**Notes:**
268*** promo
269Overlaps with many regular expression libraries in Chromium. When in doubt, use
270`re2`.
271***
272
danakja6f71cb12021-12-15 21:04:49273### Shared Pointers <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13274
275```c++
276std::shared_ptr
277```
278
279**Description:** Allows shared ownership of a pointer through reference counts
280
281**Documentation:**
282[std::shared_ptr](https://en.cppreference.com/w/cpp/memory/shared_ptr)
283
284**Notes:**
285*** promo
286Needs a lot more evaluation for Chromium, and there isn't enough of a push for
287this feature.
288
289* [Google Style Guide](https://google.github.io/styleguide/cppguide.html#Ownership_and_Smart_Pointers).
290* [Discussion Thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/aT2wsBLKvzI)
291***
292
danakja6f71cb12021-12-15 21:04:49293### String-Number Conversion Functions <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13294
295```c++
296std::stoi()
297std::stol()
298std::stoul()
299std::stoll
300std::stoull()
301std::stof()
302std::stod()
303std::stold()
304std::to_string()
305```
306
307**Description:** Converts strings to/from numbers
308
309**Documentation:**
310* [std::stoi, std::stol, std::stoll](https://en.cppreference.com/w/cpp/string/basic_string/stol),
311* [std::stoul, std::stoull](https://en.cppreference.com/w/cpp/string/basic_string/stoul),
312* [std::stof, std::stod, std::stold](https://en.cppreference.com/w/cpp/string/basic_string/stof),
313* [std::to_string](https://en.cppreference.com/w/cpp/string/basic_string/to_string)
314
315**Notes:**
316*** promo
317The string-to-number conversions rely on exceptions to communicate failure,
318while the number-to-string conversions have performance concerns and depend on
319the locale. Use the routines in `base/strings/string_number_conversions.h`
320instead.
321***
322
danakja6f71cb12021-12-15 21:04:49323### Thread Library <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13324
325*** aside
326`<thread>` and related headers, including `<future>`, `<mutex>`,
327`<condition_variable>`
328***
329
330**Description:** Provides a standard multithreading library using `std::thread`
331and associates
332
333**Documentation:**
334[Thread support library](https://en.cppreference.com/w/cpp/thread)
335
336**Notes:**
337*** promo
338Overlaps with many classes in `base/`. Keep using the `base/` classes for now.
339`base::Thread` is tightly coupled to `MessageLoop` which would make it hard to
340replace. We should investigate using standard mutexes, or unique_lock, etc. to
341replace our locking/synchronization classes.
342***
343
danakja6f71cb12021-12-15 21:04:49344### Weak Pointers <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13345
346```c++
347std::weak_ptr
348```
349
350**Description:** Allows a weak reference to a `std::shared_ptr`
351
352**Documentation:**
353[std::weak_ptr](https://en.cppreference.com/w/cpp/memory/weak_ptr)
354
355**Notes:**
356*** promo
357Banned because `std::shared_ptr` is banned. Use `base::WeakPtr` instead.
358***
359
360## C++14 Banned Library Features {#library-blocklist-14}
361
362The following C++14 library features are not allowed in the Chromium codebase.
363
danakja6f71cb12021-12-15 21:04:49364### std::chrono literals <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:13365
366```c++
367using namespace std::chrono_literals;
368auto timeout = 30s;
369```
370
371**Description:** Allows `std::chrono` types to be more easily constructed.
372
373**Documentation:**
374[std::literals::chrono_literals::operator""s](https://en.cppreference.com/w/cpp/chrono/operator%22%22s)
375
376**Notes:**
377*** promo
378Banned because `<chrono>` is banned.
379***
380
Peter Kasting1865f2772021-12-23 21:23:58381## C++17 Allowed Language Features {#core-allowlist-17}
382
383The following C++17 language features are allowed in the Chromium codebase.
384
385### Template argument deduction for class templates <sup>[allowed]</sup>
386
387```c++
388template <typename T>
389struct MyContainer {
390 MyContainer(T val) : val{val} {}
391 // ...
392};
393MyContainer c1(1); // Type deduced to be `int`.
394```
395
396**Description:** Automatic template argument deduction much like how it's done
397for functions, but now including class constructors.
398
399**Documentation:**
400[Class template argument deduction](https://en.cppreference.com/w/cpp/language/class_template_argument_deduction)
401
402**Notes:**
403*** promo
404Usage is governed by the
405[Google Style Guide](https://google.github.io/styleguide/cppguide.html#CTAD).
406***
407
Andrew Rayskiy6ce944d2022-01-04 18:13:33408### Selection statements with initializer <sup>[allowed]</sup>
409
410```c++
411if (int a = Func(); a < 3) { ...
412switch (int a = Func(); a) { ...
413```
414
415**Description:** New versions of the if and switch statements which simplify
416common code patterns and help users keep scopes tight.
417
418**Documentation:**
419[if statement](https://en.cppreference.com/w/cpp/language/if),
420[switch statement](https://en.cppreference.com/w/cpp/language/switch)
421
422**Notes:**
423*** promo
424[@cxx discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/4GP43nftePE)
425***
426
Roland Bock767858e2022-01-04 20:25:21427### fallthrough <sup>[allowed]</sup>
428
429```c++
430case 1:
431 DoSomething();
432 [[fallthrough]];
433case 2:
434 break;
435```
436
437**Description:**
438The `[[fallthrough]]` attribute can be used in switch statements to indicate
439when intentionally falling through to the next case.
440
441**Documentation:**
442[C++ attribute: fallthrough](https://en.cppreference.com/w/cpp/language/attributes/fallthrough)
443
444**Notes:**
445*** promo
446See [discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/JrvyFd243QI).
447
448See [migration task](https://bugs.chromium.org/p/chromium/issues/detail?id=1283907).
449***
450
Peter Kasting1865f2772021-12-23 21:23:58451## C++17 Allowed Library Features {#library-allowlist-17}
452
453The following C++17 language features are allowed in the Chromium codebase.
454
455### Allocation functions with explicit alignment <sup>[allowed]</sup>
456
457```c++
458class alignas(32) Vec3d {
459 double x, y, z;
460};
461auto p_vec = new Vec3d[10]; // 32-byte aligned in C++17, maybe not previously
462```
463
464**Description:** Performs heap allocation of objects whose alignment
465requirements exceed `__STDCPP_DEFAULT_NEW_ALIGNMENT__`.
466
467**Documentation:**
468[operator new](https://en.cppreference.com/w/cpp/memory/new/operator_new)
469
470**Notes:**
471*** promo
472None
473***
474
475## C++17 Banned Library Features {#library-blocklist-17}
476
477The following C++17 library features are not allowed in the Chromium codebase.
478
479### std::filesystem <sup>[banned]</sup>
480
481```c++
482#include <filesystem>
483```
484
485**Description:** A standard way to manipulate files, directories, and paths in a
486filesystem.
487
488**Documentation:**
489[Filesystem library](https://en.cppreference.com/w/cpp/filesystem)
490
491**Notes:**
492*** promo
493Banned by the [Google Style Guide](https://google.github.io/styleguide/cppguide.html#Other_Features).
494***
495
496### weak_from_this <sup>[banned]</sup>
497
498```c++
499auto weak_ptr = weak_from_this();
500```
501
502**Description:** Returns a `std::weak_ptr<T>` that tracks ownership of `*this`
503by all existing `std::shared_ptr`s that refer to `*this`.
504
505**Documentation:**
506[std::enable_shared_from_this<T>::weak_from_this](https://en.cppreference.com/w/cpp/memory/enable_shared_from_this/weak_from_this)
507
508**Notes:**
509*** promo
510Banned since `std::shared_ptr` and `std::weak_ptr` are banned.
511***
512
513### Transparent std::owner_less <sup>[banned]</sup>
514
515```c++
516std::map<std::weak_ptr<T>, U, std::owner_less<>>
517```
518
519**Description:** Function object providing mixed-type owner-based ordering of
520shared and weak pointers, regardless of the type of the pointee.
521
522**Documentation:**
523[std::owner_less](https://en.cppreference.com/w/cpp/memory/owner_less)
524
525**Notes:**
526*** promo
527Banned since `std::shared_ptr` and `std::weak_ptr` are banned.
528***
529
530### Array support for std::shared_ptr <sup>[banned]</sup>
531
532```c++
533std::shared_ptr<int[]> p(new int[10]{0,1,2,3,4,5,6,7,8,9});
534std::cout << p[3]; // "3"
535```
536
537**Description:** Supports memory management of arrays via `std::shared_ptr`.
538
539**Documentation:**
540[std::shared_ptr](https://en.cppreference.com/w/cpp/memory/shared_ptr)
541
542**Notes:**
543*** promo
544Banned since `std::shared_ptr` is banned.
545***
546
547### std::uncaught_exceptions <sup>[banned]</sup>
548
549```c++
550int count = std::uncaught_exceptions();
551```
552
553**Description:** Determines whether there are live exception objects.
554
555**Documentation:**
556[std::uncaught_exceptions](https://en.cppreference.com/w/cpp/error/uncaught_exception)
557
558**Notes:**
559*** promo
560Banned because exceptions are banned.
561***
562
563### Rounding functions for duration and time_point <sup>[banned]</sup>
564
565```c++
566std::chrono::ceil<std::chrono::seconds>(dur);
567std::chrono::ceil<std::chrono::seconds>(time_pt);
568std::chrono::floor<std::chrono::seconds>(dur);
569std::chrono::floor<std::chrono::seconds>(time_pt);
570std::chrono::round<std::chrono::seconds>(dur);
571std::chrono::round<std::chrono::seconds>(time_pt);
572```
573
574**Description:** Converts durations and time_points by rounding.
575
576**Documentation:**
577[std::chrono::duration](https://en.cppreference.com/w/cpp/chrono/duration),
578[std::chrono::time_point](https://en.cppreference.com/w/cpp/chrono/time_point)
579
580**Notes:**
581*** promo
582Banned since `std::chrono` is banned.
583***
584
585## C++17 TBD Language Features {#core-review-17}
586
587The following C++17 language features are not allowed in the Chromium codebase.
588See the top of this page on how to propose moving a feature from this list into
589the allowed or banned sections.
590
591### Declaring non-type template parameters with auto <sup>[tbd]</sup>
592
593```c++
594template <auto... seq>
595struct my_integer_sequence {
596 // ...
597};
598auto seq = my_integer_sequence<0, 1, 2>(); // Type deduced to be `int`.
599```
600
601**Description:** Following the deduction rules of `auto`, while respecting the
602non-type template parameter list of allowable types, template arguments can be
603deduced from the types of its arguments.
604
605**Documentation:**
606[Template parameters](https://en.cppreference.com/w/cpp/language/template_parameters)
607
608**Notes:**
609*** promo
610None
611***
612
613### Fold expressions <sup>[tbd]</sup>
614
615```c++
616template <typename... Args>
617auto sum(Args... args) {
618 return (... + args);
619}
620```
621
622**Description:** A fold expression performs a fold of a template parameter pack
623over a binary operator.
624
625**Documentation:**
626[Fold expression](https://en.cppreference.com/w/cpp/language/fold)
627
628**Notes:**
629*** promo
630None
631***
632
633### constexpr lambda <sup>[tbd]</sup>
634
635```c++
636auto identity = [](int n) constexpr { return n; };
637static_assert(identity(123) == 123);
638```
639
640**Description:** Compile-time lambdas using constexpr.
641
642**Documentation:**
643[Lambda expressions](https://en.cppreference.com/w/cpp/language/lambda)
644
645**Notes:**
646*** promo
647None
648***
649
650### Lambda capture this by value <sup>[tbd]</sup>
651
652```c++
653const auto l = [*this] { return member_; }
654```
655
656**Description:** `*this` captures the current object by copy, while `this`
657continues to capture by reference.
658
659**Documentation:**
660[Lambda capture](https://en.cppreference.com/w/cpp/language/lambda#Lambda_capture)
661
662**Notes:**
663*** promo
664None
665***
666
667### Inline variables <sup>[tbd]</sup>
668
669```c++
670struct S {
671 static constexpr int kZero = 0; // constexpr implies inline here
672};
673```
674
675**Description:** The `inline` specifier can be applied to variables as well as
676to functions. A variable declared inline has the same semantics as a function
677declared inline. It can also be used to declare and define a static member
678variable, such that it does not need to be initialized in the source file.
679
680**Documentation:**
681[inline specifier](https://en.cppreference.com/w/cpp/language/inline)
682
683**Notes:**
684*** promo
685None
686***
687
688### Nested namespaces <sup>[tbd]</sup>
689
690```c++
691namespace A::B::C { ...
692```
693
694**Description:** Using the namespace resolution operator to create nested
695namespace definitions.
696
697**Documentation:**
698[Namespaces](https://en.cppreference.com/w/cpp/language/namespace)
699
700**Notes:**
701*** promo
702None
703***
704
705### Structured bindings <sup>[tbd]</sup>
706
707```c++
708const auto [x, y] = FuncReturningStdPair();
709```
710
711**Description:** Allows writing `auto [x, y, z] = expr;` where the type of
712`expr` is a tuple-like object, whose elements are bound to the variables `x`,
713`y`, and `z` (which this construct declares). Tuple-like objects include
714`std::tuple`, `std::pair`, `std::array`, and aggregate structures.
715
716**Documentation:**
717[Structured binding declaration](https://en.cppreference.com/w/cpp/language/structured_binding)
718
719**Notes:**
720*** promo
721None
722***
723
Peter Kasting1865f2772021-12-23 21:23:58724### constexpr if <sup>[tbd]</sup>
725
726```c++
727if constexpr (cond) { ...
728```
729
730**Description:** Write code that is instantiated depending on a compile-time
731condition.
732
733**Documentation:**
734[if statement](https://en.cppreference.com/w/cpp/language/if)
735
736**Notes:**
737*** promo
738None
739***
740
741### UTF-8 character literals <sup>[tbd]</sup>
742
743```c++
744char x = u8'x';
745```
746
747**Description:** A character literal that begins with `u8` is a character
748literal of type `char`. The value of a UTF-8 character literal is equal to its
749ISO 10646 code point value.
750
751**Documentation:**
752[Character literal](https://en.cppreference.com/w/cpp/language/character_literal)
753
754**Notes:**
755*** promo
756C++20 changes the type to `char8_t`, causing migration hazards for code using
757this.
758***
759
Roland Bock767858e2022-01-04 20:25:21760### nodiscard attribute <sup>[tbd]</sup>
Peter Kasting1865f2772021-12-23 21:23:58761
762```c++
Peter Kasting1865f2772021-12-23 21:23:58763[[nodiscard]] bool do_something() { ...
764```
765
Roland Bock767858e2022-01-04 20:25:21766**Description:** C++17 introduces the `[[nodiscard]]` attribute.
Peter Kasting1865f2772021-12-23 21:23:58767
768**Documentation:**
Peter Kasting1865f2772021-12-23 21:23:58769[C++ attribute: nodiscard](https://en.cppreference.com/w/cpp/language/attributes/nodiscard)
770
771**Notes:**
772*** promo
Roland Bock767858e2022-01-04 20:25:21773See similar attribute macro in base/compiler_specific.h.
774***
775
776### maybe_unused attribute <sup>[tbd]</sup>
777
778```c++
779[[maybe_unused]] int x;
780```
781
782**Description:** C++17 introduces the `[[maybe_unused]]` attribute.
783
784**Documentation:**
785[C++ attribute: maybe_unused](https://en.cppreference.com/w/cpp/language/attributes/maybe_unused),
786
787**Notes:**
788*** promo
789See similar attribute macro in base/compiler_specific.h.
Peter Kasting1865f2772021-12-23 21:23:58790***
791
792### using declaration for attributes <sup>[tbd]</sup>
793
794```c++
795[[using CC: opt(1), debug]] // same as [[CC:opt(1), CC::debug]]
796```
797
798**Description:** Specifies a common namespace for a list of attributes.
799
800**Documentation:**
801[Attribute specifier sequence](https://en.cppreference.com/w/cpp/language/attributes)
802
803**Notes:**
804*** promo
805See similar attribute macros in base/compiler_specific.h.
806***
807
808### __has_include <sup>[tbd]</sup>
809
810```c++
811#if __has_include(<optional>) ...
812```
813
814**Description:** Checks whether a file is available for inclusion, i.e. the file
815exists.
816
817**Documentation:**
818[Source file inclusion](https://en.cppreference.com/w/cpp/preprocessor/include)
819
820**Notes:**
821*** promo
822None
823***
824
825## C++17 TBD Library Features {#library-review-17}
826
827The following C++17 library features are not allowed in the Chromium codebase.
828See the top of this page on how to propose moving a feature from this list into
829the allowed or banned sections.
830
831### std::variant <sup>[tbd]</sup>
832
833```c++
834std::variant<int, double> v = 12;
835```
836
837**Description:** The class template `std::variant` represents a type-safe
838`union`. An instance of `std::variant` at any given time holds a value of one of
839its alternative types (it's also possible for it to be valueless).
840
841**Documentation:**
842[std::variant](https://en.cppreference.com/w/cpp/utility/variant)
843
844**Notes:**
845*** promo
846See also `absl::variant`.
847***
848
849### std::optional <sup>[tbd]</sup>
850
851```c++
852std::optional<std::string> s;
853```
854
855**Description:** The class template `std::optional` manages an optional
856contained value, i.e. a value that may or may not be present. A common use case
857for optional is the return value of a function that may fail.
858
859**Documentation:**
860[std::optional](https://en.cppreference.com/w/cpp/utility/optional)
861
862**Notes:**
863*** promo
864See also `absl::optional`.
865***
866
867### std::any <sup>[tbd]</sup>
868
869```c++
870std::any x = 5;
871```
872
873**Description:** A type-safe container for single values of any type.
874
875**Documentation:**
876[std::any](https://en.cppreference.com/w/cpp/utility/any)
877
878**Notes:**
879*** promo
880See also `absl::any`.
881***
882
883### std::string_view <sup>[tbd]</sup>
884
885```c++
886std::string_view str = "foo";
887```
888
889**Description:** A non-owning reference to a string. Useful for providing an
890abstraction on top of strings (e.g. for parsing).
891
892**Documentation:**
893[std::basic_string_view](https://en.cppreference.com/w/cpp/string/basic_string_view)
894
895**Notes:**
896*** promo
897See also `absl::string_view` and `base::StringPiece`.
898***
899
900### std::invoke <sup>[tbd]</sup>
901
902```c++
903static_assert(std::invoke(std::plus<>(), 1, 2) == 3);
904```
905
906**Description:** Invokes a `Callable` object with parameters. An example of a
907`Callable` object is `base::Callback` where an object can be called similarly to
908a regular function.
909
910**Documentation:**
911[std::invoke](https://en.cppreference.com/w/cpp/utility/functional/invoke)
912
913**Notes:**
914*** promo
915See also `base::invoke`.
916***
917
918### std::apply <sup>[tbd]</sup>
919
920```c++
921static_assert(std::apply(std::plus<>(), std::make_tuple(1, 2)) == 3);
922```
923
924**Description:** Invokes a `Callable` object with a tuple of arguments.
925
926**Documentation:**
927[std::apply](https://en.cppreference.com/w/cpp/utility/apply)
928
929**Notes:**
930*** promo
931See also `absl::apply` and `base::apply`.
932***
933
934### std::byte <sup>[tbd]</sup>
935
936```c++
937std::byte b = 0xFF;
938int i = std::to_integer<int>(b); // 0xFF
939```
940
941**Description:** A standard way of representing data as a byte. `std::byte` is
942neither a character type nor an arithmetic type, and the only operator overloads
943available are bitwise operations.
944
945**Documentation:**
946[std::byte](https://en.cppreference.com/w/cpp/types/byte)
947
948**Notes:**
949*** promo
950None
951***
952
953### Splicing for maps and sets <sup>[tbd]</sup>
954
955```c++
956std::map<...>::extract
957std::map<...>::merge
958std::set<...>::extract
959std::set<...>::merge
960```
961
962**Description:** Moving nodes and merging containers without the overhead of
963expensive copies, moves, or heap allocations/deallocations.
964
965**Documentation:**
966[std::map::extract](https://en.cppreference.com/w/cpp/container/map/extract),
967[std::map::merge](https://en.cppreference.com/w/cpp/container/map/merge)
968
969**Notes:**
970*** promo
971None
972***
973
974### Parallel algorithms <sup>[tbd]</sup>
975
976```c++
977auto it = std::find(std::execution::par, std::begin(vec), std::end(vec), 2);
978```
979
980**Description:** Many of the STL algorithms, such as the `copy`, `find` and
981`sort` methods, now support the parallel execution policies: `seq`, `par`, and
982`par_unseq` which translate to "sequentially", "parallel" and
983"parallel unsequenced".
984
985**Documentation:**
986[execution_policy_tag_t](https://en.cppreference.com/w/cpp/algorithm/execution_policy_tag_t)
987
988**Notes:**
989*** promo
990None
991***
992
993### std::make_from_tuple <sup>[tbd]</sup>
994
995```c++
996// Calls Foo(int, double):
997auto foo = std::make_from_tuple<Foo>(std::make_tuple(1, 3.5));
998```
999
1000**Description:** Constructs an object from a tuple of arguments.
1001
1002**Documentation:**
1003[std::make_from_tuple](https://en.cppreference.com/w/cpp/utility/make_from_tuple)
1004
1005**Notes:**
1006*** promo
1007See also `absl::make_from_tuple`.
1008***
1009
1010### Searchers <sup>[tbd]</sup>
1011
1012```c++
1013auto it = std::search(haystack.begin(), haystack.end(),
1014 std::boyer_moore_searcher(needle.begin(), needle.end()));
1015```
1016
1017**Description:** Alternate string searching algorithms.
1018
1019**Documentation:**
1020[Searchers](https://en.cppreference.com/w/cpp/utility/functional#Searchers)
1021
1022**Notes:**
1023*** promo
1024None
1025***
1026
1027### std::as_const <sup>[tbd]</sup>
1028
1029```c++
1030auto&& const_ref = std::as_const(mutable_obj);
1031```
1032
1033**Description:** Forms reference to const T.
1034
1035**Documentation:**
1036[std::as_const](https://en.cppreference.com/w/cpp/utility/as_const)
1037
1038**Notes:**
1039*** promo
1040See also `base::as_const`.
1041***
1042
1043### std::not_fn <sup>[tbd]</sup>
1044
1045```c++
1046auto nonwhite = std::find_if(str.begin(), str.end(), std::not_fn(IsWhitespace));
1047```
1048
1049**Description:** Creates a forwarding call wrapper that returns the negation of
1050the callable object it holds.
1051
1052**Documentation:**
1053[std::not_fn](https://en.cppreference.com/w/cpp/utility/functional/not_fn)
1054
1055**Notes:**
1056*** promo
1057See also `base::not_fn`.
1058***
1059
1060### Uninitialized memory algorithms <sup>[tbd]</sup>
1061
1062```c++
1063std::destroy_at(ptr);
1064std::destroy(ptr, ptr + 8);
1065std::destroy_n(ptr, 8);
1066std::uninitialized_move(src.begin(), src.end(), dest.begin());
1067std::uninitialized_value_construct(std::begin(storage), std::end(storage));
1068```
1069
1070**Description:** Replaces direct constructor and destructor calls when manually
1071managing memory.
1072
1073**Documentation:**
1074[std::destroy_at](https://en.cppreference.com/w/cpp/memory/destroy_at),
1075[std::destroy](https://en.cppreference.com/w/cpp/memory/destroy),
1076[std::destroy_n](https://en.cppreference.com/w/cpp/memory/destroy_n),
1077[std::uninitialized_move](https://en.cppreference.com/w/cpp/memory/uninitialized_move),
1078[std::uninitialized_value_construct](https://en.cppreference.com/w/cpp/memory/uninitialized_value_construct)
1079
1080**Notes:**
1081*** promo
1082None
1083***
1084
1085### std::pmr::memory_resource and std::polymorphic_allocator <sup>[tbd]</sup>
1086
1087```c++
1088#include <memory_resource>
1089```
1090
1091**Description:** Manages memory allocations using runtime polymorphism.
1092
1093**Documentation:**
1094[std::pmr::memory_resource](https://en.cppreference.com/w/cpp/memory/memory_resource),
1095[std::pmr::polymorphic_allocator](https://en.cppreference.com/w/cpp/memory/polymorphic_allocator),
1096
1097**Notes:**
1098*** promo
1099May not be supported in libc++, according to the
1100[library features table](https://en.cppreference.com/w/cpp/17)
1101***
1102
1103### std::aligned_alloc <sup>[tbd]</sup>
1104
1105```c++
1106int* p2 = static_cast<int*>(std::aligned_alloc(1024, 1024));
1107```
1108
1109**Description:** Allocates uninitialized storage with the specified alignment.
1110
1111**Documentation:**
1112[std::aligned_alloc](https://en.cppreference.com/w/cpp/memory/c/aligned_alloc)
1113
1114**Notes:**
1115*** promo
1116None
1117***
1118
1119### std::conjunction/std::disjunction/std::negation <sup>[tbd]</sup>
1120
1121```c++
1122template<typename T, typename... Ts>
1123std::enable_if_t<std::conjunction_v<std::is_same<T, Ts>...>>
1124func(T, Ts...) { ...
1125```
1126
1127**Description:** Performs logical operations on type traits.
1128
1129**Documentation:**
1130[std::conjunction](https://en.cppreference.com/w/cpp/types/conjunction),
1131[std::disjunction](https://en.cppreference.com/w/cpp/types/disjunction),
1132[std::negation](https://en.cppreference.com/w/cpp/types/negation)
1133
1134**Notes:**
1135*** promo
1136None
1137***
1138
1139### Type trait variable templates <sup>[tbd]</sup>
1140
1141```c++
1142bool b = std::is_same_v<int, std::int32_t>;
1143```
1144
1145**Description:** Syntactic sugar to provide convenient access to `::value`
1146members by simply adding `_v`.
1147
1148**Documentation:**
1149[Type support](https://en.cppreference.com/w/cpp/types)
1150
1151**Notes:**
1152*** promo
1153None
1154***
1155
1156### std::is_swappable <sup>[tbd]</sup>
1157
1158```c++
1159std::is_swappable<T>
1160std::is_swappable_with_v<T, U>
1161```
1162
1163**Description:** Checks whether classes may be swapped.
1164
1165**Documentation:**
1166[std::is_swappable](https://en.cppreference.com/w/cpp/types/is_swappable)
1167
1168**Notes:**
1169*** promo
1170None
1171***
1172
1173### std::is_invocable <sup>[tbd]</sup>
1174
1175```c++
1176std::is_invocable_v<Fn, 1, "Hello">
1177```
1178
1179**Description:** Checks whether a function may be invoked with the given
1180argument types. The `_r` variant also evaluates whether the result is
1181convertible to a given type.
1182
1183**Documentation:**
1184[std::is_invocable](https://en.cppreference.com/w/cpp/types/is_invocable)
1185
1186**Notes:**
1187*** promo
1188None
1189***
1190
1191### std::is_aggregate <sup>[tbd]</sup>
1192
1193```c++
1194if constexpr(std::is_aggregate_v<T>) { ...
1195```
1196
1197**Description:** Checks wither the given type is an aggregate type.
1198
1199**Documentation:**
1200[std::is_aggregate](https://en.cppreference.com/w/cpp/types/is_aggregate)
1201
1202**Notes:**
1203*** promo
1204None
1205***
1206
1207### std::has_unique_object_representations <sup>[tbd]</sup>
1208
1209```c++
1210std::has_unique_object_representations_v<foo>
1211```
1212
1213**Description:** Checks wither the given type is trivially copyable and any two
1214objects with the same value have the same object representation.
1215
1216**Documentation:**
1217[std::has_unique_object_representations](https://en.cppreference.com/w/cpp/types/has_unique_object_representations)
1218
1219**Notes:**
1220*** promo
1221None
1222***
1223
1224### std::clamp <sup>[tbd]</sup>
1225
1226```c++
1227int x = base::clamp(inp, 0, 100);
1228```
1229
1230**Description:** Clamps a value between a minimum and a maximum.
1231
1232**Documentation:**
1233[std::clamp](https://en.cppreference.com/w/cpp/algorithm/clamp)
1234
1235**Notes:**
1236*** promo
1237See also `base::clamp`.
1238***
1239
1240### std::reduce <sup>[tbd]</sup>
1241
1242```c++
1243std::reduce(std::execution::par, v.cbegin(), v.cend());
1244```
1245
1246**Description:** Like `std::accumulate` except the elements of the range may be
1247grouped and rearranged in arbitrary order.
1248
1249**Documentation:**
1250[std::reduce](https://en.cppreference.com/w/cpp/algorithm/reduce)
1251
1252**Notes:**
1253*** promo
1254Makes the most sense in conjunction with `std::execution::par`.
1255***
1256
1257### std::inclusive_scan <sup>[tbd]</sup>
1258
1259```c++
1260std::inclusive_scan(data.begin(), data.end(), output.begin());
1261```
1262
1263**Description:** Like `std::accumulate` but writes the result at each step into
1264the output range.
1265
1266**Documentation:**
1267[std::inclusive_scan](https://en.cppreference.com/w/cpp/algorithm/inclusive_scan)
1268
1269**Notes:**
1270*** promo
1271None
1272***
1273
1274### std::exclusive_scan <sup>[tbd]</sup>
1275
1276```c++
1277std::exclusive_scan(data.begin(), data.end(), output.begin());
1278```
1279
1280**Description:** Like `std::inclusive_scan` but omits the current element from
1281the written output at each step; that is, results are "one value behind" those
1282of `std::inclusive_scan`.
1283
1284**Documentation:**
1285[std::exclusive_scan](https://en.cppreference.com/w/cpp/algorithm/exclusive_scan)
1286
1287**Notes:**
1288*** promo
1289None
1290***
1291
1292### std::gcd <sup>[tbd]</sup>
1293
1294```c++
1295static_assert(std::gcd(12, 18) == 6);
1296```
1297
1298**Description:** Computes the greatest common divisor of its arguments.
1299
1300**Documentation:**
1301[std::gcd](https://en.cppreference.com/w/cpp/numeric/gcd)
1302
1303**Notes:**
1304*** promo
1305None
1306***
1307
1308### std::lcm <sup>[tbd]</sup>
1309
1310```c++
1311static_assert(std::lcm(12, 18) == 36);
1312```
1313
1314**Description:** Computes the least common multiple of its arguments.
1315
1316**Documentation:**
1317[std::lcm](https://en.cppreference.com/w/cpp/numeric/lcm)
1318
1319**Notes:**
1320*** promo
1321None
1322***
1323
1324### std::map::try_emplace <sup>[tbd]</sup>
1325
1326```c++
1327std::map<std::string, std::string> m;
1328m.try_emplace("c", 10, 'c');
1329m.try_emplace("c", "Won't be inserted");
1330```
1331
1332**Description:** Like `emplace`, but does not move from rvalue arguments if the
1333insertion does not happen.
1334
1335**Documentation:**
1336[std::map::try_emplace](https://en.cppreference.com/w/cpp/container/map/try_emplace),
1337
1338**Notes:**
1339*** promo
1340Already available on `base::flat_map`.
1341***
1342
1343### std::map::insert_or_assign <sup>[tbd]</sup>
1344
1345```c++
1346std::map<std::string, std::string> m;
1347m.insert_or_assign("c", "cherry");
1348m.insert_or_assign("c", "clementine");
1349```
1350
1351**Description:** Like `operator[]`, but returns more information and does not
1352require default-constructibility of the mapped type.
1353
1354**Documentation:**
1355[std::map::insert_or_assign](https://en.cppreference.com/w/cpp/container/map/insert_or_assign)
1356
1357**Notes:**
1358*** promo
1359Already available on `base::flat_map`.
1360***
1361
1362### Non-member std::size/std::empty/std::data <sup>[tbd]</sup>
1363
1364```c++
1365for (std::size_t i = 0; i < std::size(c); ++i) { ...
1366if (!std::empty(c)) { ...
1367std::strcpy(arr, std::data(str));
1368```
1369
1370**Description:** Non-member versions of what are normally member functions, for
1371symmetrical use with things like arrays and initializer_lists.
1372
1373**Documentation:**
1374[std::size](https://en.cppreference.com/w/cpp/iterator/size),
1375[std::empty](https://en.cppreference.com/w/cpp/iterator/empty),
1376[std::data](https://en.cppreference.com/w/cpp/iterator/data)
1377
1378**Notes:**
1379*** promo
1380See `base::size`, `base::empty`, and `base::data`.
1381***
1382
1383### Mathematical special functions <sup>[tbd]</sup>
1384
1385```c++
1386std::assoc_laguerre()
1387std::assoc_legendre()
1388std::beta()
1389std::comp_ellint_1()
1390std::comp_ellint_2()
1391std::comp_ellint_3()
1392std::cyl_bessel_i()
1393std::cyl_bessel_j()
1394std::cyl_bessel_k()
1395std::cyl_neumann()
1396std::ellint_1()
1397std::ellint_2()
1398std::ellint_3()
1399std::expint()
1400std::hermite()
1401std::legendre()
1402std::laguerre()
1403std::riemann_zeta()
1404std::sph_bessel()
1405std::sph_legendre()
1406std::sph_neumann()
1407```
1408
1409**Description:** A variety of mathematical functions.
1410
1411**Documentation:**
1412[Mathematical special functions](https://en.cppreference.com/w/cpp/numeric/special_functions)
1413
1414**Notes:**
1415*** promo
1416May not be supported in libc++, according to the
1417[library features table](https://en.cppreference.com/w/cpp/17)
1418***
1419
1420### 3D std::hypot <sup>[tbd]</sup>
1421
1422```c++
1423double dist = std::hypot(1.0, 2.5, 3.7);
1424```
1425
1426**Description:** Computes the distance from the origin in 3D space.
1427
1428**Documentation:**
1429[std::hypot](https://en.cppreference.com/w/cpp/numeric/math/hypot)
1430
1431**Notes:**
1432*** promo
1433None
1434***
1435
1436### Cache line interface <sup>[tbd]</sup>
1437
1438```c++
1439alignas(std::hardware_destructive_interference_size) std::atomic<int> cat;
1440static_assert(sizeof(S) <= std::hardware_constructive_interference_size);
1441```
1442
1443**Description:** A portable way to access the L1 data cache line size.
1444
1445**Documentation:**
1446[Hardware interference size](https://en.cppreference.com/w/cpp/thread/hardware_destructive_interference_size)
1447
1448**Notes:**
1449*** promo
1450May not be supported in libc++, according to the
1451[library features table](https://en.cppreference.com/w/cpp/17)
1452***
1453
1454### std::launder <sup>[tbd]</sup>
1455
1456```c++
1457struct Y { int z; };
1458alignas(Y) std::byte s[sizeof(Y)];
1459Y* q = new(&s) Y{2};
1460const int h = std::launder(reinterpret_cast<Y*>(&s))->z;
1461```
1462
1463**Description:** When used to wrap a pointer, makes it valid to access the
1464resulting object in cases it otherwise wouldn't have been, in a very limited set
1465of circumstances.
1466
1467**Documentation:**
1468[std::launder](https://en.cppreference.com/w/cpp/utility/launder)
1469
1470**Notes:**
1471*** promo
1472None
1473***
1474
1475### std::to_chars/std::from_chars <sup>[tbd]</sup>
1476
1477```c++
1478std::to_chars(str.data(), str.data() + str.size(), 42);
1479std::from_chars(str.data(), str.data() + str.size(), result);
1480```
1481
1482**Description:** Locale-independent, non-allocating, non-throwing functions to
1483convert values to/from character strings, designed for use in high-throughput
1484contexts.
1485
1486**Documentation:**
1487[std::to_chars](https://en.cppreference.com/w/cpp/utility/to_chars),
1488[std::from_chars](https://en.cppreference.com/w/cpp/utility/from_chars)
1489
1490**Notes:**
1491*** promo
1492None
1493***
1494
1495### std::atomic<T>::is_always_lock_free <sup>[tbd]</sup>
1496
1497```c++
1498template <typename T>
1499struct is_lock_free_impl
1500: std::integral_constant<bool, std::atomic<T>::is_always_lock_free> {};
1501```
1502
1503**Description:** True when the given atomic type is always lock-free.
1504
1505**Documentation:**
1506[std::atomic<T>::is_always_lock_free](https://en.cppreference.com/w/cpp/atomic/atomic/is_always_lock_free)
1507
1508**Notes:**
1509*** promo
1510None
1511***
1512
1513### std::scoped_lock <sup>[tbd]</sup>
1514
1515```c++
1516std::scoped_lock lock(e1.m, e2.m);
1517```
1518
1519**Description:** Provides an RAII-style mechanism for owning one or more mutexes
1520for the duration of a scoped block.
1521
1522**Documentation:**
1523[std::scoped_lock](https://en.cppreference.com/w/cpp/thread/scoped_lock)
1524
1525**Notes:**
1526*** promo
1527See also `base::AutoLock`.
1528***
1529
1530### std::timespec_get <sup>[tbd]</sup>
1531
1532```c++
1533std::timespec ts;
1534std::timespec_get(&ts, TIME_UTC);
1535```
1536
1537**Description:** Gets the current calendar time in the given time base.
1538
1539**Documentation:**
1540[std::timespec_get](https://en.cppreference.com/w/cpp/chrono/c/timespec_get)
1541
1542**Notes:**
1543*** promo
1544None
1545***
1546
Joe Masonfe4f2562021-09-15 15:23:131547## Abseil Allowed Library Features {#absl-allowlist}
1548
1549The following Abseil library features are allowed in the Chromium codebase.
1550
danakja6f71cb12021-12-15 21:04:491551### Optional <sup>[allowed]</sup>
Joe Masonfe4f2562021-09-15 15:23:131552
1553```c++
1554absl::optional
1555```
1556
1557**Description:** Early adaptation of C++17 `std::optional`.
1558
1559**Documentation:**
1560[std::optional](https://en.cppreference.com/w/cpp/utility/optional)
1561
1562**Notes:**
1563*** promo
1564Replaces `base::Optional`.
1565[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/zUGqagX1NFU)
1566***
1567
danakja6f71cb12021-12-15 21:04:491568### Status <sup>[allowed]</sup>
Joe Masonfe4f2562021-09-15 15:23:131569
1570```c++
1571absl::Status
1572```
1573
1574**Description:** Type for returning detailed errors.
1575
1576**Documentation:**
1577[status.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/status/status.h)
1578
1579**Notes:**
1580*** promo
1581Approved for use inside a wrapper type. Use
1582[abseil_string_conversions.h](https://source.chromium.org/chromium/chromium/src/+/main:base/strings/abseil_string_conversions.h)
1583to convert to and from
1584[absl::string_view](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/strings/string_view.h)
1585so the wrapper can expose
1586[base::StringPiece](https://source.chromium.org/chromium/chromium/src/+/main:base/strings/string_piece.h).
1587Use
1588[absl::Cord](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/strings/cord.h)
1589directly as minimally necessary to interface; do not expose in the wrapper type
1590API.
1591
1592[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ImdFCSZ-NMA)
1593***
1594
danakja6f71cb12021-12-15 21:04:491595### Variant <sup>[allowed]</sup>
Joe Masonfe4f2562021-09-15 15:23:131596
1597```c++
1598absl::variant
1599```
1600
1601**Description:** Early adaptation of C++17 `std::variant`.
1602
1603**Documentation:**
1604[std::variant](https://en.cppreference.com/w/cpp/utility/variant)
1605
1606**Notes:**
1607*** promo
1608[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/DqvG-TpvMyU)
1609***
1610
1611## Abseil Banned Library Features {#absl-blocklist}
1612
1613The following Abseil library features are not allowed in the Chromium codebase.
1614
danakja6f71cb12021-12-15 21:04:491615### Any <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:131616
1617```c++
1618absl::any a = int{5};
1619EXPECT_THAT(absl::any_cast<int>(&a), Pointee(5));
1620EXPECT_EQ(absl::any_cast<size_t>(&a), nullptr);
1621```
1622
1623**Description:** Early adaptation of C++17 `std::any`.
1624
1625**Documentation:** [std::any](https://en.cppreference.com/w/cpp/utility/any)
1626
1627**Notes:**
1628*** promo
1629Banned since workaround for lack of RTTI isn't compatible with the component
1630build. ([Bug](https://crbug.com/1096380))
1631***
1632
danakja6f71cb12021-12-15 21:04:491633### Command line flags <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:131634
1635```c++
1636ABSL_FLAG(bool, logs, false, "print logs to stderr");
1637app --logs=true;
1638```
1639
1640**Description:** Allows programmatic access to flag values passed on the
1641command-line to binaries.
1642
1643**Documentation:** [Flags Library](https://abseil.io/docs/cpp/guides/flags)
1644
1645**Notes:**
1646*** promo
1647Banned since workaround for lack of RTTI isn't compatible with the component
1648build. ([Bug](https://crbug.com/1096380)) Use `base::CommandLine` instead.
1649***
1650
danakja6f71cb12021-12-15 21:04:491651### Span <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:131652
1653```c++
1654absl::Span
1655```
1656
1657**Description:** Early adaptation of C++20 `std::span`.
1658
1659**Documentation:** [Using absl::Span](https://abseil.io/tips/93)
1660
1661**Notes:**
1662*** promo
1663Banned due to being less std::-compliant than `base::span`. Keep using
1664`base::span`.
1665***
1666
danakja6f71cb12021-12-15 21:04:491667### string_view <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:131668
1669```c++
1670absl::string_view
1671```
1672
1673**Description:** Early adaptation of C++17 `std::string_view`.
1674
1675**Documentation:** [absl::string_view](https://abseil.io/tips/1)
1676
1677**Notes:**
1678*** promo
1679Banned due to only working with 8-bit characters. Keep using
1680`base::StringPiece` from `base/strings/`.
1681***
1682
1683## Abseil TBD Features {#absl-review}
1684
1685The following Abseil library 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
danakja6f71cb12021-12-15 21:04:491689### 128bit integer <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:131690
1691```c++
1692uint64_t a;
1693absl::uint128 v = a;
1694```
1695
1696**Description:** Signed and unsigned 128-bit integer types meant to mimic
1697intrinsic types as closely as possible.
1698
1699**Documentation:**
1700[Numerics](https://abseil.io/docs/cpp/guides/numeric)
1701
1702**Notes:**
1703*** promo
1704None
1705***
1706
danakja6f71cb12021-12-15 21:04:491707### bind_front <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:131708
1709```c++
1710absl::bind_front
1711```
1712
1713**Description:** Binds the first N arguments of an invocable object and stores them by value.
1714
1715**Documentation:**
1716* [bind_front.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/bind_front.h)
1717* [Avoid std::bind](https://abseil.io/tips/108)
1718
1719**Notes:**
1720*** promo
1721Overlaps with `base::Bind`.
1722***
1723
danakja6f71cb12021-12-15 21:04:491724### Cleanup <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:131725
1726```c++
1727FILE* sink_file = fopen(sink_path, "w");
1728auto sink_closer = absl::MakeCleanup([sink_file] { fclose(sink_file); });
1729```
1730
1731**Description:** Implements the scope guard idiom, invoking the contained
1732callback's `operator()() &&` on scope exit.
1733
1734**Documentation:**
1735[cleanup.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/cleanup/cleanup.h)
1736
1737**Notes:**
1738*** promo
1739Similar to `defer` in Golang.
1740***
1741
danakja6f71cb12021-12-15 21:04:491742### Containers <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:131743
1744```c++
1745absl::flat_hash_map
1746absl::flat_hash_set
1747absl::node_hash_map
1748absl::node_hash_set
1749absl::btree_map
1750absl::btree_set
1751absl::btree_multimap
1752absl::btree_multiset
1753absl::InlinedVector
1754absl::FixedArray
1755```
1756
1757**Description:** Alternatives to STL containers designed to be more efficient
1758in the general case.
1759
1760**Documentation:**
1761* [Containers](https://abseil.io/docs/cpp/guides/container)
1762* [Hash](https://abseil.io/docs/cpp/guides/hash)
1763
1764**Notes:**
1765*** promo
1766Supplements `base/containers/`.
1767***
1768
danakja6f71cb12021-12-15 21:04:491769### Container utilities <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:131770
1771```c++
1772auto it = absl::c_find(container, value);
1773```
1774
1775**Description:** Container-based versions of algorithmic functions within C++
1776standard library.
1777
1778**Documentation:**
1779[container.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/algorithm/container.h)
1780
1781**Notes:**
1782*** promo
1783Overlaps with `base/ranges/algorithm.h`.
1784***
1785
danakja6f71cb12021-12-15 21:04:491786### FunctionRef <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:131787
1788```c++
1789absl::FunctionRef
1790```
1791
1792**Description:** Type for holding a non-owning reference to an object of any
1793invocable type.
1794
1795**Documentation:**
1796[function_ref.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/function_ref.h)
1797
1798**Notes:**
1799*** promo
1800None
1801***
1802
danakja6f71cb12021-12-15 21:04:491803### Random <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:131804
1805```c++
1806absl::BitGen bitgen;
1807size_t index = absl::Uniform(bitgen, 0u, elems.size());
1808```
1809
1810**Description:** Functions and utilities for generating pseudorandom data.
1811
1812**Documentation:** [Random library](https://abseil.io/docs/cpp/guides/random)
1813
1814**Notes:**
1815*** promo
1816Overlaps with `base/rand_util.h`.
1817***
1818
danakja6f71cb12021-12-15 21:04:491819### StatusOr <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:131820
1821```c++
1822absl::StatusOr<T>
1823```
1824
1825**Description:** An object that is either a usable value, or an error Status
1826explaining why such a value is not present.
1827
1828**Documentation:**
1829[statusor.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/status/statusor.h)
1830
1831**Notes:**
1832*** promo
1833None
1834***
1835
danakja6f71cb12021-12-15 21:04:491836### String Formatting <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:131837
1838```c++
1839absl::StrFormat
1840```
1841
1842**Description:** A typesafe replacement for the family of printf() string
1843formatting routines.
1844
1845**Documentation:**
1846[String Formatting](https://abseil.io/docs/cpp/guides/format)
1847
1848**Notes:**
1849*** promo
1850None
1851***
1852
danakja6f71cb12021-12-15 21:04:491853### Strings Library <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:131854
1855```c++
1856absl::StrSplit
1857absl::StrJoin
1858absl::StrCat
1859absl::StrAppend
1860absl::Substitute
1861absl::StrContains
1862```
1863
1864**Description:** Classes and utility functions for manipulating and comparing
1865strings.
1866
1867**Documentation:**
1868[String Utilities](https://abseil.io/docs/cpp/guides/strings)
1869
1870**Notes:**
1871*** promo
1872Overlaps with `base/strings`.
1873***
1874
danakja6f71cb12021-12-15 21:04:491875### Synchronization <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:131876
1877```c++
1878absl::Mutex
1879```
1880
1881**Description:** Primitives for managing tasks across different threads.
1882
1883**Documentation:**
1884[Synchronization](https://abseil.io/docs/cpp/guides/synchronization)
1885
1886**Notes:**
1887*** promo
1888Overlaps with `Lock` in `base/synchronization/`.
1889***
1890
danakja6f71cb12021-12-15 21:04:491891### Time library <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:131892
1893```c++
1894absl::Duration
1895absl::Time
1896absl::TimeZone
1897absl::CivilDay
1898```
1899
1900**Description:** Abstractions for holding time values, both in terms of
1901absolute time and civil time.
1902
1903**Documentation:** [Time](https://abseil.io/docs/cpp/guides/time)
1904
1905**Notes:**
1906*** promo
1907Overlaps with `Time` APIs in `base/`.
1908***