This document is part of the more general Chromium C++ style guide. It summarizes the supported state of new and updated language and library features in recent C++ standards and the Abseil library. This guide applies to both Chromium and its subprojects, though subprojects can choose to be more restrictive if necessary for toolchain support.
The C++ language has in recent years received an updated standard every three years (C++11, C++14, C++17). For various reasons, Chromium does not immediately allow new features on the publication of such a standard. Instead, once toolchain support is sufficient, a standard is declared “initially supported”, with new language/library features banned pending discussion.
You can propose changing the status of a feature by sending an email to cxx@chromium.org. Include a short blurb on what the feature is and why you think it should or should not be allowed, along with links to any relevant previous discussion. If the list arrives at some consensus, send a codereview to change this file accordingly, linking to your discussion thread.
If an item remains on the TBD list two years after initial support is added, style arbiters should explicitly move it to an appropriate allowlist or blocklist, allowing it if there are no obvious reasons to ban.
The current status of existing standards and Abseil features is:
The following C++11 language features are not allowed in the Chromium codebase.
inline namespace foo { ... }
Description: Allows better versioning of namespaces.
Documentation: Inline namespaces
Notes:
long long var = value;
Description: An integer of at least 64 bits.
Documentation: Fundamental types
Notes:
type var = literal_value_type;
Description: Allows user-defined literal expressions.
Documentation: User-defined literals
Notes:
thread_local int foo = 1;
Description: Puts variables into thread local storage.
Documentation: Storage duration
Notes:
base::SequenceLocalStorageSlot
for sequence support, and base::ThreadLocal
/base::ThreadLocalStorage
otherwise.The following C++11 library features are not allowed in the Chromium codebase.
std::bind(function, args, ...)
Description: Declares a function object bound to certain arguments
Documentation: std::bind
Notes:
base::Bind
instead. Compared to std::bind
, base::Bind
helps prevent lifetime issues by preventing binding of capturing lambdas and by forcing callers to declare raw pointers as Unretained
. Discussion thread#include <cfenv> #include <fenv.h>
Description: Provides floating point status flags and control modes for C-compatible code
Documentation: Standard library header “cfenv”
Notes:
#include <chrono>
Description: A standard date and time library
Documentation: Date and time utilities
Notes:
Time
APIs in base/
. Keep using the base/
classes.#include <exception>
Description: Enhancements to exception throwing and handling
Documentation: Standard library header “exception”
Notes:
noexcept
specifier is explicitly allowed. Discussion threadstd::function
Description: Wraps a standard polymorphic function
Documentation: std::function
Notes:
base::{Once,Repeating}Callback
instead. Compared to std::function
, base::{Once,Repeating}Callback
directly supports Chromium's refcounting classes and weak pointers and deals with additional thread safety concerns. Discussion thread<random>
(see separate item for random number distributions), e.g.: linear_congruential_engine
, mersenne_twister_engine
, minstd_rand0
, mt19937
, ranlinux48
, random_device
Description: Random number generation algorithms and utilities.
Documentation: Pseudo-random number generation
Notes:
<random>
. Instead, use base::RandomBitGenerator
. Discussion threadstd::ratio<numerator, denominator>
Description: Provides compile-time rational numbers
Documentation: std::ratio
Notes:
#include <regex>
Description: A standard regular expressions library
Documentation: Regular expressions library
Notes:
re2
.std::shared_ptr
Description: Allows shared ownership of a pointer through reference counts
Documentation: std::shared_ptr
Notes:
Needs a lot more evaluation for Chromium, and there isn't enough of a push for this feature.
std::stoi() std::stol() std::stoul() std::stoll std::stoull() std::stof() std::stod() std::stold() std::to_string()
Description: Converts strings to/from numbers
Documentation:
Notes:
base/strings/string_number_conversions.h
instead.<thread>
and related headers, including <future>
, <mutex>
, <condition_variable>
Description: Provides a standard multithreading library using std::thread
and associates
Documentation: Thread support library
Notes:
base/
. Keep using the base/
classes for now. base::Thread
is tightly coupled to MessageLoop
which would make it hard to replace. We should investigate using standard mutexes, or unique_lock, etc. to replace our locking/synchronization classes.std::weak_ptr
Description: Allows a weak reference to a std::shared_ptr
Documentation: std::weak_ptr
Notes:
std::shared_ptr
is banned. Use base::WeakPtr
instead.The following C++14 library features are not allowed in the Chromium codebase.
using namespace std::chrono_literals; auto timeout = 30s;
Description: Allows std::chrono
types to be more easily constructed.
Documentation: std::literals::chrono_literals::operator""s
Notes:
<chrono>
is banned.The following Abseil library features are allowed in the Chromium codebase.
absl::optional
Description: Early adaptation of C++17 std::optional
.
Documentation: std::optional
Notes:
base::Optional
. Discussion threadabsl::Status
Description: Type for returning detailed errors.
Documentation: status.h
Notes:
Approved for use inside a wrapper type. Use abseil_string_conversions.h to convert to and from absl::string_view so the wrapper can expose base::StringPiece. Use absl::Cord directly as minimally necessary to interface; do not expose in the wrapper type API.
absl::variant
Description: Early adaptation of C++17 std::variant
.
Documentation: std::variant
Notes:
The following Abseil library features are not allowed in the Chromium codebase.
absl::any a = int{5}; EXPECT_THAT(absl::any_cast<int>(&a), Pointee(5)); EXPECT_EQ(absl::any_cast<size_t>(&a), nullptr);
Description: Early adaptation of C++17 std::any
.
Documentation: std::any
Notes:
ABSL_FLAG(bool, logs, false, "print logs to stderr"); app --logs=true;
Description: Allows programmatic access to flag values passed on the command-line to binaries.
Documentation: Flags Library
Notes:
base::CommandLine
instead.absl::Span
Description: Early adaptation of C++20 std::span
.
Documentation: Using absl::Span
Notes:
base::span
. Keep using base::span
.absl::string_view
Description: Early adaptation of C++17 std::string_view
.
Documentation: absl::string_view
Notes:
base::StringPiece
from base/strings/
.The following Abseil library features are not allowed in the Chromium codebase. See the top of this page on how to propose moving a feature from this list into the allowed or banned sections.
uint64_t a; absl::uint128 v = a;
Description: Signed and unsigned 128-bit integer types meant to mimic intrinsic types as closely as possible.
Documentation: Numerics
Notes:
absl::bind_front
Description: Binds the first N arguments of an invocable object and stores them by value.
Documentation:
Notes:
base::Bind
.FILE* sink_file = fopen(sink_path, "w"); auto sink_closer = absl::MakeCleanup([sink_file] { fclose(sink_file); });
Description: Implements the scope guard idiom, invoking the contained callback's operator()() &&
on scope exit.
Documentation: cleanup.h
Notes:
defer
in Golang.absl::flat_hash_map absl::flat_hash_set absl::node_hash_map absl::node_hash_set absl::btree_map absl::btree_set absl::btree_multimap absl::btree_multiset absl::InlinedVector absl::FixedArray
Description: Alternatives to STL containers designed to be more efficient in the general case.
Documentation:
Notes:
base/containers/
.auto it = absl::c_find(container, value);
Description: Container-based versions of algorithmic functions within C++ standard library.
Documentation: container.h
Notes:
base/ranges/algorithm.h
.absl::FunctionRef
Description: Type for holding a non-owning reference to an object of any invocable type.
Documentation: function_ref.h
Notes:
absl::BitGen bitgen; size_t index = absl::Uniform(bitgen, 0u, elems.size());
Description: Functions and utilities for generating pseudorandom data.
Documentation: Random library
Notes:
base/rand_util.h
.absl::StatusOr<T>
Description: An object that is either a usable value, or an error Status explaining why such a value is not present.
Documentation: statusor.h
Notes:
absl::StrFormat
Description: A typesafe replacement for the family of printf() string formatting routines.
Documentation: String Formatting
Notes:
absl::StrSplit absl::StrJoin absl::StrCat absl::StrAppend absl::Substitute absl::StrContains
Description: Classes and utility functions for manipulating and comparing strings.
Documentation: String Utilities
Notes:
base/strings
.absl::Mutex
Description: Primitives for managing tasks across different threads.
Documentation: Synchronization
Notes:
Lock
in base/synchronization/
.absl::Duration absl::Time absl::TimeZone absl::CivilDay
Description: Abstractions for holding time values, both in terms of absolute time and civil time.
Documentation: Time
Notes:
Time
APIs in base/
.