std::span(3)
πΈ NAME πΈ
std::span β literally std::span π
, from the C++ Standard Libary manual (std::span(3)). This is the main-character page for the span class template, bestie. β¨
β¨ Synopsis β¨
Defined in header <span>, and it's giving template realness:
template<
class T, (since C++20)
std::size_t Extent = std::dynamic_extent
> class span;
Okay so the class template span describes an object that can refer to a contiguous sequence of objects, with the first element of the sequence at position zero. A span can either have a static extent, in which case the number of elements in the sequence is known at compile-time and encoded in the type, or a dynamic extent. No cap. π«Ά
For a span s, pointers, iterators, and references to elements of s are invalidated when an operation invalidates a pointer in the range [s.data(), s.data() + s.size()). That range is literally right there, memorize the vibe. π
Every specialization of std::span is a TriviallyCopyable type. (since C++23) π
A typical implementation holds a pointer to T; if the extent is dynamic, the implementation also holds a size. That's the whole lowkey implementation tea. π
π Template parameters π
Tβ element type; must be a complete object type that is not an abstract class typeExtentβ the number of elements in the sequence, orstd::dynamic_extentif dynamic
π Member types π
element_typeβTvalue_typeβstd::remove_cv_t<T>size_typeβstd::size_tdifference_typeβstd::ptrdiff_tpointerβT*const_pointerβconst T*referenceβT&const_referenceβconst T&iteratorβ implementation-definedLegacyRandomAccessIterator,ConstexprIterator, andcontiguous_iteratorwhosevalue_typeisvalue_typeconst_iterator(since C++23) βstd::const_iterator<iterator>reverse_iteratorβstd::reverse_iterator<iterator>const_reverse_iterator(since C++23) βstd::const_iterator<reverse_iterator>
Note:iteratoris a mutable iterator ifTis not const-qualified. π
All requirements on the iterator types of a Container apply to the iterator type of span as well. Periodt. π
π§ Member constant π§
static constexpr std::size_t extent = Extent;
(since C++20)
π¦ Member functions π¦
constructorβ constructs a span (public member function)operator=β assigns a span (public member function)destructorβ destructs a span (implicitly declared) (public member function)
π₯ Iterators π₯
beginβ returns an iterator to the beginning (public member function)cbegin(C++23) β (public member function)endβ returns an iterator to the end (public member function)cend(C++23) β (public member function)rbeginβ returns a reverse iterator to the beginning (public member function)crbegin(C++23) β (public member function)rendβ returns a reverse iterator to the end (public member function)crend(C++23) β (public member function)
π Element access π
frontβ access the first element (public member function)backβ access the last element (public member function)at(C++26) β access specified element with bounds checking (public member function)operator[]β access specified element (public member function)dataβ direct access to the underlying contiguous storage (public member function)
π Observers π
sizeβ returns the number of elements (public member function)size_bytesβ returns the size of the sequence in bytes (public member function)emptyβ checks if the sequence is empty (public member function)
π§ Subviews π§
firstβ obtains a subspan consisting of the first N elements of the sequence (public member function)lastβ obtains a subspan consisting of the last N elements of the sequence (public member function)subspanβ obtains a subspan (public member function)
π Non-member functions π
as_bytesβ converts a span into a view of its underlying bytes (function template) (C++20)as_writable_bytesβ (function template) (C++20), listed right alongsideas_bytes
β Non-member constant β
dynamic_extent(C++20) β a constant of typestd::size_tsignifying that the span has dynamic extent (constant)
π«Ά Helper templates π«Ά
template< class T, std::size_t Extent >
inline constexpr bool ranges::enable_borrowed_range<std::span<T, Extent>> = true;
(since C++20)
This specialization of ranges::enable_borrowed_range makes span satisfy borrowed_range. Slay. β¨
template< class T, std::size_t Extent >
inline constexpr bool ranges::enable_view<std::span<T, Extent>> = true;
(since C++20)
This specialization of ranges::enable_view makes span satisfy view. Iconic. π
π¦ Deduction guides π¦
The page has this heading, but no deduction guides are listed under it. That's the whole section, bestie. π«
π Notes π
Specializations of std::span are already trivially copyable types in all existing implementations, even before the formal requirement introduced in C++23. Ngl, they were ahead of the game. π
Feature-test macro tea:
__cpp_lib_spanβ202002L(C++20) βstd::span__cpp_lib_spanβ202311L(C++26) βstd::span::at__cpp_lib_span_initializer_listβ202311L(C++26) β Constructingstd::spanfrom astd::initializer_list
π₯ Example π₯
The example uses std::span to implement some algorithms on contiguous ranges. It's giving algorithm slay. β¨
// Run this code
#include <algorithm>
#include <cstddef>
#include <iostream>
#include <span>
template<class T, std::size_t N>
[[nodiscard]]
constexpr auto slide(std::span<T, N> s, std::size_t offset, std::size_t width)
{
return s.subspan(offset, offset + width <= s.size() ? width : 0U);
}
template<class T, std::size_t N, std::size_t M>
constexpr bool starts_with(std::span<T, N> data, std::span<T, M> prefix)
{
return data.size() >= prefix.size()
&& std::equal(prefix.begin(), prefix.end(), data.begin());
}
template<class T, std::size_t N, std::size_t M>
constexpr bool ends_with(std::span<T, N> data, std::span<T, M> suffix)
{
return data.size() >= suffix.size()
&& std::equal(data.end() - suffix.size(), data.end(),
suffix.end() - suffix.size());
}
template<class T, std::size_t N, std::size_t M>
constexpr bool contains(std::span<T, N> span, std::span<T, M> sub)
{
return std::ranges::search(span, sub).begin() != span.end();
}
void println(const auto& seq)
{
for (const auto& elem : seq)
std::cout << elem << ' ';
std::cout << '\n';
}
int main()
{
constexpr int a[]{0, 1, 2, 3, 4, 5, 6, 7, 8};
constexpr int b[]{8, 7, 6};
constexpr static std::size_t width{6};
for (std::size_t offset{}; ; ++offset)
if (auto s = slide(std::span{a}, offset, width); !s.empty())
println(s);
else
break;
static_assert(""
&& starts_with(std::span{a}, std::span{a, 4})
&& starts_with(std::span{a + 1, 4}, std::span{a + 1, 3})
&& !starts_with(std::span{a}, std::span{b})
&& !starts_with(std::span{a, 8}, std::span{a + 1, 3})
&& ends_with(std::span{a}, std::span{a + 6, 3})
&& !ends_with(std::span{a}, std::span{a + 6, 2})
&& contains(std::span{a}, std::span{a + 1, 4})
&& !contains(std::span{a, 8}, std::span{a, 9})
);
}
π Output π
0 1 2 3 4 5
1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. Yes, we're doing lore. π
LWG 3203β applied toC++20β behavior as published: "it was unclear when the pointers, iterators, and references to elements of span are invalidated"; correct behavior: "made clear"LWG 3903β applied toC++20β behavior as published: "the declaration of span's destructor was unnucessary" (typo preserved, bestie π); correct behavior: "the declaration was removed"P2325R3β applied toC++20β behavior as published: "a span of non-zero static extents was not a view"; correct behavior: "any span is a view"
π See also π
mdspan(C++23) β a multi-dimensional non-owning array view (class template)ranges::subrange(C++20) β combines an iterator-sentinel pair into a view (class template)initializer_list(C++11) β creates a temporary array in list-initialization and then references it (class template)basic_string_view(C++17) β read-only string view (class template)
okay bye bestie! this page came from http://cppreference.com, dated 2024.06.10, and the footer labels it std::span(3). mwah π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms