A span is a view over a contiguous sequence of objects. It provides fast and bounds-safe access. Unlike vector or array, it doesn't "own" the elements.

See span class for detailed information. Here's an example of how to use a span:

#include <span>
#include <iostream>

void Show(std::span<int> someValues)
    // show values in reverse
    for (auto rIt = someValues.rbegin(); rIt != someValues.rend(); ++rIt)
        std::cout << *rIt;

    // show a subspan
    for (auto& i : someValues.subspan(1, 2))
        std::cout << i;

int main()
    int numbers[]{ 0,1,2,3,4 };
    Show(numbers); // note conversion from array to span


Header: <span>

Namespace: std

Compiler option: /std:c++20 or later is required.



Name Description
span Provides a view over a contiguous sequence of objects.


Name Description
operator= Span assignment
operator[] Element access


Name Description
as_bytes Get the underlying read-only bytes of the span.
as_writable_bytes Get the underlying bytes of the span.


Name Description
dynamic_extent Indicates that the span size is determined at runtime rather than compile time. When the number of elements in the span is known at compile time, it's specified as the Extent template parameter. When the number isn't known until runtime, specify dynamic_extent instead.

See also

Header Files Reference