When a C++17 codebase is rebuilt with -std=c++20, which language and library changes most often stop it compiling, and how do you fix them?
answer
- new keywords collide with identifiers
- u8 literals change type
- user-declared constructor ends aggregate
- reversed operator== candidates
- removed result_of and allocator members
basics
~20 sThe usual breaks are new keywords (concept, requires, char8_t, co_await), u8 literals becoming char8_t, classes with user-declared constructors losing aggregate status, reversed operator== candidates, and removed library names such as std::result_of. Fix each mode-neutrally, then rebuild dependencies.
solid answer
~40 sC++20's compatibility annex lists the breaks. **New keywords** — `char8_t`, `concept`, `consteval`, `constinit`, `co_await`, `co_yield`, `co_return`, `requires` — turn old identifiers into errors. **UTF-8 literals** become `const char8_t[]`, so `std::string s = u8"x";` fails. A class with **any user-declared constructor** is no longer an aggregate, so `Settings s{5, 1000};` after `Settings() = default;` stops compiling. `operator==` gains **reversed candidates**, making a non-const member `operator==` ambiguous. The library drops `std::result_of`, `std::uncaught_exception`, several `std::allocator` members and `throw()`. Fix each in a way that compiles in both modes, run a C++20 CI job beside C++17, and rebuild every dependency in one mode: some declarations change between modes, so mixed objects can fail to link.
code
cpp · 19 lines#include <string>
struct Settings { // C++17: still an aggregate; C++20: not one
Settings() = default; // any user-declared constructor ends aggregate status in C++20
int retries = 3;
int timeout_ms = 500;
};
struct Id {
int value;
bool operator==(const Id& o) { return value == o.value; } // missing const
};
int main() {
Settings s{5, 1000}; // aggregate initialization
std::string banner = u8"ready"; // u8 literal is const char8_t[] in C++20
Id a{1}, b{1};
return (a == b) && s.retries == 5 && !banner.empty() ? 0 : 1;
}go deeper
Know that raising the standard can break old code: new keywords, changed literal types and removed library names, with std::auto_ptr in C++17 as the classic example.
Explain two or three concrete C++20 breaks — char8_t literals, the aggregate rule, reversed == candidates — and the fix for each that compiles in both modes.
Walk through a migration you ran: a parallel CI job, mode-neutral fixes, dependencies rebuilt in one mode, and how you caught issues one compiler only warned about.
Size the migration from the compatibility annex and the dependency graph before committing, and sequence it so the C++17 build stays shippable throughout.
## Why a mode switch breaks working code Every revision lists, in its compatibility annex, the valid code of the previous revision that is no longer valid or now means something else. Moving a codebase from C++17 to C++20 means working through that list. Most items are mechanical, and a small set accounts for most failures. ## Language changes that break builds | Change in C++20 | C++17 code that breaks | Mode-neutral fix | |---|---|---| | new keywords `char8_t`, `concept`, `consteval`, `constinit`, `co_await`, `co_yield`, `co_return`, `requires` | a variable or function named `requires` or `concept` | rename the identifier | | UTF-8 literals have type `const char8_t[]` and `char8_t` | `std::string s = u8"text";`, `f(u8"text")` for `f(const char*)` | drop the `u8` prefix where the ordinary literal encoding is UTF-8, or convert explicitly | | a class with **any user-declared constructor** is never an aggregate | `struct S { S() = default; int a, b; }; S s{1, 2};` | remove the defaulted constructor or write a real one | | `operator==` and `!=` consider **reversed and rewritten candidates** | a non-const member `bool operator==(const T&)` becomes ambiguous | make comparison members `const`, or default them | | `throw()` is removed | `void f() throw();` | `noexcept` | Two details matter in practice. Apple clang 21 diagnoses the `==` case only as a warning, `-Wambiguous-reversed-operator`, although the standard calls it ambiguous — another compiler may reject the same line. And GCC and Clang accept `-fno-char8_t` to restore the old literal types: a temporary bridge, not a fix. The `<=` token followed by `>` now lexes as `<=>`, and `module` and `import` at the start of a line can be read as module declarations; both are rare in real code. ## Library removals C++20 removed several facilities C++17 had deprecated: - **`std::result_of`** and `std::is_literal_type` — use `std::invoke_result`. - **`std::uncaught_exception`** — use `std::uncaught_exceptions`. - `std::allocator`'s `pointer`, `reference`, `rebind`, `construct`, `destroy` and `max_size` members — go through **`std::allocator_traits`**. - `std::not1`, `std::not2` and the `argument_type` / `result_type` typedefs of standard function objects — use `std::not_fn` and `decltype`. - `shared_ptr::unique`, `std::raw_storage_iterator`, `std::get_temporary_buffer`. - the empty headers `<ccomplex>`, `<ciso646>`, `<cstdalign>`, `<cstdbool>` and `<ctgmath>`. - `std::cout << u8"text"` is now ill-formed, because C++20 declares the `char8_t` overloads of `operator<<` as deleted. Libraries delete removed names at different speeds: libc++ hides `std::result_of` in C++20 mode unless `_LIBCPP_ENABLE_CXX20_REMOVED_TYPE_TRAITS` is defined, while libstdc++ 16 still declares it with a deprecation warning. Code can therefore build against libstdc++ and fail against libc++. Old third-party code is where most of these hide; a dependency pinned to an old release can block the whole upgrade. ## Changes that only warn Some C++20 changes are deprecations, so the build succeeds with a warning: implicit capture of `this` through `[=]` (`-Wdeprecated-this-capture` in Clang) is the common one. Fix these in the same pass — deprecated features are the next revision's removals. ## Changes that compile silently The dangerous items are the ones that still compile. The annex lists a few: - **Implicit move on return** widened: returning a local of a derived type, or an rvalue-reference parameter, can now pick a move constructor where C++17 copied. The annex's example shows a class whose source object is left empty, so later code reading it has undefined behaviour. - **Equality operators** may silently select a different overload, because reversed candidates join overload resolution. - **Lambda capture defaults** may capture entities that C++17 did not capture. These do not show up as build failures, so the migration must run the full test suite, ideally under sanitizers, rather than stop at a green compile. ## Rebuild everything in one mode A mode switch can change **library declarations**, not only grammar. The compatibility annex notes that `std::list::remove` and `std::forward_list::remove` now return `size_type` instead of `void`, and that translation units compiled as C++20 may fail to link with, or have undefined behavior alongside, units compiled as C++17. The safe rule: every translation unit and every prebuilt library in one program uses the same standard mode. That pulls the package manager into the migration. ## A working order 1. Add a **non-gating CI job** that builds with `-std=c++20` (and `/std:c++20` on MSVC) next to the existing C++17 job, on every compiler. 2. Fix errors in a way that **compiles in both modes**, so the C++17 build stays green. 3. Clear the **deprecation warnings** while the context is fresh. 4. Rebuild **all dependencies** in C++20 mode and run the full test suite, including sanitizer builds. 5. Flip the default, delete the C++17 job, and only then start using C++20 features.
- Why does a defaulted constructor stop aggregate initialization in C++20 but not in C++17?C++17 allowed a class with user-declared but not user-provided constructors, such as `S() = default;` or `S() = delete;`, to remain an aggregate, so `S{1, 2}` bypassed the declared constructors entirely — even a deleted one. C++20 closed the hole: any user-declared constructor makes the class a non-aggregate. Delete the defaulted declaration if you want aggregate initialization.
- Is -fno-char8_t a reasonable way to finish a C++20 migration?Only as a bridge. It restores the C++17 types of `u8` literals so the rest of the migration can proceed, but it is a GCC and Clang flag, so MSVC builds need their own handling, and it keeps the codebase on a dialect that differs from standard C++20. Fix the literal uses, then remove the flag.
- C++20 only adds features, so valid C++17 code always still compiles.
- Objects built with -std=c++17 and -std=c++20 can always be linked safely.
- A clean build on one compiler proves the C++20 migration is done.
- If one standard library still accepts std::result_of in C++20 mode, the code is portable C++20.
- u8 string literals still have type const char[] in C++20.
Assumes the C++20 changes listed in the compatibility annex, which C++23 keeps; checked with Apple clang 21.