skip to content

C++20 and C++23 Features

C++20 brought concepts, ranges, coroutines and modules, and C++23 added expected, print, deducing this and flat_map. Interviewers ask which of them a candidate has used.

on this pageshow

explore

questions

4

What are C++20's four headline features — concepts, ranges, coroutines and modules — and what problem does each solve?

level: middleimportance: must knowfreq 65%

answer

  1. requirements, pipelines, suspension, compiled interfaces
  2. errors at the call site
  3. whole-range algorithms and lazy views
  4. language mechanism without library types
  5. build systems lag behind modules

basics

~10 s

Concepts state template requirements so misuse fails at the call; ranges let algorithms take whole ranges and compose lazy views; coroutines let a function suspend and resume; modules replace textual #include with compiled interfaces.

solid answer

~40 s

**Concepts** (`template <std::integral T>`, `requires` clauses) put a template's requirements in its declaration: a bad argument fails at the call with "does not satisfy", not deep inside the template, and they replace most `std::enable_if` tricks. **Ranges** give algorithms that take a whole container (`std::ranges::sort(v)`) and lazy, composable views (`v | std::views::filter(f)`). **Coroutines** (`co_await`, `co_yield`, `co_return`) let a function suspend and resume, the basis for generators and async code — but C++20 ships only the low-level machinery, not ready-made task or generator types. **Modules** (`export module`, `import`) replace textual inclusion with compiled interfaces that macros do not leak through. Concepts and ranges are broadly usable today; coroutines need a library, and modules wait on build-system support.

code

cpp · 22 lines
cpp
#include <concepts>
#include <iostream>
#include <ranges>
#include <vector>

// C++20 concept: the requirement is part of the declaration
template <std::integral T>
T sum_even(const std::vector<T>& values) {
    T total{};
    // C++20 ranges: a lazy pipeline over the whole container, no iterator pairs
    for (T v : values | std::views::filter([](T x) { return x % 2 == 0; })) {
        total += v;
    }
    return total;
}

int main() {
    std::vector<int> ids{1, 2, 3, 4, 10};
    std::cout << sum_even(ids) << '\n';        // prints 16
    // std::vector<double> prices{1.5, 2.0};
    // sum_even(prices);  // error: no matching function; note: 'double' does not satisfy 'integral'
}

go deeper

for a junior

Name the four features and one sentence each: concepts constrain templates, ranges simplify algorithms, coroutines suspend and resume, modules replace includes.

for a middle

Explain what each replaced in C++17 code and show one concept and one range pipeline you could write on a whiteboard.

for a senior

Discuss adoption: which features you shipped in a real codebase, which you deferred, and why coroutines and modules need more than a compiler flag.

for a principal

Weigh where each feature pays off across a large codebase — error clarity, build times, async design — against toolchain maturity and training cost.

## Why C++20 is the second big release C++20 is often compared to C++11: it changes how templates are written, how algorithms are called, how asynchronous code can be structured and how code is packaged. Interviewers ask about the **big four** — concepts, ranges, coroutines and modules — because a candidate who modernised a C++17 service can say which of them they adopted, what each replaced, and which they deliberately left alone. | Feature | Replaces | Adoption caveat | |---|---|---| | Concepts | `std::enable_if`, SFINAE tricks, comments saying "T must be..." | broadly supported | | Ranges | iterator pairs, hand-written loops, temporary containers | broadly supported; some views arrived in C++23 | | Coroutines | callback chains, hand-written state machines | needs a library for task and generator types | | Modules | textual `#include`, precompiled headers | needs compiler flags and build-system support | ## Concepts A **concept** is a named, compile-time predicate on types, such as `std::integral` or `std::ranges::range`. Writing `template <std::integral T>` or adding a `requires` clause makes the requirement part of the declaration. Two benefits follow: - **Errors move to the call site.** Calling the template with `double` fails with a message saying `double` does not satisfy `integral`, instead of a cascade from inside the body. - **Overloading on requirements** becomes readable: the more constrained overload wins, replacing `std::enable_if` and tag dispatch. The `<concepts>` header supplies the standard vocabulary (`std::same_as`, `std::convertible_to`, `std::invocable`...). ## Ranges The ranges library (`<ranges>`) adds two things. First, **range algorithms** in `std::ranges` that take a whole container and accept projections: `std::ranges::sort(people, {}, &Person::age)`. Second, **views**: lazy adaptors such as `std::views::filter`, `std::views::transform` and `std::views::take` that compose with `|` and compute elements on demand without allocating. C++23 added more views and `std::ranges::to` for turning a view back into a container. ## Coroutines A **coroutine** is a function containing `co_await`, `co_yield` or `co_return`. It can suspend, keep its local state in a coroutine frame, and resume later. That is the building block for **generators** (lazy sequences) and for **asynchronous code** that reads sequentially instead of as nested callbacks. C++20 standardised only the language mechanism and low-level library support in `<coroutine>` — `std::coroutine_handle`, `std::suspend_always`, `std::suspend_never`. The promise types that make a usable `task` or `generator` were left to libraries; `std::generator` arrived in C++23. ## Modules A **module** is a translation unit that declares `export module name;` and marks what it exports. Importers write `import name;` and see only the exported declarations, compiled once rather than re-parsed from text in every file. Modules isolate **macros** (they do not leak into importers) and remove include-order dependencies. The cost is tooling: the build must compile module interfaces before their importers. GCC 16 still documents C++20 modules as experimental behind `-fmodules`, and C++23's `import std` is what finally makes the standard library itself importable. ## Before and after in one function The difference concepts make is easiest to show side by side. The C++17 way hides the requirement in a template argument: ```cpp // C++17: the requirement lives in a defaulted template argument template <class T, class = std::enable_if_t<std::is_integral_v<T>>> T half(T x) { return x / 2; } // C++20: the requirement is part of the declaration template <std::integral T> T half(T x) { return x / 2; } ``` Both reject `half(2.5)`, but the C++20 version says why in one line, can be overloaded against a `std::floating_point` version without tricks, and reads as documentation. Ranges make the same kind of change for loops: a filter-then-transform loop that used a temporary vector becomes one lazy pipeline that allocates nothing until the result is stored. Three misconceptions are worth avoiding when you describe the release: - concepts are a **compile-time** feature; they do not make code run faster or slower; - views are **lazy** and usually non-owning, so a pipeline over a temporary container can dangle; - coroutines are **stackless**: only the coroutine's own frame is suspended, not its callers. ## How to talk about them in an interview The strongest answers are concrete: 1. **Name what you used.** "We replaced our `enable_if` overloads with concepts and moved hot loops to range pipelines." 2. **Say what it replaced and why it was better** — clearer errors, fewer temporaries, less boilerplate. 3. **State the caveat honestly.** "We did not adopt modules: our build system and one compiler were not ready," or "we used coroutines through a library, because C++20 has no task type." Saying that all four are equally production-ready is the weak answer; each has a different maturity.

  • If C++20 has coroutines, why do teams still need a library to use them?
    C++20 standardised the language transformation and `<coroutine>`'s handles and trivial awaitables, but not the promise types a coroutine's return type must provide. A `task<T>` for async code or a generator needs that promise type, so teams used a library or wrote their own until C++23 added `std::generator`. A standard task type for asynchronous code came later still, with C++26's `std::execution`.
  • What does a concept give you that static_assert inside the template does not?
    A `static_assert` fires only after the template is selected and instantiated, so it cannot steer overload resolution and its error appears inside the body. A concept is checked before the template is chosen: a failing constraint removes the candidate, another overload can win, and the diagnostic names the unsatisfied requirement at the call.

  • C++20 coroutines come with ready-made task and generator types.
  • Concepts make templates run faster at run time.
  • Range views copy the elements into a new container at each step.
  • Modules are a drop-in replacement any build system already supports.
  • Concepts are just new syntax for static_assert inside the template body.

Assumes C++20 as the revision that introduced these features, as kept in C++23; modules status per GCC 16 documentation.

open as a page

Besides the big four, which smaller C++20 additions — <=>, std::format, std::span and std::jthread — would you cite, and what did each replace?

level: juniorimportance: should knowfreq 55%

basics

~10 s

A defaulted operator<=> generates all comparisons; std::format replaces printf and stream manipulators with type-safe {} formatting; std::span replaces pointer-and-length parameters; std::jthread replaces std::thread and joins automatically after requesting stop.

open as a page

Which C++23 additions — std::print, std::generator, import std, std::mdspan and std::flat_map — complete what C++20 started, and how?

level: middleimportance: should knowfreq 40%

basics

~20 s

C++23 fills gaps C++20 left: std::print writes std::format output directly, std::generator gives coroutines a standard lazy sequence, import std makes the library importable as a module, std::mdspan adds multidimensional views, and std::flat_map offers a contiguous sorted map.

open as a page

In a C++23 modernization, what do std::expected and deducing this each replace, and when would you reach for them?

level: seniorimportance: should knowfreq 45%

basics

~20 s

std::expected<T, E> returns either a value or a typed error, replacing error codes with out-parameters and ad hoc result structs where failure is normal. Deducing this makes the object an explicit parameter, replacing duplicated const and non-const overloads and much CRTP boilerplate.

open as a page