C++ Reference ยท Containers
C++ std::span class
Reference for std::span: a non-owning view of contiguous elements, static and dynamic extent, subspan, first, last, as_bytes and dangling-pointer pitfalls.
std::span<T> (C++20, header <span>) is a lightweight, non-owning view of a contiguous sequence of objects: a pointer and a length bundled together. It can refer to a C array, a std::array, a std::vector or any other contiguous storage, and it never copies or owns the elements. Passing a span to a function replaces the old (T* data, size_t n) pair with one safe-looking parameter that accepts any contiguous container.
A span<T> can modify the elements it views; a span<const T> is read-only. Copying a span is cheap (two words), so pass it by value. The span does not extend the lifetime of the data: if the underlying storage goes away or reallocates, the span dangles.
#include <span> int sum(std::span<const int> xs); // works with vector, array, C array
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
template <class T, std::size_t Extent = std::dynamic_extent> class span; constexpr span(It first, size_type count); constexpr span(T (&arr)[N]); // also std::array<T, N>&, and contiguous ranges constexpr span<T, dynamic_extent> first(size_type n) const; constexpr span<T, dynamic_extent> last(size_type n) const; constexpr span<T, dynamic_extent> subspan(size_type offset, size_type count = dynamic_extent) const; constexpr reference operator[](size_type i) const; // also front(), back(), data() constexpr size_type size() const noexcept; constexpr bool empty() const noexcept; constexpr size_type size_bytes() const noexcept; auto as_bytes(span<T, E> s); auto as_writable_bytes(span<T, E> s);
| Name | Description |
|---|---|
T |
Element type. Use const T for a read-only view. |
Extent |
Number of elements known at compile time, or std::dynamic_extent (the default) for a runtime size. |
n, offset, count |
Number of elements to take, where to start, and how many from there. |
Returns: the slicing functions return new spans over part of the same memory; as_bytes returns a span<const std::byte> over the object representation.
Errors / undefined behavior: an out-of-range index in operator[], front() or back() on an empty span, first(n) with n > size(), or subspan past the end is undefined (checked in hardened builds). A span over destroyed or reallocated storage dangles.
Usage notes
- Pass by value, like
std::string_view. Preferspan<const T>for read-only parameters. - No ownership, no copy of elements. Assigning a span rebinds the view; it does not copy the contents.
- Dangling:
std::span<int> s(v); v.push_back(1);may leavespointing at freed memory. Do not return a span of a local container. - Implicit conversions: a
vector<int>&,array, or C array converts tospan<int>, andspan<int>converts tospan<const int>. A temporary vector converts only tospan<const T>(the range is borrowed), so keep it alive for the call. - Not for non-contiguous containers such as
list,dequeormap. Use iterators or ranges there. - Cheap: a dynamic-extent span is two words; a static-extent span is one pointer.
- Byte views:
std::as_bytes(span)andstd::as_writable_bytes(span)give astd::byteview for serialization. - C++26 adds
at()andstd::spanconstruction from an initializer list; do not rely on them in C++23 code.
Related
- std::string_view, std::array, std::vector, std::mdspan
- Lessons: std::span, Pointers