I came across some discussion of
span, which may find its way into the C++20 standard.
The
span proposal is for a non-owning view into contiguous memory, such as is held by a
std::vector,
std::array, or built in array. It is conceptually similar to, and may be implemented with, a pointer and a length pair. It does not allocate or free the memory it accesses, and so does not risk allocation related exceptions. It does however require the memory it is pointed at to outlive the view's lifetime.
One particular use for a
span is as a function parameter. This would allow a function to accept any contiguous container as an argument. For example, any of
std::vector,
std::array, or built in arrays could be passed as arguments, which would be converted to a
span at the call site.
This is similar to
string_view, which is able to handle both null-terminated C-style string literals, and the C++ style
std::string. It is also a non-owning view of the string, and is also conceptually similar to a pointer and length pair.
You might think of
span as something like a generalized type version of
string_view. Though the exact details may get a little messier than that. In particular,
string_view is meant as a read-only view into the string. As such the
iterator member type is defined to be equal to the
const_iterator member type, and internally it contains a
const CharT*. In contrast, the
span type is meant to be usable in a mutable context. This has implications concerning Regularity, which impacts copy, assignment, and comparison operators, and is related to treating user defined types as value types.
There's a
paper on the design of span, which is interesting, though skip the change log. There's also a discussion
Should Span be Regular? which has some interesting discussion on value type semantics, and how this impacts some of the operators.
Edit: Correct link to
span documentation.