TRT
Sign in

Core · Module 7 · Lesson 8 of 11

Modern Standard-Library Tools: optional, variant, span and ranges

Represent absence and alternatives safely, pass contiguous data without ownership, and compose algorithms with C++20 ranges.

Pro — members onlyAbout 12 minutes

By the end you can

  • Choose between optional, variant and a plain value for an API result
  • Use span to borrow contiguous data without copying or owning it
  • Explain how a range view composes with an algorithm and what lifetime it requires

Builds on Classic Algorithms: Predicates, Iterators and Output Ranges, Iterator Categories, Invalidation and Ranges.

01 · A question first

A function sometimes finds a record, sometimes does not, and a parser can return one of three error kinds. Should it use a magic integer, a nullable pointer, or a type that states the alternatives? And how can a function inspect an array without taking ownership of it?

Make a guess before you read on.

02 · The idea

Members only

Unlock the full lesson

This lesson is part of the membership. Join to read it in full, along with every other premium deep dive.

What's inside

  • Start with the problem
  • How the idea works
  • Why it is correct
  • What it costs
  • Pitfalls and variations
  • Production guidance

Something wrong with this lesson? Report a problem