A span<T>
is this:
template <typename T>
struct span
{
T * ptr_to_array; // pointer to a contiguous C-style array of data
// (which memory is NOT allocated nor deallocated
// nor in any way managed by the span)
std::size_t length; // number of elements of type `T` in the array
// Plus a bunch of constructors and convenience accessor methods here
}
It is a light-weight wrapper around a C-style array, preferred by C++ developers whenever they are using C libraries and want to wrap them with a C++-style data container for "type safety" and "C++-ishness" and "feelgoodery". :)
Note: I call the struct container defined above, known as a span, a "light-weight wrapper around a C-style array" because it points to a contiguous piece of memory, such as a C-style array, and wraps it with accessor methods and the array's size. This is what I mean by "light-weight wrapper": it is a wrapper around a pointer and a length variable, plus functions.
Unlike a std::vector<>
and other C++ standard containers, however, which may also just have fixed class sizes and contain pointers which point to their storage memory, a span does not own the memory it points to, and will never delete it nor resize it nor allocate new memory automatically. Again, a container like a vector owns the memory it points to, and will manage (allocate, reallocate, etc.) it, but a span does not own the memory it points to, and therefore will not manage it.
Going further:
@einpoklum does a pretty good job of introducing what a span
is in his answer here. However, even after reading his answer, it is easy for someone new to spans to still have a sequence of stream-of-thought questions which aren't fully answered, such as the following:
- How is a
span
different from a C array? Why not just use one of those? It seems like it's just one of those with the size known as well...
- Wait, that sounds like a
std::array
, how is a span
different from that?
- Oh, that reminds me, isn't a
std::vector
like a std::array
too?
- I'm so confused. :( What's a
span
?
So, here's some additional clarity on that:
DIRECT QUOTE OF HIS ANSWER--WITH MY ADDITIONS and parenthetical comments IN BOLD and my emphasis in italics:
What is it?
A span<T>
is:
- A very lightweight abstraction of a contiguous sequence of values of type
T
somewhere in memory.
- Basically a single struct
{ T * ptr; std::size_t length; }
with a bunch of convenience methods. (Notice this is distinctly different from std::array<>
because a span
enables convenience accessor methods, comparable to std::array
, via a pointer to type T
and length (number of elements) of type T
, whereas std::array
is an actual container which holds one or more values of type T
.)
- A non-owning type (i.e. a "reference-type" rather than a "value type"): It never allocates nor deallocates anything and does not keep smart pointers alive.
It was formerly known as an array_view
and even earlier as array_ref
.
Those bold parts are critical to one's understanding, so don't miss them or misread them! A span
is NOT a C-array of structs, nor is it a struct of a C-array of type T
plus the length of the array (this would be essentially what the std::array
container is), NOR is it a C-array of structs of pointers to type T
plus the length, but rather it is a single struct containing one single pointer to type T
, and the length, which is the number of elements (of type T
) in the contiguous memory block that the pointer to type T
points to! In this way, the only overhead you've added by using a span
are the variables to store the pointer and length, and any convenience accessor functions you use which the span
provides.
This is UNLIKE a std::array<>
because the std::array<>
actually allocates memory for the entire contiguous block, and it is UNLIKE std::vector<>
because a std::vector
is basically just a std::array
that also does dynamic growing (usually doubling in size) each time it fills up and you try to add something else to it. A std::array
is fixed in size, and a span
doesn't even manage the memory of the block it points to, it just points to the block of memory, knows how long the block of memory is, knows what data type is in a C-array in the memory, and provides convenience accessor functions to work with the elements in that contiguous memory.
It is part of the C++ standard:
std::span
is part of the C++ standard as of C++20. You can read its documentation here: https://en.cppreference.com/w/cpp/container/span. To see how to use Google's absl::Span<T>(array, length)
in C++11 or later today, see below.
Summary Descriptions, and Key References:
std::span<T, Extent>
(Extent
= "the number of elements in the sequence, or std::dynamic_extent
if dynamic". A span just points to memory and makes it easy to access, but does NOT manage it!):
- https://en.cppreference.com/w/cpp/container/span
std::array<T, N>
(notice it has a fixed size N
!):
- https://en.cppreference.com/w/cpp/container/array
- http://www.cplusplus.com/reference/array/array/
std::vector<T>
(automatically dynamically grows in size as necessary):
- https://en.cppreference.com/w/cpp/container/vector
- http://www.cplusplus.com/reference/vector/vector/
How Can I Use span
in C++11 or later today?
Google has open-sourced their internal C++11 libraries in the form of their "Abseil" library. This library is intended to provide C++14 to C++20 and beyond features which work in C++11 and later, so that you can use tomorrow's features, today. They say:
Compatibility with the C++ Standard
Google has developed many abstractions that either match or closely match features incorporated into C++14, C++17, and beyond. Using the Abseil versions of these abstractions allows you to access these features now, even if your code is not yet ready for life in a post C++11 world.
Here are some key resources and links:
- Main site: https://abseil.io/
- https://abseil.io/docs/cpp/
- GitHub repository: https://github.com/abseil/abseil-cpp
span.h
header, and absl::Span<T>(array, length)
template class: https://github.com/abseil/abseil-cpp/blob/master/absl/types/span.h#L153
Other references:
- Struct with template variables in C++
- Wikipedia: C++ classes
- default visibility of C++ class/struct members
Related:
- [another one of my answers on templates and spans] How to make span of spans
gsl::span
rather thanstd::span
. See also my answer below. – Cassondracassoulet