std::span
std::span<T> is a non-owning view of a contiguous sequence of T: a pointer and a length. It can refer to a vector, an array, a C array, or a counted pointer, without copying the elements.
Detailed Explanation
You write a function that sums integers. If it takes const std::vector<int>&, callers with a std::array or a C array have to copy into a vector. If it takes const int* and a length, every caller splits one argument into two. std::span<const int> (C++20) is the single parameter that accepts all of those.
A span is a pointer plus a size. span<T> allows mutation of the elements. span<const T> does not. The span itself is a cheap value; copying it copies the two words. It does not own, allocate, or extend lifetime. The same dangling rules as string_view apply.
The extent can be static or dynamic. span<int> has a runtime size(). span<int, 4> has size() == 4 baked into the type, and constructing it from a shorter range is undefined or a contract violation (libraries may throw or abort in debug). Static extent is how you pass a 4x4 matrix without a length check on every call.
subspan, first, and last return spans into the same memory. begin/end give iterators. data() is T*. There is no capacity and no push_back. A span cannot grow the underlying store.
string_view is conceptually span<const char> plus string-ish operations (find, starts_with) and implicit construction from literals. Prefer string_view for text. Prefer span for everything else, including mutable char buffers you are about to fill.
Construction from a vector<bool> does not work: vector<bool> is not contiguous bools. Construction from a range requires contiguous iterators (vector, array, C array, string). A std::list is not a span source.
Use span at API boundaries: "give me some Ts in a row". Do not use it as a stored member unless the owner is stored too. Do not return a span into a local container.
std::as_bytes and std::as_writable_bytes recast a span to span<const std::byte> for serialisation. That is defined for types that are eligible; it is not a licence to type-pun every object.
Code Examples
#include <array>
#include <iostream>
#include <span>
#include <vector>
int sum(std::span<const int> xs) {
int total = 0;
for (int n : xs) total += n;
return total;
}
int main() {
std::vector<int> v{1, 2, 3};
std::array<int, 2> a{4, 5};
int raw[] = {6};
std::cout << sum(v) << '\n'; // prints: 6
std::cout << sum(a) << '\n'; // prints: 9
std::cout << sum(raw) << '\n'; // prints: 6
}#include <span>
#include <vector>
void scale(std::span<int> xs, int factor) {
for (int& n : xs) n *= factor;
}
int main() {
std::vector<int> v{1, 2, 3};
scale(v, 10);
// v is {10, 20, 30}
}Common Gotchas
Returning a span into a local
the pointer dangles when the function returns. Return a `vector`, or have the caller pass the buffer in.
`span<T>` from a temporary `vector`
`sum(std::vector<int>{1, 2})` is valid for the duration of the full expression (the parameter lives until `sum` returns). `std::span<int> s = std::vector<int>{1};` dangles.
Static extent is part of the type
`span<int, 4>` does not bind to a `vector` of other sizes without an explicit `span<int, 4>(first, 4)` that you must prove is safe.
Not every container is contiguous
`list`, `map`, and `deque` (except in special cases) are not span sources.