Modern C++ use in Chromium

This document lives at src/styleguide/c++/c++11.html in a Chromium checkout and 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:

C++11: Default allowed; see banned features below

C++14: Default allowed; see banned features below

C++17: Not yet supported in Chromium, unlikely before mid-2021; tracking bug

C++20: Not yet standardized

Abseil: Initially supported July 31, 2020; see allowed/banned/TBD features below absl::StatusOr: Initially supported September 3, 2020

Initially supported July 31, 2020; see allowed/banned/TBD features below

Table of Contents

Allowed Features Library Abseil Banned Features Language C++11 Library C++11 C++14 Abseil To Be Discussed Library Abseil

C++11 Banned Language Features

The following C++11 language features are not allowed in the Chromium codebase.

Feature or Library Snippet Description Documentation Link Notes and Discussion Thread Inline Namespaces inline namespace foo { ... } Allows better versioning of namespaces Inline namespaces Banned in the Google Style Guide. Unclear how it will work with components. long long Type long long var = value; An integer of at least 64 bits Fundamental types Use a stdint.h type if you need a 64-bit number. Discussion thread User-Defined Literals type var = literal_value_type Allows user-defined literal expressions User-defined literals Banned in the Google Style Guide. thread_local storage class thread_local int foo = 1; Puts variables into thread local storage. Storage duration Some surprising effects on Mac (discussion, fork). Use base::SequenceLocalStorageSlot for sequence support, and base::ThreadLocal / base::ThreadLocalStorage otherwise.

C++11 Banned Library Features

The following C++11 library features are not allowed in the Chromium codebase.

C++14 Banned Library Features

The following C++14 library features are not allowed in the Chromium codebase.

Feature Snippet Description Documentation Link Notes and Discussion Thread std::chrono literals using namespace std::chrono_literals;

auto timeout = 30s; Allows std::chrono types to be more easily constructed. std::literals::chrono_literals::operator""s Banned because <chrono> is banned.

Abseil Allowed Library Features

The following Abseil library features are allowed in the Chromium codebase.

Feature Snippet Description Documentation Link Notes and Discussion Thread Variant absl::variant Early adaptation of C++17 std::variant. std::variant Discussion thread

Abseil Banned Library Features

The following Abseil library features are not allowed in the Chromium codebase.

Feature Snippet Description Documentation Link Notes and Discussion Thread Any absl::any a = int{5};

EXPECT_THAT(absl::any_cast<int>(&a), Pointee(5));

EXPECT_EQ(absl::any_cast<size_t>(&a), nullptr); Early adaptation of C++17 std::any. std::any Banned since workaround for lack of RTTI isn't compatible with the component build. Bug Command line flags ABSL_FLAG(bool, logs, false, "print logs to stderr");

app --logs=true; Allows programmatic access to flag values passed on the command-line to binaries. Flags Library Banned since workaround for lack of RTTI isn't compatible with the component build. Bug

Use base::CommandLine instead. Span absl::Span Early adaptation of C++20 std::span. Using absl::Span Banned due to being less std::-compliant than base::span .

Keep using base::span . string_view absl::string_view Early adaptation of C++17 std::string_view. absl::string_view Banned due to only working with 8-bit characters. Keep using base::StringPiece from base/strings/ .

Abseil TBD Features

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.