blob: 40f912f0500a9faede5d3a2cf07394a5c8ae831d [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
12years (C++11, C++14, C++17). For various reasons, Chromium does not immediately
13allow 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_
32* **C++17:** _Not yet supported in Chromium, unlikely before mid-2021;
33 [tracking bug](https://crbug.com/752720)_
34* **C++20:** _Not yet standardized_
35* **Abseil:** Initially supported July 31, 2020; see allowed/banned/TBD
36 features below
37 * absl::StatusOr: Initially supported September 3, 2020
38 * absl::Cleanup: Initially supported February 4, 2021
39
40[TOC]
41
42## C++11 Banned Language Features {#core-blocklist}
43
44The following C++11 language features are not allowed in the Chromium codebase.
45
46### Inline Namespaces
47
48```c++
49inline namespace foo { ... }
50```
51
52**Description:** Allows better versioning of namespaces.
53
54**Documentation:**
55[Inline namespaces](https://en.cppreference.com/w/cpp/language/namespace#Inline_namespaces)
56
57**Notes:**
58*** promo
59Banned in the
60[Google Style Guide](https://google.github.io/styleguide/cppguide.html#Namespaces).
61Unclear how it will work with components.
62***
63
64### long long Type
65
66```c++
67long long var = value;
68```
69
70**Description:** An integer of at least 64 bits.
71
72**Documentation:**
73[Fundamental types](https://en.cppreference.com/w/cpp/language/types)
74
75**Notes:**
76*** promo
77Use a stdint.h type if you need a 64-bit number.
78[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/chromium-dev/RxugZ-pIDxk)
79***
80
81### User-Defined Literals
82
83```c++
84type var = literal_value_type;
85```
86
87**Description:** Allows user-defined literal expressions.
88
89**Documentation:**
90[User-defined literals](https://en.cppreference.com/w/cpp/language/user_literal)
91
92**Notes:**
93*** promo
94Banned in the
95[Google Style Guide](https://google.github.io/styleguide/cppguide.html#Operator_Overloading).
96***
97
98### thread_local Storage Class
99
100```c++
101thread_local int foo = 1;
102```
103
104**Description:** Puts variables into thread local storage.
105
106**Documentation:**
107[Storage duration](https://en.cppreference.com/w/cpp/language/storage_duration)
108
109**Notes:**
110*** promo
111Some surprising effects on Mac
112([discussion](https://groups.google.com/a/chromium.org/forum/#!topic/chromium-dev/2msN8k3Xzgs),
113[fork](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/h7O5BdtWCZw)).
114Use `base::SequenceLocalStorageSlot` for sequence support, and
115`base::ThreadLocal`/`base::ThreadLocalStorage` otherwise.
116***
117
118## C++11 Banned Library Features {#library-blocklist}
119
120The following C++11 library features are not allowed in the Chromium codebase.
121
122### Bind Operations
123
124```c++
125std::bind(function, args, ...)
126```
127
128**Description:** Declares a function object bound to certain arguments
129
130**Documentation:**
131[std::bind](https://en.cppreference.com/w/cpp/utility/functional/bind)
132
133**Notes:**
134*** promo
135Use `base::Bind` instead. Compared to `std::bind`, `base::Bind` helps prevent
136lifetime issues by preventing binding of capturing lambdas and by forcing
137callers to declare raw pointers as `Unretained`.
138[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/SoEj7oIDNuA)
139***
140
141### C Floating-Point Environment
142
143```c++
144#include <cfenv>
145#include <fenv.h>
146```
147
148**Description:** Provides floating point status flags and control modes for
149C-compatible code
150
151**Documentation:**
152[Standard library header "cfenv"](https://en.cppreference.com/w/cpp/header/cfenv)
153
154**Notes:**
155*** promo
156Banned by the
157[Google Style Guide](https://google.github.io/styleguide/cppguide.html#C++11)
158due to concerns about compiler support.
159***
160
161### Date and time utilities
162
163```c++
164#include <chrono>
165```
166
167**Description:** A standard date and time library
168
169**Documentation:**
170[Date and time utilities](https://en.cppreference.com/w/cpp/chrono)
171
172**Notes:**
173*** promo
174Overlaps with `Time` APIs in `base/`. Keep using the `base/` classes.
175***
176
177### Exceptions
178
179```c++
180#include <exception>
181```
182
183**Description:** Enhancements to exception throwing and handling
184
185**Documentation:**
186[Standard library header "exception"](https://en.cppreference.com/w/cpp/header/exception)
187
188**Notes:**
189*** promo
190Exceptions are banned by the
191[Google Style Guide](https://google.github.io/styleguide/cppguide.html#Exceptions)
192and disabled in Chromium compiles. However, the `noexcept` specifier is
193explicitly allowed.
194[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/chromium-dev/8i4tMqNpHhg)
195***
196
197### Function Objects
198
199```c++
200std::function
201```
202
203**Description:** Wraps a standard polymorphic function
204
205**Documentation:**
206[std::function](https://en.cppreference.com/w/cpp/utility/functional/function)
207
208**Notes:**
209*** promo
210Use `base::{Once,Repeating}Callback` instead. Compared to `std::function`,
211`base::{Once,Repeating}Callback` directly supports Chromium's refcounting
212classes and weak pointers and deals with additional thread safety concerns.
213[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/SoEj7oIDNuA)
214***
215
216### Random Number Engines
217
218*** aside
219The random number engines defined in `<random>` (see separate item for random
220number distributions), e.g.: `linear_congruential_engine`,
221`mersenne_twister_engine`, `minstd_rand0`, `mt19937`, `ranlinux48`,
222`random_device`
223***
224
225**Description:** Random number generation algorithms and utilities.
226
227**Documentation:**
228[Pseudo-random number generation](https://en.cppreference.com/w/cpp/numeric/random)
229
230**Notes:**
231*** promo
232Do not use any random number engines from `<random>`. Instead, use
233`base::RandomBitGenerator`.
234[Discussion thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/16Xmw05C-Y0)
235***
236
237### Ratio Template Class
238
239```c++
240std::ratio<numerator, denominator>
241```
242
243**Description:** Provides compile-time rational numbers
244
245**Documentation:**
246[std::ratio](https://en.cppreference.com/w/cpp/numeric/ratio/ratio)
247
248**Notes:**
249*** promo
250Banned by the
251[Google Style Guide](https://google.github.io/styleguide/cppguide.html#C++11)
252due to concerns that this is tied to a more template-heavy interface style.
253***
254
255### Regular Expressions
256
257```c++
258#include <regex>
259```
260
261**Description:** A standard regular expressions library
262
263**Documentation:**
264[Regular expressions library](https://en.cppreference.com/w/cpp/regex)
265
266**Notes:**
267*** promo
268Overlaps with many regular expression libraries in Chromium. When in doubt, use
269`re2`.
270***
271
272### Shared Pointers
273
274```c++
275std::shared_ptr
276```
277
278**Description:** Allows shared ownership of a pointer through reference counts
279
280**Documentation:**
281[std::shared_ptr](https://en.cppreference.com/w/cpp/memory/shared_ptr)
282
283**Notes:**
284*** promo
285Needs a lot more evaluation for Chromium, and there isn't enough of a push for
286this feature.
287
288* [Google Style Guide](https://google.github.io/styleguide/cppguide.html#Ownership_and_Smart_Pointers).
289* [Discussion Thread](https://groups.google.com/a/chromium.org/forum/#!topic/cxx/aT2wsBLKvzI)
290***
291
292### String-Number Conversion Functions
293
294```c++
295std::stoi()
296std::stol()
297std::stoul()
298std::stoll
299std::stoull()
300std::stof()
301std::stod()
302std::stold()
303std::to_string()
304```
305
306**Description:** Converts strings to/from numbers
307
308**Documentation:**
309* [std::stoi, std::stol, std::stoll](https://en.cppreference.com/w/cpp/string/basic_string/stol),
310* [std::stoul, std::stoull](https://en.cppreference.com/w/cpp/string/basic_string/stoul),
311* [std::stof, std::stod, std::stold](https://en.cppreference.com/w/cpp/string/basic_string/stof),
312* [std::to_string](https://en.cppreference.com/w/cpp/string/basic_string/to_string)
313
314**Notes:**
315*** promo
316The string-to-number conversions rely on exceptions to communicate failure,
317while the number-to-string conversions have performance concerns and depend on
318the locale. Use the routines in `base/strings/string_number_conversions.h`
319instead.
320***
321
322### Thread Library
323
324*** aside
325`<thread>` and related headers, including `<future>`, `<mutex>`,
326`<condition_variable>`
327***
328
329**Description:** Provides a standard multithreading library using `std::thread`
330and associates
331
332**Documentation:**
333[Thread support library](https://en.cppreference.com/w/cpp/thread)
334
335**Notes:**
336*** promo
337Overlaps with many classes in `base/`. Keep using the `base/` classes for now.
338`base::Thread` is tightly coupled to `MessageLoop` which would make it hard to
339replace. We should investigate using standard mutexes, or unique_lock, etc. to
340replace our locking/synchronization classes.
341***
342
343### Weak Pointers
344
345```c++
346std::weak_ptr
347```
348
349**Description:** Allows a weak reference to a `std::shared_ptr`
350
351**Documentation:**
352[std::weak_ptr](https://en.cppreference.com/w/cpp/memory/weak_ptr)
353
354**Notes:**
355*** promo
356Banned because `std::shared_ptr` is banned. Use `base::WeakPtr` instead.
357***
358
359## C++14 Banned Library Features {#library-blocklist-14}
360
361The following C++14 library features are not allowed in the Chromium codebase.
362
363### std::chrono literals
364
365```c++
366using namespace std::chrono_literals;
367auto timeout = 30s;
368```
369
370**Description:** Allows `std::chrono` types to be more easily constructed.
371
372**Documentation:**
373[std::literals::chrono_literals::operator""s](https://en.cppreference.com/w/cpp/chrono/operator%22%22s)
374
375**Notes:**
376*** promo
377Banned because `<chrono>` is banned.
378***
379
380## Abseil Allowed Library Features {#absl-allowlist}
381
382The following Abseil library features are allowed in the Chromium codebase.
383
384### Optional
385
386```c++
387absl::optional
388```
389
390**Description:** Early adaptation of C++17 `std::optional`.
391
392**Documentation:**
393[std::optional](https://en.cppreference.com/w/cpp/utility/optional)
394
395**Notes:**
396*** promo
397Replaces `base::Optional`.
398[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/zUGqagX1NFU)
399***
400
401### Status
402
403```c++
404absl::Status
405```
406
407**Description:** Type for returning detailed errors.
408
409**Documentation:**
410[status.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/status/status.h)
411
412**Notes:**
413*** promo
414Approved for use inside a wrapper type. Use
415[abseil_string_conversions.h](https://source.chromium.org/chromium/chromium/src/+/main:base/strings/abseil_string_conversions.h)
416to convert to and from
417[absl::string_view](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/strings/string_view.h)
418so the wrapper can expose
419[base::StringPiece](https://source.chromium.org/chromium/chromium/src/+/main:base/strings/string_piece.h).
420Use
421[absl::Cord](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/strings/cord.h)
422directly as minimally necessary to interface; do not expose in the wrapper type
423API.
424
425[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/ImdFCSZ-NMA)
426***
427
428### Variant
429
430```c++
431absl::variant
432```
433
434**Description:** Early adaptation of C++17 `std::variant`.
435
436**Documentation:**
437[std::variant](https://en.cppreference.com/w/cpp/utility/variant)
438
439**Notes:**
440*** promo
441[Discussion thread](https://groups.google.com/a/chromium.org/g/cxx/c/DqvG-TpvMyU)
442***
443
444## Abseil Banned Library Features {#absl-blocklist}
445
446The following Abseil library features are not allowed in the Chromium codebase.
447
448### Any
449
450```c++
451absl::any a = int{5};
452EXPECT_THAT(absl::any_cast<int>(&a), Pointee(5));
453EXPECT_EQ(absl::any_cast<size_t>(&a), nullptr);
454```
455
456**Description:** Early adaptation of C++17 `std::any`.
457
458**Documentation:** [std::any](https://en.cppreference.com/w/cpp/utility/any)
459
460**Notes:**
461*** promo
462Banned since workaround for lack of RTTI isn't compatible with the component
463build. ([Bug](https://crbug.com/1096380))
464***
465
466### Command line flags
467
468```c++
469ABSL_FLAG(bool, logs, false, "print logs to stderr");
470app --logs=true;
471```
472
473**Description:** Allows programmatic access to flag values passed on the
474command-line to binaries.
475
476**Documentation:** [Flags Library](https://abseil.io/docs/cpp/guides/flags)
477
478**Notes:**
479*** promo
480Banned since workaround for lack of RTTI isn't compatible with the component
481build. ([Bug](https://crbug.com/1096380)) Use `base::CommandLine` instead.
482***
483
484### Span
485
486```c++
487absl::Span
488```
489
490**Description:** Early adaptation of C++20 `std::span`.
491
492**Documentation:** [Using absl::Span](https://abseil.io/tips/93)
493
494**Notes:**
495*** promo
496Banned due to being less std::-compliant than `base::span`. Keep using
497`base::span`.
498***
499
500### string_view
501
502```c++
503absl::string_view
504```
505
506**Description:** Early adaptation of C++17 `std::string_view`.
507
508**Documentation:** [absl::string_view](https://abseil.io/tips/1)
509
510**Notes:**
511*** promo
512Banned due to only working with 8-bit characters. Keep using
513`base::StringPiece` from `base/strings/`.
514***
515
516## Abseil TBD Features {#absl-review}
517
518The following Abseil library features are not allowed in the Chromium codebase.
519See the top of this page on how to propose moving a feature from this list into
520the allowed or banned sections.
521
522### 128bit integer
523
524```c++
525uint64_t a;
526absl::uint128 v = a;
527```
528
529**Description:** Signed and unsigned 128-bit integer types meant to mimic
530intrinsic types as closely as possible.
531
532**Documentation:**
533[Numerics](https://abseil.io/docs/cpp/guides/numeric)
534
535**Notes:**
536*** promo
537None
538***
539
540### bind_front
541
542```c++
543absl::bind_front
544```
545
546**Description:** Binds the first N arguments of an invocable object and stores them by value.
547
548**Documentation:**
549* [bind_front.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/bind_front.h)
550* [Avoid std::bind](https://abseil.io/tips/108)
551
552**Notes:**
553*** promo
554Overlaps with `base::Bind`.
555***
556
557### Cleanup
558
559```c++
560FILE* sink_file = fopen(sink_path, "w");
561auto sink_closer = absl::MakeCleanup([sink_file] { fclose(sink_file); });
562```
563
564**Description:** Implements the scope guard idiom, invoking the contained
565callback's `operator()() &&` on scope exit.
566
567**Documentation:**
568[cleanup.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/cleanup/cleanup.h)
569
570**Notes:**
571*** promo
572Similar to `defer` in Golang.
573***
574
575### Containers
576
577```c++
578absl::flat_hash_map
579absl::flat_hash_set
580absl::node_hash_map
581absl::node_hash_set
582absl::btree_map
583absl::btree_set
584absl::btree_multimap
585absl::btree_multiset
586absl::InlinedVector
587absl::FixedArray
588```
589
590**Description:** Alternatives to STL containers designed to be more efficient
591in the general case.
592
593**Documentation:**
594* [Containers](https://abseil.io/docs/cpp/guides/container)
595* [Hash](https://abseil.io/docs/cpp/guides/hash)
596
597**Notes:**
598*** promo
599Supplements `base/containers/`.
600***
601
602### Container utilities
603
604```c++
605auto it = absl::c_find(container, value);
606```
607
608**Description:** Container-based versions of algorithmic functions within C++
609standard library.
610
611**Documentation:**
612[container.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/algorithm/container.h)
613
614**Notes:**
615*** promo
616Overlaps with `base/ranges/algorithm.h`.
617***
618
619### FunctionRef
620
621```c++
622absl::FunctionRef
623```
624
625**Description:** Type for holding a non-owning reference to an object of any
626invocable type.
627
628**Documentation:**
629[function_ref.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/functional/function_ref.h)
630
631**Notes:**
632*** promo
633None
634***
635
636### Random
637
638```c++
639absl::BitGen bitgen;
640size_t index = absl::Uniform(bitgen, 0u, elems.size());
641```
642
643**Description:** Functions and utilities for generating pseudorandom data.
644
645**Documentation:** [Random library](https://abseil.io/docs/cpp/guides/random)
646
647**Notes:**
648*** promo
649Overlaps with `base/rand_util.h`.
650***
651
652### StatusOr
653
654```c++
655absl::StatusOr<T>
656```
657
658**Description:** An object that is either a usable value, or an error Status
659explaining why such a value is not present.
660
661**Documentation:**
662[statusor.h](https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/status/statusor.h)
663
664**Notes:**
665*** promo
666None
667***
668
669### String Formatting
670
671```c++
672absl::StrFormat
673```
674
675**Description:** A typesafe replacement for the family of printf() string
676formatting routines.
677
678**Documentation:**
679[String Formatting](https://abseil.io/docs/cpp/guides/format)
680
681**Notes:**
682*** promo
683None
684***
685
686### Strings Library
687
688```c++
689absl::StrSplit
690absl::StrJoin
691absl::StrCat
692absl::StrAppend
693absl::Substitute
694absl::StrContains
695```
696
697**Description:** Classes and utility functions for manipulating and comparing
698strings.
699
700**Documentation:**
701[String Utilities](https://abseil.io/docs/cpp/guides/strings)
702
703**Notes:**
704*** promo
705Overlaps with `base/strings`.
706***
707
708### Synchronization
709
710```c++
711absl::Mutex
712```
713
714**Description:** Primitives for managing tasks across different threads.
715
716**Documentation:**
717[Synchronization](https://abseil.io/docs/cpp/guides/synchronization)
718
719**Notes:**
720*** promo
721Overlaps with `Lock` in `base/synchronization/`.
722***
723
724### Time library
725
726```c++
727absl::Duration
728absl::Time
729absl::TimeZone
730absl::CivilDay
731```
732
733**Description:** Abstractions for holding time values, both in terms of
734absolute time and civil time.
735
736**Documentation:** [Time](https://abseil.io/docs/cpp/guides/time)
737
738**Notes:**
739*** promo
740Overlaps with `Time` APIs in `base/`.
741***