RFC-0258: Update from C++17 to C++20

RFC-0258: Update from C++17 to C++20
StatusAccepted
Areas
  • Build
  • Languages and Libraries
  • Toolchain
Description

Update from C++17 to C++20 as the Fuchsia tree C++ version and expand the set of formally supported C++ versions in the SDK to include C++20.

Issues
Gerrit change
Authors
Reviewers
Date submitted (year-month-day)2024-08-01
Date reviewed (year-month-day)2024-09-10

Summary

Following the process outlined in RFC-0193: Supported C++ Versions, this RFC proposes updating from C++17 to C++20 as the Fuchsia tree C++ version and expanding the set of formally supported C++ versions in the SDK to include C++20.

Motivation

C++17 is the only supported version in the Fuchsia platform and the SDK according to RFC-0193: Supported C++ Versions. C++20 introduced a number of new features that significantly improve the ergonomics, but these improvements are currently unavailable to Fuchsia platform developers.

Stakeholders

Facilitator: davemoore@google.com

Reviewers:

  • ianloic@google.com
  • jamesr@google.com
  • mcgrathr@google.com

Socialization: Discussed among the Fuchsia C++ users and the toolchain team.

Requirements

  • Update the Fuchsia tree C++ version from C++17 to C++20.
  • Allow in-tree platform code to rely on C++20 features.

Non-goals

  • Make every feature in the C++20 standard immediately available in Fuchsia.

Design

The Fuchsia tree can be already compiled in either C++17 or C++20 modes. This will change the C++ version used by the Fuchsia tree from C++17 to C++20 after which it will no longer be an option to build in-tree (or "platform") code in the C++17 mode.

Implementation

The C++ standard used by the Fuchsia tree is controlled by a build argument. We plan to submit a change that updates the Fuchsia tree C++ version from C++17 to C++20, but ask Fuchsia developers to avoid using C++20 features until the RFC is approved, to preserve the ability to roll back to C++17 if needed.

There are existing CI/CQ builders that build Fuchsia tree in the C++20 mode. These will be eventually turned down and the freed up resources will be reused for testing future C++ versions (such as C++23).

Performance

C++20 expanded the use of compile time evaluation, including marking constructors for many of the standard containers as constexpr, which MAY improve runtime performance in certain cases, but have not observed any significant difference so far.

Ergonomics

Access to all C++20 standard library features—beyond what is already available in the stdcompat library—will reduce boilerplate and improve the C++ language usability.

Language features such as concepts or coroutines can improve ergonomics even further but their usage within the Fuchsia tree is outside of the scope for this RFC and SHOULD be covered by a future RFC.

Backwards Compatibility

C++17 MUST remain supported in the SDK. The in-tree (or "platform") code which is not part of the SDK API surface can start relying on C++20 features and thus become incompatible with C++17.

Security considerations

N/A

Privacy considerations

N/A

Testing

We have a basic SDK test that tries to compile all C++ headers in the SDK using C++17 and C++20 to ensure we do not introduce code incompatible with either version, but this test will not catch issues in the C++ source files.

To improve the SDK test coverage, we SHOULD compile tests for every package included in the SDK using C++17 and C++20 as was already done for stdcompat.

Documentation

The SDK documentation will be updated to reflect that C++20 is officially supported in addition to C++17; we will also document the constraints on C++20 feature support available to the SDK users.

C++ in Zircon will be updated to cover supported C++20 features and constraints on their use.

Drawbacks, alternatives, and unknowns

While we have been trying to provide a subset of the C++20 standard library functionality through the stdcompat library, implementing additional features would require an outsized investment and in some cases would be impossible without the C++20 language support.

Prior art and references

Google and Chromium already target C++20. The SDK already supports C++20 and partners like Google and Chromium rely on C++20 features, even though it has not been documented as formally supported.