This is the first article in a series discussing some of the underlying properties of C++ ranges and in particular range adaptors. At the same time, I introduce the design of an experimental library which aims to solve some of the problems discussed here.
Owning and non-owning C++ Ranges
by Hannes Hauswedell
From the article:
We will begin by having a look at ranges from the standard library prior to C++20, since this is what people are most used to. Note that although the ranges themselves are from C++17, I will use some terminology/concepts/algorithms introduced later to explain how they relate to each other. Remember that to count as a range in C++, a type needs to have just
begin()
andend()
. Everything else is bonus.[…]
Containers are the ranges everybody already used before Ranges were a thing. They own their elements, i.e. the storage of the elements is managed by the container and the elements disappear when the container does. Containers are multi-pass ranges, i.e. you can iterate over them multiple times and will always observe the same elements.
[…]
If containers are owning ranges, what are non-owning ranges? C++17 introduced a first example:
std::string_view
, a range that consists just of abegin
andend
pointer into another range’s elements.[…]
However, the most important (and controversial) change came by way of P2415, which allowed views to become owning ranges. It was also applied to C++20 as a defect report, although it was quite a significant design change. This is a useful feature, however, it resulted in the
std::ranges::view
concept being changed to where it no longer means “non-owning range”.[…]
Add a Comment