blob: 3e2971ff65d564518e6a52b791a965b205f80700 [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
Avi Drissmanefca4122022-01-05 23:59:36385### Nested namespaces <sup>[allowed]</sup>
386
387```c++
388namespace A::B::C { ...
389```
390
391**Description:** Using the namespace resolution operator to create nested
392namespace definitions.
393
394**Documentation:**
395[Namespaces](https://en.cppreference.com/w/cpp/language/namespace)
396
397**Notes:**
398*** promo
399[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/gLdR3apDSmg/)
400***
401
Peter Kasting1865f2772021-12-23 21:23:58402### Template argument deduction for class templates <sup>[allowed]</sup>
403
404```c++
405template <typename T>
406struct MyContainer {
407 MyContainer(T val) : val{val} {}
408 // ...
409};
410MyContainer c1(1); // Type deduced to be `int`.
411```
412
413**Description:** Automatic template argument deduction much like how it's done
414for functions, but now including class constructors.
415
416**Documentation:**
417[Class template argument deduction](https://en.cppreference.com/w/cpp/language/class_template_argument_deduction)
418
419**Notes:**
420*** promo
421Usage is governed by the
422[Google Style Guide](https://google.github.io/styleguide/cppguide.html#CTAD).
423***
424
Andrew Rayskiy6ce944d2022-01-04 18:13:33425### Selection statements with initializer <sup>[allowed]</sup>
426
427```c++
428if (int a = Func(); a < 3) { ...
429switch (int a = Func(); a) { ...
430```
431
432**Description:** New versions of the if and switch statements which simplify
433common code patterns and help users keep scopes tight.
434
435**Documentation:**
436[if statement](https://en.cppreference.com/w/cpp/language/if),
437[switch statement](https://en.cppreference.com/w/cpp/language/switch)
438
439**Notes:**
440*** promo
441[@cxx discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/4GP43nftePE)
442***
443
Roland Bockf5242fb2022-01-05 17:54:38444### fallthrough attribute <sup>[allowed]</sup>
Roland Bock767858e2022-01-04 20:25:21445
446```c++
447case 1:
448 DoSomething();
449 [[fallthrough]];
450case 2:
451 break;
452```
453
454**Description:**
455The `[[fallthrough]]` attribute can be used in switch statements to indicate
456when intentionally falling through to the next case.
457
458**Documentation:**
459[C++ attribute: fallthrough](https://en.cppreference.com/w/cpp/language/attributes/fallthrough)
460
461**Notes:**
462*** promo
463See [discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/JrvyFd243QI).
464
465See [migration task](https://bugs.chromium.org/p/chromium/issues/detail?id=1283907).
466***
467
Jayson Adams62f16c02022-01-11 15:03:35468### constexpr if <sup>[allowed]</sup>
469
470```c++
471if constexpr (cond) { ...
472```
473
474**Description:** Write code that is instantiated depending on a compile-time
475condition.
476
477**Documentation:**
478[if statement](https://en.cppreference.com/w/cpp/language/if)
479
480**Notes:**
481*** promo
482See [discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/op2ePZnjP0w).
Avi Drissmanc2558ba2022-01-13 19:49:01483***
Victor Costandcddd262022-01-12 19:20:39484
Avi Drissman7ba88aa62022-01-13 14:15:48485### maybe_unused attribute <sup>[allowed]</sup>
486
487```c++
488struct [[maybe_unused]] MyUnusedThing;
489[[maybe_unused]] int x;
490```
491
492**Description:**
493The `[[maybe_unused]]` attribute can be used to indicate that individual
494variables, functions, or fields of a class/struct/enum can be left unused.
495
496**Documentation:**
497[C++ attribute: maybe_unused](https://en.cppreference.com/w/cpp/language/attributes/maybe_unused)
498
499**Notes:**
500*** promo
501See [discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/jPLfU5eRg8M/).
502***
503
Victor Costandcddd262022-01-12 19:20:39504### Structured bindings <sup>[allowed]</sup>
505
506```c++
507const auto [x, y] = FuncReturningStdPair();
508```
509
510**Description:** Allows writing `auto [x, y, z] = expr;` where the type of
511`expr` is a tuple-like object, whose elements are bound to the variables `x`,
512`y`, and `z` (which this construct declares). Tuple-like objects include
513`std::tuple`, `std::pair`, `std::array`, and aggregate structures.
514
515**Documentation:**
516[Structured binding declaration](https://en.cppreference.com/w/cpp/language/structured_binding)
517[Explanation of structured binding types](https://jguegant.github.io/blogs/tech/structured-bindings.html)
518
519**Notes:**
520*** promo
521In C++17, structured bindings don't work with lambda captures.
522[C++20 will allow capturing structured bindings by value](https://wg21.link/p1091r3).
523
524This feature forces omitting type names. Its use should follow
525[the guidance around `auto` in Google C++ Style guide](https://google.github.io/styleguide/cppguide.html#Type_deduction).
526
527See [discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ExfSorNLNf4).
Jayson Adams62f16c02022-01-11 15:03:35528***
529
Peter Kasting1865f2772021-12-23 21:23:58530## C++17 Allowed Library Features {#library-allowlist-17}
531
532The following C++17 language features are allowed in the Chromium codebase.
533
534### Allocation functions with explicit alignment <sup>[allowed]</sup>
535
536```c++
537class alignas(32) Vec3d {
538 double x, y, z;
539};
540auto p_vec = new Vec3d[10]; // 32-byte aligned in C++17, maybe not previously
541```
542
543**Description:** Performs heap allocation of objects whose alignment
544requirements exceed `__STDCPP_DEFAULT_NEW_ALIGNMENT__`.
545
546**Documentation:**
547[operator new](https://en.cppreference.com/w/cpp/memory/new/operator_new)
548
549**Notes:**
550*** promo
551None
552***
553
Daniel Cheng6f510fa2022-01-12 19:36:03554### Type trait variable templates <sup>[tbd]</sup>
555
556```c++
557bool b = std::is_same_v<int, std::int32_t>;
558```
559
560**Description:** Syntactic sugar to provide convenient access to `::value`
561members by simply adding `_v`.
562
563**Documentation:**
564[Type support](https://en.cppreference.com/w/cpp/types)
565
566**Notes:**
567*** promo
568[Discussion thread](Non://groups.google.com/a/chromium.org/g/cxx/c/KEa-0AOGRNY/m/IV_S3_pvAAAJ)
569***
570
Peter Kasting1865f2772021-12-23 21:23:58571## C++17 Banned Library Features {#library-blocklist-17}
572
573The following C++17 library features are not allowed in the Chromium codebase.
574
Daniel Chengc05fcc62022-01-12 16:54:29575### std::any <sup>[banned]</sup>
576
577```c++
578std::any x = 5;
579```
580
581**Description:** A type-safe container for single values of any type.
582
583**Documentation:**
584[std::any](https://en.cppreference.com/w/cpp/utility/any)
585
586**Notes:**
587*** promo
588[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/KEa-0AOGRNY/m/IV_S3_pvAAAJ)
589
590Banned since workaround for lack of RTTI isn't compatible with the component
591build ([Bug](https://crbug.com/1096380)). Also see `absl::any`.
592***
593
Peter Kasting1865f2772021-12-23 21:23:58594### std::filesystem <sup>[banned]</sup>
595
596```c++
597#include <filesystem>
598```
599
600**Description:** A standard way to manipulate files, directories, and paths in a
601filesystem.
602
603**Documentation:**
604[Filesystem library](https://en.cppreference.com/w/cpp/filesystem)
605
606**Notes:**
607*** promo
608Banned by the [Google Style Guide](https://google.github.io/styleguide/cppguide.html#Other_Features).
609***
610
611### weak_from_this <sup>[banned]</sup>
612
613```c++
614auto weak_ptr = weak_from_this();
615```
616
617**Description:** Returns a `std::weak_ptr<T>` that tracks ownership of `*this`
618by all existing `std::shared_ptr`s that refer to `*this`.
619
620**Documentation:**
621[std::enable_shared_from_this<T>::weak_from_this](https://en.cppreference.com/w/cpp/memory/enable_shared_from_this/weak_from_this)
622
623**Notes:**
624*** promo
625Banned since `std::shared_ptr` and `std::weak_ptr` are banned.
626***
627
628### Transparent std::owner_less <sup>[banned]</sup>
629
630```c++
631std::map<std::weak_ptr<T>, U, std::owner_less<>>
632```
633
634**Description:** Function object providing mixed-type owner-based ordering of
635shared and weak pointers, regardless of the type of the pointee.
636
637**Documentation:**
638[std::owner_less](https://en.cppreference.com/w/cpp/memory/owner_less)
639
640**Notes:**
641*** promo
642Banned since `std::shared_ptr` and `std::weak_ptr` are banned.
643***
644
645### Array support for std::shared_ptr <sup>[banned]</sup>
646
647```c++
648std::shared_ptr<int[]> p(new int[10]{0,1,2,3,4,5,6,7,8,9});
649std::cout << p[3]; // "3"
650```
651
652**Description:** Supports memory management of arrays via `std::shared_ptr`.
653
654**Documentation:**
655[std::shared_ptr](https://en.cppreference.com/w/cpp/memory/shared_ptr)
656
657**Notes:**
658*** promo
659Banned since `std::shared_ptr` is banned.
660***
661
662### std::uncaught_exceptions <sup>[banned]</sup>
663
664```c++
665int count = std::uncaught_exceptions();
666```
667
668**Description:** Determines whether there are live exception objects.
669
670**Documentation:**
671[std::uncaught_exceptions](https://en.cppreference.com/w/cpp/error/uncaught_exception)
672
673**Notes:**
674*** promo
675Banned because exceptions are banned.
676***
677
678### Rounding functions for duration and time_point <sup>[banned]</sup>
679
680```c++
681std::chrono::ceil<std::chrono::seconds>(dur);
682std::chrono::ceil<std::chrono::seconds>(time_pt);
683std::chrono::floor<std::chrono::seconds>(dur);
684std::chrono::floor<std::chrono::seconds>(time_pt);
685std::chrono::round<std::chrono::seconds>(dur);
686std::chrono::round<std::chrono::seconds>(time_pt);
687```
688
689**Description:** Converts durations and time_points by rounding.
690
691**Documentation:**
692[std::chrono::duration](https://en.cppreference.com/w/cpp/chrono/duration),
693[std::chrono::time_point](https://en.cppreference.com/w/cpp/chrono/time_point)
694
695**Notes:**
696*** promo
697Banned since `std::chrono` is banned.
698***
699
700## C++17 TBD Language Features {#core-review-17}
701
702The following C++17 language features are not allowed in the Chromium codebase.
703See the top of this page on how to propose moving a feature from this list into
704the allowed or banned sections.
705
706### Declaring non-type template parameters with auto <sup>[tbd]</sup>
707
708```c++
709template <auto... seq>
710struct my_integer_sequence {
711 // ...
712};
713auto seq = my_integer_sequence<0, 1, 2>(); // Type deduced to be `int`.
714```
715
716**Description:** Following the deduction rules of `auto`, while respecting the
717non-type template parameter list of allowable types, template arguments can be
718deduced from the types of its arguments.
719
720**Documentation:**
721[Template parameters](https://en.cppreference.com/w/cpp/language/template_parameters)
722
723**Notes:**
724*** promo
725None
726***
727
728### Fold expressions <sup>[tbd]</sup>
729
730```c++
731template <typename... Args>
732auto sum(Args... args) {
733 return (... + args);
734}
735```
736
737**Description:** A fold expression performs a fold of a template parameter pack
738over a binary operator.
739
740**Documentation:**
741[Fold expression](https://en.cppreference.com/w/cpp/language/fold)
742
743**Notes:**
744*** promo
745None
746***
747
748### constexpr lambda <sup>[tbd]</sup>
749
750```c++
751auto identity = [](int n) constexpr { return n; };
752static_assert(identity(123) == 123);
753```
754
755**Description:** Compile-time lambdas using constexpr.
756
757**Documentation:**
758[Lambda expressions](https://en.cppreference.com/w/cpp/language/lambda)
759
760**Notes:**
761*** promo
762None
763***
764
765### Lambda capture this by value <sup>[tbd]</sup>
766
767```c++
768const auto l = [*this] { return member_; }
769```
770
771**Description:** `*this` captures the current object by copy, while `this`
772continues to capture by reference.
773
774**Documentation:**
775[Lambda capture](https://en.cppreference.com/w/cpp/language/lambda#Lambda_capture)
776
777**Notes:**
778*** promo
779None
780***
781
782### Inline variables <sup>[tbd]</sup>
783
784```c++
785struct S {
786 static constexpr int kZero = 0; // constexpr implies inline here
787};
788```
789
790**Description:** The `inline` specifier can be applied to variables as well as
791to functions. A variable declared inline has the same semantics as a function
792declared inline. It can also be used to declare and define a static member
793variable, such that it does not need to be initialized in the source file.
794
795**Documentation:**
796[inline specifier](https://en.cppreference.com/w/cpp/language/inline)
797
798**Notes:**
799*** promo
800None
801***
802
Peter Kasting1865f2772021-12-23 21:23:58803### UTF-8 character literals <sup>[tbd]</sup>
804
805```c++
806char x = u8'x';
807```
808
809**Description:** A character literal that begins with `u8` is a character
810literal of type `char`. The value of a UTF-8 character literal is equal to its
811ISO 10646 code point value.
812
813**Documentation:**
814[Character literal](https://en.cppreference.com/w/cpp/language/character_literal)
815
816**Notes:**
817*** promo
818C++20 changes the type to `char8_t`, causing migration hazards for code using
819this.
820***
821
Roland Bock767858e2022-01-04 20:25:21822### nodiscard attribute <sup>[tbd]</sup>
Peter Kasting1865f2772021-12-23 21:23:58823
824```c++
Roland Bockf5242fb2022-01-05 17:54:38825struct [[nodiscard]] ErrorOrValue;
826[[nodiscard]] bool DoSomething();
Peter Kasting1865f2772021-12-23 21:23:58827```
828
Roland Bockf5242fb2022-01-05 17:54:38829**Description:**
830The `[[nodiscard]]` attribute can be used to indicate that
831
832 - the return value of a function should not be ignored
833 - values of annotated classes/structs/enums returned from functions should not
834 be ignored
Peter Kasting1865f2772021-12-23 21:23:58835
836**Documentation:**
Peter Kasting1865f2772021-12-23 21:23:58837[C++ attribute: nodiscard](https://en.cppreference.com/w/cpp/language/attributes/nodiscard)
838
839**Notes:**
840*** promo
Roland Bockf5242fb2022-01-05 17:54:38841See `WARN_UNUSED_RESULT` macro in base/compiler_specific.h.
Roland Bock767858e2022-01-04 20:25:21842***
843
Peter Kasting1865f2772021-12-23 21:23:58844### using declaration for attributes <sup>[tbd]</sup>
845
846```c++
847[[using CC: opt(1), debug]] // same as [[CC:opt(1), CC::debug]]
848```
849
850**Description:** Specifies a common namespace for a list of attributes.
851
852**Documentation:**
853[Attribute specifier sequence](https://en.cppreference.com/w/cpp/language/attributes)
854
855**Notes:**
856*** promo
857See similar attribute macros in base/compiler_specific.h.
858***
859
860### __has_include <sup>[tbd]</sup>
861
862```c++
863#if __has_include(<optional>) ...
864```
865
866**Description:** Checks whether a file is available for inclusion, i.e. the file
867exists.
868
869**Documentation:**
870[Source file inclusion](https://en.cppreference.com/w/cpp/preprocessor/include)
871
872**Notes:**
873*** promo
874None
875***
876
877## C++17 TBD Library Features {#library-review-17}
878
879The following C++17 library features are not allowed in the Chromium codebase.
880See the top of this page on how to propose moving a feature from this list into
881the allowed or banned sections.
882
883### std::variant <sup>[tbd]</sup>
884
885```c++
886std::variant<int, double> v = 12;
887```
888
889**Description:** The class template `std::variant` represents a type-safe
890`union`. An instance of `std::variant` at any given time holds a value of one of
891its alternative types (it's also possible for it to be valueless).
892
893**Documentation:**
894[std::variant](https://en.cppreference.com/w/cpp/utility/variant)
895
896**Notes:**
897*** promo
898See also `absl::variant`.
899***
900
901### std::optional <sup>[tbd]</sup>
902
903```c++
904std::optional<std::string> s;
905```
906
907**Description:** The class template `std::optional` manages an optional
908contained value, i.e. a value that may or may not be present. A common use case
909for optional is the return value of a function that may fail.
910
911**Documentation:**
912[std::optional](https://en.cppreference.com/w/cpp/utility/optional)
913
914**Notes:**
915*** promo
916See also `absl::optional`.
917***
918
Peter Kasting1865f2772021-12-23 21:23:58919### std::string_view <sup>[tbd]</sup>
920
921```c++
922std::string_view str = "foo";
923```
924
925**Description:** A non-owning reference to a string. Useful for providing an
926abstraction on top of strings (e.g. for parsing).
927
928**Documentation:**
929[std::basic_string_view](https://en.cppreference.com/w/cpp/string/basic_string_view)
930
931**Notes:**
932*** promo
933See also `absl::string_view` and `base::StringPiece`.
934***
935
936### std::invoke <sup>[tbd]</sup>
937
938```c++
939static_assert(std::invoke(std::plus<>(), 1, 2) == 3);
940```
941
942**Description:** Invokes a `Callable` object with parameters. An example of a
943`Callable` object is `base::Callback` where an object can be called similarly to
944a regular function.
945
946**Documentation:**
947[std::invoke](https://en.cppreference.com/w/cpp/utility/functional/invoke)
948
949**Notes:**
950*** promo
951See also `base::invoke`.
952***
953
954### std::apply <sup>[tbd]</sup>
955
956```c++
957static_assert(std::apply(std::plus<>(), std::make_tuple(1, 2)) == 3);
958```
959
960**Description:** Invokes a `Callable` object with a tuple of arguments.
961
962**Documentation:**
963[std::apply](https://en.cppreference.com/w/cpp/utility/apply)
964
965**Notes:**
966*** promo
967See also `absl::apply` and `base::apply`.
968***
969
970### std::byte <sup>[tbd]</sup>
971
972```c++
973std::byte b = 0xFF;
974int i = std::to_integer<int>(b); // 0xFF
975```
976
977**Description:** A standard way of representing data as a byte. `std::byte` is
978neither a character type nor an arithmetic type, and the only operator overloads
979available are bitwise operations.
980
981**Documentation:**
982[std::byte](https://en.cppreference.com/w/cpp/types/byte)
983
984**Notes:**
985*** promo
986None
987***
988
989### Splicing for maps and sets <sup>[tbd]</sup>
990
991```c++
992std::map<...>::extract
993std::map<...>::merge
994std::set<...>::extract
995std::set<...>::merge
996```
997
998**Description:** Moving nodes and merging containers without the overhead of
999expensive copies, moves, or heap allocations/deallocations.
1000
1001**Documentation:**
1002[std::map::extract](https://en.cppreference.com/w/cpp/container/map/extract),
1003[std::map::merge](https://en.cppreference.com/w/cpp/container/map/merge)
1004
1005**Notes:**
1006*** promo
1007None
1008***
1009
1010### Parallel algorithms <sup>[tbd]</sup>
1011
1012```c++
1013auto it = std::find(std::execution::par, std::begin(vec), std::end(vec), 2);
1014```
1015
1016**Description:** Many of the STL algorithms, such as the `copy`, `find` and
1017`sort` methods, now support the parallel execution policies: `seq`, `par`, and
1018`par_unseq` which translate to "sequentially", "parallel" and
1019"parallel unsequenced".
1020
1021**Documentation:**
1022[execution_policy_tag_t](https://en.cppreference.com/w/cpp/algorithm/execution_policy_tag_t)
1023
1024**Notes:**
1025*** promo
1026None
1027***
1028
1029### std::make_from_tuple <sup>[tbd]</sup>
1030
1031```c++
1032// Calls Foo(int, double):
1033auto foo = std::make_from_tuple<Foo>(std::make_tuple(1, 3.5));
1034```
1035
1036**Description:** Constructs an object from a tuple of arguments.
1037
1038**Documentation:**
1039[std::make_from_tuple](https://en.cppreference.com/w/cpp/utility/make_from_tuple)
1040
1041**Notes:**
1042*** promo
1043See also `absl::make_from_tuple`.
1044***
1045
1046### Searchers <sup>[tbd]</sup>
1047
1048```c++
1049auto it = std::search(haystack.begin(), haystack.end(),
1050 std::boyer_moore_searcher(needle.begin(), needle.end()));
1051```
1052
1053**Description:** Alternate string searching algorithms.
1054
1055**Documentation:**
1056[Searchers](https://en.cppreference.com/w/cpp/utility/functional#Searchers)
1057
1058**Notes:**
1059*** promo
1060None
1061***
1062
1063### std::as_const <sup>[tbd]</sup>
1064
1065```c++
1066auto&& const_ref = std::as_const(mutable_obj);
1067```
1068
1069**Description:** Forms reference to const T.
1070
1071**Documentation:**
1072[std::as_const](https://en.cppreference.com/w/cpp/utility/as_const)
1073
1074**Notes:**
1075*** promo
1076See also `base::as_const`.
1077***
1078
1079### std::not_fn <sup>[tbd]</sup>
1080
1081```c++
1082auto nonwhite = std::find_if(str.begin(), str.end(), std::not_fn(IsWhitespace));
1083```
1084
1085**Description:** Creates a forwarding call wrapper that returns the negation of
1086the callable object it holds.
1087
1088**Documentation:**
1089[std::not_fn](https://en.cppreference.com/w/cpp/utility/functional/not_fn)
1090
1091**Notes:**
1092*** promo
1093See also `base::not_fn`.
1094***
1095
1096### Uninitialized memory algorithms <sup>[tbd]</sup>
1097
1098```c++
1099std::destroy_at(ptr);
1100std::destroy(ptr, ptr + 8);
1101std::destroy_n(ptr, 8);
1102std::uninitialized_move(src.begin(), src.end(), dest.begin());
1103std::uninitialized_value_construct(std::begin(storage), std::end(storage));
1104```
1105
1106**Description:** Replaces direct constructor and destructor calls when manually
1107managing memory.
1108
1109**Documentation:**
1110[std::destroy_at](https://en.cppreference.com/w/cpp/memory/destroy_at),
1111[std::destroy](https://en.cppreference.com/w/cpp/memory/destroy),
1112[std::destroy_n](https://en.cppreference.com/w/cpp/memory/destroy_n),
1113[std::uninitialized_move](https://en.cppreference.com/w/cpp/memory/uninitialized_move),
1114[std::uninitialized_value_construct](https://en.cppreference.com/w/cpp/memory/uninitialized_value_construct)
1115
1116**Notes:**
1117*** promo
1118None
1119***
1120
1121### std::pmr::memory_resource and std::polymorphic_allocator <sup>[tbd]</sup>
1122
1123```c++
1124#include <memory_resource>
1125```
1126
1127**Description:** Manages memory allocations using runtime polymorphism.
1128
1129**Documentation:**
1130[std::pmr::memory_resource](https://en.cppreference.com/w/cpp/memory/memory_resource),
1131[std::pmr::polymorphic_allocator](https://en.cppreference.com/w/cpp/memory/polymorphic_allocator),
1132
1133**Notes:**
1134*** promo
1135May not be supported in libc++, according to the
1136[library features table](https://en.cppreference.com/w/cpp/17)
1137***
1138
1139### std::aligned_alloc <sup>[tbd]</sup>
1140
1141```c++
1142int* p2 = static_cast<int*>(std::aligned_alloc(1024, 1024));
1143```
1144
1145**Description:** Allocates uninitialized storage with the specified alignment.
1146
1147**Documentation:**
1148[std::aligned_alloc](https://en.cppreference.com/w/cpp/memory/c/aligned_alloc)
1149
1150**Notes:**
1151*** promo
1152None
1153***
1154
1155### std::conjunction/std::disjunction/std::negation <sup>[tbd]</sup>
1156
1157```c++
1158template<typename T, typename... Ts>
1159std::enable_if_t<std::conjunction_v<std::is_same<T, Ts>...>>
1160func(T, Ts...) { ...
1161```
1162
1163**Description:** Performs logical operations on type traits.
1164
1165**Documentation:**
1166[std::conjunction](https://en.cppreference.com/w/cpp/types/conjunction),
1167[std::disjunction](https://en.cppreference.com/w/cpp/types/disjunction),
1168[std::negation](https://en.cppreference.com/w/cpp/types/negation)
1169
1170**Notes:**
1171*** promo
1172None
1173***
1174
Peter Kasting1865f2772021-12-23 21:23:581175### std::is_swappable <sup>[tbd]</sup>
1176
1177```c++
1178std::is_swappable<T>
1179std::is_swappable_with_v<T, U>
1180```
1181
1182**Description:** Checks whether classes may be swapped.
1183
1184**Documentation:**
1185[std::is_swappable](https://en.cppreference.com/w/cpp/types/is_swappable)
1186
1187**Notes:**
1188*** promo
1189None
1190***
1191
1192### std::is_invocable <sup>[tbd]</sup>
1193
1194```c++
1195std::is_invocable_v<Fn, 1, "Hello">
1196```
1197
1198**Description:** Checks whether a function may be invoked with the given
1199argument types. The `_r` variant also evaluates whether the result is
1200convertible to a given type.
1201
1202**Documentation:**
1203[std::is_invocable](https://en.cppreference.com/w/cpp/types/is_invocable)
1204
1205**Notes:**
1206*** promo
1207None
1208***
1209
1210### std::is_aggregate <sup>[tbd]</sup>
1211
1212```c++
1213if constexpr(std::is_aggregate_v<T>) { ...
1214```
1215
1216**Description:** Checks wither the given type is an aggregate type.
1217
1218**Documentation:**
1219[std::is_aggregate](https://en.cppreference.com/w/cpp/types/is_aggregate)
1220
1221**Notes:**
1222*** promo
1223None
1224***
1225
1226### std::has_unique_object_representations <sup>[tbd]</sup>
1227
1228```c++
1229std::has_unique_object_representations_v<foo>
1230```
1231
1232**Description:** Checks wither the given type is trivially copyable and any two
1233objects with the same value have the same object representation.
1234
1235**Documentation:**
1236[std::has_unique_object_representations](https://en.cppreference.com/w/cpp/types/has_unique_object_representations)
1237
1238**Notes:**
1239*** promo
1240None
1241***
1242
1243### std::clamp <sup>[tbd]</sup>
1244
1245```c++
1246int x = base::clamp(inp, 0, 100);
1247```
1248
1249**Description:** Clamps a value between a minimum and a maximum.
1250
1251**Documentation:**
1252[std::clamp](https://en.cppreference.com/w/cpp/algorithm/clamp)
1253
1254**Notes:**
1255*** promo
1256See also `base::clamp`.
1257***
1258
1259### std::reduce <sup>[tbd]</sup>
1260
1261```c++
1262std::reduce(std::execution::par, v.cbegin(), v.cend());
1263```
1264
1265**Description:** Like `std::accumulate` except the elements of the range may be
1266grouped and rearranged in arbitrary order.
1267
1268**Documentation:**
1269[std::reduce](https://en.cppreference.com/w/cpp/algorithm/reduce)
1270
1271**Notes:**
1272*** promo
1273Makes the most sense in conjunction with `std::execution::par`.
1274***
1275
1276### std::inclusive_scan <sup>[tbd]</sup>
1277
1278```c++
1279std::inclusive_scan(data.begin(), data.end(), output.begin());
1280```
1281
1282**Description:** Like `std::accumulate` but writes the result at each step into
1283the output range.
1284
1285**Documentation:**
1286[std::inclusive_scan](https://en.cppreference.com/w/cpp/algorithm/inclusive_scan)
1287
1288**Notes:**
1289*** promo
1290None
1291***
1292
1293### std::exclusive_scan <sup>[tbd]</sup>
1294
1295```c++
1296std::exclusive_scan(data.begin(), data.end(), output.begin());
1297```
1298
1299**Description:** Like `std::inclusive_scan` but omits the current element from
1300the written output at each step; that is, results are "one value behind" those
1301of `std::inclusive_scan`.
1302
1303**Documentation:**
1304[std::exclusive_scan](https://en.cppreference.com/w/cpp/algorithm/exclusive_scan)
1305
1306**Notes:**
1307*** promo
1308None
1309***
1310
1311### std::gcd <sup>[tbd]</sup>
1312
1313```c++
1314static_assert(std::gcd(12, 18) == 6);
1315```
1316
1317**Description:** Computes the greatest common divisor of its arguments.
1318
1319**Documentation:**
1320[std::gcd](https://en.cppreference.com/w/cpp/numeric/gcd)
1321
1322**Notes:**
1323*** promo
1324None
1325***
1326
1327### std::lcm <sup>[tbd]</sup>
1328
1329```c++
1330static_assert(std::lcm(12, 18) == 36);
1331```
1332
1333**Description:** Computes the least common multiple of its arguments.
1334
1335**Documentation:**
1336[std::lcm](https://en.cppreference.com/w/cpp/numeric/lcm)
1337
1338**Notes:**
1339*** promo
1340None
1341***
1342
1343### std::map::try_emplace <sup>[tbd]</sup>
1344
1345```c++
1346std::map<std::string, std::string> m;
1347m.try_emplace("c", 10, 'c');
1348m.try_emplace("c", "Won't be inserted");
1349```
1350
1351**Description:** Like `emplace`, but does not move from rvalue arguments if the
1352insertion does not happen.
1353
1354**Documentation:**
1355[std::map::try_emplace](https://en.cppreference.com/w/cpp/container/map/try_emplace),
1356
1357**Notes:**
1358*** promo
1359Already available on `base::flat_map`.
1360***
1361
1362### std::map::insert_or_assign <sup>[tbd]</sup>
1363
1364```c++
1365std::map<std::string, std::string> m;
1366m.insert_or_assign("c", "cherry");
1367m.insert_or_assign("c", "clementine");
1368```
1369
1370**Description:** Like `operator[]`, but returns more information and does not
1371require default-constructibility of the mapped type.
1372
1373**Documentation:**
1374[std::map::insert_or_assign](https://en.cppreference.com/w/cpp/container/map/insert_or_assign)
1375
1376**Notes:**
1377*** promo
1378Already available on `base::flat_map`.
1379***
1380
1381### Non-member std::size/std::empty/std::data <sup>[tbd]</sup>
1382
1383```c++
1384for (std::size_t i = 0; i < std::size(c); ++i) { ...
1385if (!std::empty(c)) { ...
1386std::strcpy(arr, std::data(str));
1387```
1388
1389**Description:** Non-member versions of what are normally member functions, for
1390symmetrical use with things like arrays and initializer_lists.
1391
1392**Documentation:**
1393[std::size](https://en.cppreference.com/w/cpp/iterator/size),
1394[std::empty](https://en.cppreference.com/w/cpp/iterator/empty),
1395[std::data](https://en.cppreference.com/w/cpp/iterator/data)
1396
1397**Notes:**
1398*** promo
1399See `base::size`, `base::empty`, and `base::data`.
1400***
1401
1402### Mathematical special functions <sup>[tbd]</sup>
1403
1404```c++
1405std::assoc_laguerre()
1406std::assoc_legendre()
1407std::beta()
1408std::comp_ellint_1()
1409std::comp_ellint_2()
1410std::comp_ellint_3()
1411std::cyl_bessel_i()
1412std::cyl_bessel_j()
1413std::cyl_bessel_k()
1414std::cyl_neumann()
1415std::ellint_1()
1416std::ellint_2()
1417std::ellint_3()
1418std::expint()
1419std::hermite()
1420std::legendre()
1421std::laguerre()
1422std::riemann_zeta()
1423std::sph_bessel()
1424std::sph_legendre()
1425std::sph_neumann()
1426```
1427
1428**Description:** A variety of mathematical functions.
1429
1430**Documentation:**
1431[Mathematical special functions](https://en.cppreference.com/w/cpp/numeric/special_functions)
1432
1433**Notes:**
1434*** promo
1435May not be supported in libc++, according to the
1436[library features table](https://en.cppreference.com/w/cpp/17)
1437***
1438
1439### 3D std::hypot <sup>[tbd]</sup>
1440
1441```c++
1442double dist = std::hypot(1.0, 2.5, 3.7);
1443```
1444
1445**Description:** Computes the distance from the origin in 3D space.
1446
1447**Documentation:**
1448[std::hypot](https://en.cppreference.com/w/cpp/numeric/math/hypot)
1449
1450**Notes:**
1451*** promo
1452None
1453***
1454
1455### Cache line interface <sup>[tbd]</sup>
1456
1457```c++
1458alignas(std::hardware_destructive_interference_size) std::atomic<int> cat;
1459static_assert(sizeof(S) <= std::hardware_constructive_interference_size);
1460```
1461
1462**Description:** A portable way to access the L1 data cache line size.
1463
1464**Documentation:**
1465[Hardware interference size](https://en.cppreference.com/w/cpp/thread/hardware_destructive_interference_size)
1466
1467**Notes:**
1468*** promo
1469May not be supported in libc++, according to the
1470[library features table](https://en.cppreference.com/w/cpp/17)
1471***
1472
1473### std::launder <sup>[tbd]</sup>
1474
1475```c++
1476struct Y { int z; };
1477alignas(Y) std::byte s[sizeof(Y)];
1478Y* q = new(&s) Y{2};
1479const int h = std::launder(reinterpret_cast<Y*>(&s))->z;
1480```
1481
1482**Description:** When used to wrap a pointer, makes it valid to access the
1483resulting object in cases it otherwise wouldn't have been, in a very limited set
1484of circumstances.
1485
1486**Documentation:**
1487[std::launder](https://en.cppreference.com/w/cpp/utility/launder)
1488
1489**Notes:**
1490*** promo
1491None
1492***
1493
1494### std::to_chars/std::from_chars <sup>[tbd]</sup>
1495
1496```c++
1497std::to_chars(str.data(), str.data() + str.size(), 42);
1498std::from_chars(str.data(), str.data() + str.size(), result);
1499```
1500
1501**Description:** Locale-independent, non-allocating, non-throwing functions to
1502convert values to/from character strings, designed for use in high-throughput
1503contexts.
1504
1505**Documentation:**
1506[std::to_chars](https://en.cppreference.com/w/cpp/utility/to_chars),
1507[std::from_chars](https://en.cppreference.com/w/cpp/utility/from_chars)
1508
1509**Notes:**
1510*** promo
1511None
1512***
1513
1514### std::atomic<T>::is_always_lock_free <sup>[tbd]</sup>
1515
1516```c++
1517template <typename T>
1518struct is_lock_free_impl
1519: std::integral_constant<bool, std::atomic<T>::is_always_lock_free> {};
1520```
1521
1522**Description:** True when the given atomic type is always lock-free.
1523
1524**Documentation:**
1525[std::atomic<T>::is_always_lock_free](https://en.cppreference.com/w/cpp/atomic/atomic/is_always_lock_free)
1526
1527**Notes:**
1528*** promo
1529None
1530***
1531
1532### std::scoped_lock <sup>[tbd]</sup>
1533
1534```c++
1535std::scoped_lock lock(e1.m, e2.m);
1536```
1537
1538**Description:** Provides an RAII-style mechanism for owning one or more mutexes
1539for the duration of a scoped block.
1540
1541**Documentation:**
1542[std::scoped_lock](https://en.cppreference.com/w/cpp/thread/scoped_lock)
1543
1544**Notes:**
1545*** promo
1546See also `base::AutoLock`.
1547***
1548
1549### std::timespec_get <sup>[tbd]</sup>
1550
1551```c++
1552std::timespec ts;
1553std::timespec_get(&ts, TIME_UTC);
1554```
1555
1556**Description:** Gets the current calendar time in the given time base.
1557
1558**Documentation:**
1559[std::timespec_get](https://en.cppreference.com/w/cpp/chrono/c/timespec_get)
1560
1561**Notes:**
1562*** promo
1563None
1564***
1565
Joe Masonfe4f2562021-09-15 15:23:131566## Abseil Allowed Library Features {#absl-allowlist}
1567
1568The following Abseil library features are allowed in the Chromium codebase.
1569
danakja6f71cb12021-12-15 21:04:491570### Optional <sup>[allowed]</sup>
Joe Masonfe4f2562021-09-15 15:23:131571
1572```c++
1573absl::optional
1574```
1575
1576**Description:** Early adaptation of C++17 `std::optional`.
1577
1578**Documentation:**
1579[std::optional](https://en.cppreference.com/w/cpp/utility/optional)
1580
1581**Notes:**
1582*** promo
1583Replaces `base::Optional`.
1584[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/zUGqagX1NFU)
1585***
1586
danakja6f71cb12021-12-15 21:04:491587### Status <sup>[allowed]</sup>
Joe Masonfe4f2562021-09-15 15:23:131588
1589```c++
1590absl::Status
1591```
1592
1593**Description:** Type for returning detailed errors.
1594
1595**Documentation:**
1596[status.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/status/status.h)
1597
1598**Notes:**
1599*** promo
1600Approved for use inside a wrapper type. Use
1601[abseil_string_conversions.h](https://source.chromium.org/chromium/chromium/src/+/main:base/strings/abseil_string_conversions.h)
1602to convert to and from
1603[absl::string_view](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/strings/string_view.h)
1604so the wrapper can expose
1605[base::StringPiece](https://source.chromium.org/chromium/chromium/src/+/main:base/strings/string_piece.h).
1606Use
1607[absl::Cord](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/strings/cord.h)
1608directly as minimally necessary to interface; do not expose in the wrapper type
1609API.
1610
1611[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ImdFCSZ-NMA)
1612***
1613
danakja6f71cb12021-12-15 21:04:491614### Variant <sup>[allowed]</sup>
Joe Masonfe4f2562021-09-15 15:23:131615
1616```c++
1617absl::variant
1618```
1619
1620**Description:** Early adaptation of C++17 `std::variant`.
1621
1622**Documentation:**
1623[std::variant](https://en.cppreference.com/w/cpp/utility/variant)
1624
1625**Notes:**
1626*** promo
1627[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/DqvG-TpvMyU)
1628***
1629
1630## Abseil Banned Library Features {#absl-blocklist}
1631
1632The following Abseil library features are not allowed in the Chromium codebase.
1633
danakja6f71cb12021-12-15 21:04:491634### Any <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:131635
1636```c++
1637absl::any a = int{5};
1638EXPECT_THAT(absl::any_cast<int>(&a), Pointee(5));
1639EXPECT_EQ(absl::any_cast<size_t>(&a), nullptr);
1640```
1641
1642**Description:** Early adaptation of C++17 `std::any`.
1643
1644**Documentation:** [std::any](https://en.cppreference.com/w/cpp/utility/any)
1645
1646**Notes:**
1647*** promo
1648Banned since workaround for lack of RTTI isn't compatible with the component
Daniel Chengc05fcc62022-01-12 16:54:291649build ([Bug](https://crbug.com/1096380)). Also see `std::any`.
Joe Masonfe4f2562021-09-15 15:23:131650***
1651
danakja6f71cb12021-12-15 21:04:491652### Command line flags <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:131653
1654```c++
1655ABSL_FLAG(bool, logs, false, "print logs to stderr");
1656app --logs=true;
1657```
1658
1659**Description:** Allows programmatic access to flag values passed on the
1660command-line to binaries.
1661
1662**Documentation:** [Flags Library](https://abseil.io/docs/cpp/guides/flags)
1663
1664**Notes:**
1665*** promo
1666Banned since workaround for lack of RTTI isn't compatible with the component
1667build. ([Bug](https://crbug.com/1096380)) Use `base::CommandLine` instead.
1668***
1669
danakja6f71cb12021-12-15 21:04:491670### Span <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:131671
1672```c++
1673absl::Span
1674```
1675
1676**Description:** Early adaptation of C++20 `std::span`.
1677
1678**Documentation:** [Using absl::Span](https://abseil.io/tips/93)
1679
1680**Notes:**
1681*** promo
1682Banned due to being less std::-compliant than `base::span`. Keep using
1683`base::span`.
1684***
1685
danakja6f71cb12021-12-15 21:04:491686### string_view <sup>[banned]</sup>
Joe Masonfe4f2562021-09-15 15:23:131687
1688```c++
1689absl::string_view
1690```
1691
1692**Description:** Early adaptation of C++17 `std::string_view`.
1693
1694**Documentation:** [absl::string_view](https://abseil.io/tips/1)
1695
1696**Notes:**
1697*** promo
1698Banned due to only working with 8-bit characters. Keep using
1699`base::StringPiece` from `base/strings/`.
1700***
1701
1702## Abseil TBD Features {#absl-review}
1703
1704The following Abseil library features are not allowed in the Chromium codebase.
1705See the top of this page on how to propose moving a feature from this list into
1706the allowed or banned sections.
1707
danakja6f71cb12021-12-15 21:04:491708### 128bit integer <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:131709
1710```c++
1711uint64_t a;
1712absl::uint128 v = a;
1713```
1714
1715**Description:** Signed and unsigned 128-bit integer types meant to mimic
1716intrinsic types as closely as possible.
1717
1718**Documentation:**
1719[Numerics](https://abseil.io/docs/cpp/guides/numeric)
1720
1721**Notes:**
1722*** promo
1723None
1724***
1725
danakja6f71cb12021-12-15 21:04:491726### bind_front <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:131727
1728```c++
1729absl::bind_front
1730```
1731
1732**Description:** Binds the first N arguments of an invocable object and stores them by value.
1733
1734**Documentation:**
1735* [bind_front.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/bind_front.h)
1736* [Avoid std::bind](https://abseil.io/tips/108)
1737
1738**Notes:**
1739*** promo
1740Overlaps with `base::Bind`.
1741***
1742
danakja6f71cb12021-12-15 21:04:491743### Cleanup <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:131744
1745```c++
1746FILE* sink_file = fopen(sink_path, "w");
1747auto sink_closer = absl::MakeCleanup([sink_file] { fclose(sink_file); });
1748```
1749
1750**Description:** Implements the scope guard idiom, invoking the contained
1751callback's `operator()() &&` on scope exit.
1752
1753**Documentation:**
1754[cleanup.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/cleanup/cleanup.h)
1755
1756**Notes:**
1757*** promo
1758Similar to `defer` in Golang.
1759***
1760
danakja6f71cb12021-12-15 21:04:491761### Containers <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:131762
1763```c++
1764absl::flat_hash_map
1765absl::flat_hash_set
1766absl::node_hash_map
1767absl::node_hash_set
1768absl::btree_map
1769absl::btree_set
1770absl::btree_multimap
1771absl::btree_multiset
1772absl::InlinedVector
1773absl::FixedArray
1774```
1775
1776**Description:** Alternatives to STL containers designed to be more efficient
1777in the general case.
1778
1779**Documentation:**
1780* [Containers](https://abseil.io/docs/cpp/guides/container)
1781* [Hash](https://abseil.io/docs/cpp/guides/hash)
1782
1783**Notes:**
1784*** promo
1785Supplements `base/containers/`.
1786***
1787
danakja6f71cb12021-12-15 21:04:491788### Container utilities <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:131789
1790```c++
1791auto it = absl::c_find(container, value);
1792```
1793
1794**Description:** Container-based versions of algorithmic functions within C++
1795standard library.
1796
1797**Documentation:**
1798[container.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/algorithm/container.h)
1799
1800**Notes:**
1801*** promo
1802Overlaps with `base/ranges/algorithm.h`.
1803***
1804
danakja6f71cb12021-12-15 21:04:491805### FunctionRef <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:131806
1807```c++
1808absl::FunctionRef
1809```
1810
1811**Description:** Type for holding a non-owning reference to an object of any
1812invocable type.
1813
1814**Documentation:**
1815[function_ref.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/function_ref.h)
1816
1817**Notes:**
1818*** promo
1819None
1820***
1821
danakja6f71cb12021-12-15 21:04:491822### Random <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:131823
1824```c++
1825absl::BitGen bitgen;
1826size_t index = absl::Uniform(bitgen, 0u, elems.size());
1827```
1828
1829**Description:** Functions and utilities for generating pseudorandom data.
1830
1831**Documentation:** [Random library](https://abseil.io/docs/cpp/guides/random)
1832
1833**Notes:**
1834*** promo
1835Overlaps with `base/rand_util.h`.
1836***
1837
danakja6f71cb12021-12-15 21:04:491838### StatusOr <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:131839
1840```c++
1841absl::StatusOr<T>
1842```
1843
1844**Description:** An object that is either a usable value, or an error Status
1845explaining why such a value is not present.
1846
1847**Documentation:**
1848[statusor.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/status/statusor.h)
1849
1850**Notes:**
1851*** promo
1852None
1853***
1854
danakja6f71cb12021-12-15 21:04:491855### String Formatting <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:131856
1857```c++
1858absl::StrFormat
1859```
1860
1861**Description:** A typesafe replacement for the family of printf() string
1862formatting routines.
1863
1864**Documentation:**
1865[String Formatting](https://abseil.io/docs/cpp/guides/format)
1866
1867**Notes:**
1868*** promo
1869None
1870***
1871
danakja6f71cb12021-12-15 21:04:491872### Strings Library <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:131873
1874```c++
1875absl::StrSplit
1876absl::StrJoin
1877absl::StrCat
1878absl::StrAppend
1879absl::Substitute
1880absl::StrContains
1881```
1882
1883**Description:** Classes and utility functions for manipulating and comparing
1884strings.
1885
1886**Documentation:**
1887[String Utilities](https://abseil.io/docs/cpp/guides/strings)
1888
1889**Notes:**
1890*** promo
1891Overlaps with `base/strings`.
1892***
1893
danakja6f71cb12021-12-15 21:04:491894### Synchronization <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:131895
1896```c++
1897absl::Mutex
1898```
1899
1900**Description:** Primitives for managing tasks across different threads.
1901
1902**Documentation:**
1903[Synchronization](https://abseil.io/docs/cpp/guides/synchronization)
1904
1905**Notes:**
1906*** promo
1907Overlaps with `Lock` in `base/synchronization/`.
1908***
1909
danakja6f71cb12021-12-15 21:04:491910### Time library <sup>[tbd]</sup>
Joe Masonfe4f2562021-09-15 15:23:131911
1912```c++
1913absl::Duration
1914absl::Time
1915absl::TimeZone
1916absl::CivilDay
1917```
1918
1919**Description:** Abstractions for holding time values, both in terms of
1920absolute time and civil time.
1921
1922**Documentation:** [Time](https://abseil.io/docs/cpp/guides/time)
1923
1924**Notes:**
1925*** promo
1926Overlaps with `Time` APIs in `base/`.
1927***