Talk review: C++20 Ranges in Practice

C++20 Ranges in Practice – Tristan Brindle – CppCon 2020

C++20 Ranges in Practice – Tristan Brindle – CppCon 2020


In this talk, Tristan is showing us some practical aspects of using the newly introduced ranges in C++20. His presentation is divided into three simple examples, which turned out to be quite surprising in a number of little details that the programmer has to think of during development.

The first one is all about replacing STL algorithms that operate on iterators with their std::ranges counterparts. This is presented on std::min_element example, where we are looking for the smallest value in the array of integers. This task is simple enough, so we can fully focus on new semantics introduces by ranges. In particular, Tristan is pointing out the potential problem of dangling iterators and how can we avoid it. Also, he explains what is a “borrowed range” and how can we enable it for custom types.

The second example is about summing the squares of values from the std::vector. Here we again want to replace the combination of “old” algorithms (std::transform + std::accumulate) with ranges. This turns out to be problematic because there is no range algorithm that behaves like std::accumulate. Tristan Brindle takes this opportunity to show us how can we create such an algorithm ourselves.

The last one is really interesting – trimming the string. Here we are introduced to the concept of range adaptors and how can we combine them to form a constant “transform” utility. This example really shows how ranges can beautifully express the programmer’s intent in a lazy-evaluated context. After watching this part, I really got excited about the possibilities that ranges enable in C++20.

In this talk you will learn

  • How can we replace old STL algorithms with std::ranges counterparts?
  • What is a range view, dangling iterator and a borrowed range?
  • How can we implement missing algorithms that operate on ranges?
  • What are range adaptors and how can we combine them?

Selected keynote slides

Subscribe to get notified about new content

Thanks for taking your time to read it. If you like it or have other opinions, please share it in the comments. You can also subscribe to my newsletter to get latest news about my content. Happy coding and let’s stay connected!