| RFC-0258: Update from C++17 to C++20 ||
|---|---|
| Status | Accepted |
| 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 | - [42064981](https://fxbug.dev/42064981) |
| Gerrit change | - [1092960](https://fuchsia-review.googlesource.com/c/fuchsia/+/1092960) |
| Authors | - [phosek@google.com](https://fuchsia-review.googlesource.com/q/owner:phosek@google.com) |
| Reviewers | - [ianloic@google.com](https://fuchsia-review.googlesource.com/q/owner:ianloic@google.com) - [jamesr@google.com](https://fuchsia-review.googlesource.com/q/owner:jamesr@google.com) - [mcgrathr@google.com](https://fuchsia-review.googlesource.com/q/owner:mcgrathr@google.com) |
| Date submitted (year-month-day) | 2024-08-01 |
| Date reviewed (year-month-day) | 2024-09-10 |

[Edit this RFC](https://ci.android.com/edit?repo=fuchsia/fuchsia/main&file=docs/contribute/governance/rfcs/0258_update_from_c++17_to_c++20.md)
[Edit RFC metadata](https://ci.android.com/edit?repo=fuchsia/fuchsia/main&file=docs/contribute/governance/rfcs/_rfcs.yaml&searchAndJump=-+name:+'RFC-0258')

## Summary

Following the process outlined in [RFC-0193: Supported C++ Versions](https://fuchsia.dev/docs/contribute/governance/rfcs/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](https://fuchsia.dev/docs/contribute/governance/rfcs/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](https://fxrev.dev/1093351) 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](https://cs.opensource.google/fuchsia/fuchsia/+/main:sdk/lib/stdcompat/;drc=4b39193a714dcf9302814877dc900f7548102083) 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](https://cs.opensource.google/fuchsia/fuchsia/+/main:build/bazel_sdk/tests/fuchsia/cpp_variants/;drc=447e9827598cecaf325359f7e0f79228380f1fb7) that tries to compile all C++ headers
in the SDK using C++17 and C++20 to [ensure](https://fxrev.dev/939203) 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](https://cs.opensource.google/fuchsia/fuchsia/+/main:sdk/lib/stdcompat/test/BUILD.gn;l=105;drc=8fdb1718dedced8a757b467d070cb0879b1ab948).

## Documentation

The [SDK documentation](https://fuchsia.dev/docs/development/idk/documentation/compilation) 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](https://fuchsia.dev/docs/development/languages/c-cpp/cxx) 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](https://cs.opensource.google/fuchsia/fuchsia/+/main:sdk/lib/stdcompat/;drc=4b39193a714dcf9302814877dc900f7548102083) 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](https://google.github.io/styleguide/cppguide.html#C++_Version). 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.
[](https://issues.fuchsia.dev/issues/new?component=1439230&template=1881640&description=Issue+on:+https://fuchsia.dev/docs/contribute/governance/rfcs/0258_update_from_c++17_to_c++20.md "Report a bug or suggest improvements on this page") [](https://cs.opensource.google/fuchsia/fuchsia/+/main:docs/contribute/governance/rfcs/0258_update_from_c++17_to_c++20.md "View the source code of this page") [](https://ci.android.com/edit?repo=fuchsia/fuchsia/main&file=docs/contribute/governance/rfcs/0258_update_from_c++17_to_c++20.md&cloneRepo=fuchsia/fuchsia:refs/heads/main&fullPath=fuchsia/docs/contribute/governance/rfcs/0258_update_from_c++17_to_c++20.md "Edit the source code of this page")