Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Preprocessor, Tools and Concepts

What is new in C++20

A tour of the main C++20 features: concepts, ranges, coroutines, modules, the spaceship operator, std::format, span, jthread, consteval and calendar types.

C++20 is the biggest update since C++11. It adds four large language features (concepts, ranges, coroutines and modules), a shorter way to write comparisons, a type-safe formatting library, and many smaller improvements that remove boilerplate. Compilers have supported most of it for years, which makes C++20 a sensible baseline for new projects.

This page is a map: each feature gets a short explanation and a link to the page with the details. Live examples run on GCC 15.2 in -std=c++23 mode, which includes everything in C++20 except modules (see below).

Terminal
g++ -std=c++20 main.cpp

Try it

C++ Concepts, ranges, spaceship and designated initializers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ format, span, jthread, consteval and more

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Language features

Feature What it gives you Page
Concepts Named, checked requirements on template arguments (concept, requires), with readable errors. Concepts
Ranges Composable views and range-based algorithms (std::ranges::sort, views::filter). Ranges
Coroutines co_await, co_yield, co_return for lazy generators and asynchronous tasks. The standard supplies the machinery; std::generator followed in C++23. Coroutines
Modules import replaces #include to give faster builds and no macro leakage. Not available on our runner: read-only. Modules
Three-way comparison operator<=> with = default generates all comparison operators. Spaceship
Designated initializers Point{.x = 1, .y = 2} for aggregates, in declaration order. Designated initializers
consteval, constinit Functions that must run at compile time; variables initialized at compile time. consteval and constinit
Lambda improvements Template lambdas ([]<typename T>(T x)), capture of parameter packs, lambdas in unevaluated contexts, default-constructible stateless lambdas. Lambdas
using enum Brings enumerators into scope: using enum Color;. enum
[[likely]], [[unlikely]], [[no_unique_address]] Hints for branches and for empty members. Attributes
Range-for with initializer for (auto v = make(); auto& x : v). Range-based for
char8_t, std::u8string Distinct type for UTF-8 text. Unicode

Library features

  • <format>: type-safe formatting with Python-like replacement fields (std::format("{:>8}", x)), faster than iostreams and checked at compile time. See std::format.
  • <span>: a non-owning view of a contiguous sequence. See std::span.
  • <ranges>, <concepts>, <bit> (popcount, bit_cast, rotl), <numbers> (std::numbers::pi), <source_location>, <version>.
  • <chrono> gains calendars (year_month_day) and time zones. See chrono calendar.
  • Concurrency: std::jthread and stop_token, std::latch, std::barrier, std::counting_semaphore, atomic_ref, and waiting on atomics.
  • Containers: contains() on associative containers, std::erase/std::erase_if for sequences, starts_with/ends_with on strings, constexpr for std::vector and std::string, std::make_shared<T[]>, std::ssize.
  • Algorithms: std::ranges:: versions of nearly all algorithms, std::midpoint, std::lerp, std::shift_left.

Usage notes

  • Support status. GCC, Clang and MSVC all implement concepts, ranges, <=>, coroutines and <format> in their current releases. Modules work in GCC, Clang and MSVC but need build-system support (CMake 3.28+ with Ninja) and are still the least mature feature.
  • Ranges are views, not copies. A view that refers to a temporary container dangles. Keep the container alive for as long as you use the view.
  • <=> and ==. Defaulting <=> also defaults ==. If you write <=> by hand, you must declare == yourself.
  • consteval versus constexpr. A constexpr function may run at runtime; a consteval function may not. Use consteval when a runtime call would be a bug.
  • Deprecations. Implicit capture of this by [=] is deprecated; write [=, this]. Some volatile uses and std::codecvt for UTF-8 conversions are deprecated too.
  • Migration. Moving from C++17 to C++20 is usually painless. The most common breakage is new comparison rewrites (a == b can match a reversed operator==), which can make an ambiguous overload.