Standard library header <span> (C++20)
From cppreference.com
This header is part of the containers library.
Classes | ||
(C++20) | a non-owning view over a contiguous sequence of objects (class template) | |
Constants | ||
(C++20) | a constant of type std::size_t signifying that the span has dynamic extent (constant) | |
Functions | ||
(C++20) | converts a span into a view of its underlying bytes (function template) | |
Range access | ||
(C++11)(C++14) | returns an iterator to the beginning of a container or array (function template) | |
(C++11)(C++14) | returns an iterator to the end of a container or array (function template) | |
(C++14) | returns a reverse iterator to the beginning of a container or array (function template) | |
(C++14) | returns a reverse end iterator for a container or array (function template) | |
(C++17)(C++20) | returns the size of a container or array (function template) | |
(C++17) | checks whether the container is empty (function template) | |
(C++17) | obtains the pointer to the underlying array (function template) |
[edit]Synopsis
#include <initializer_list> // mostly freestandingnamespace std {// constantsinlineconstexpr size_t dynamic_extent = numeric_limits<size_t>::max(); template<class T> concept /*integral-constant-like*/=// exposition only is_integral_v<decltype(T::value)>&&!is_same_v<bool, remove_const_t<decltype(T::value)>>&& convertible_to<T, decltype(T::value)>&& equality_comparable_with<T, decltype(T::value)>&& bool_constant<T()== T::value>::value&& bool_constant<static_cast<decltype(T::value)>(T())== T::value>::value; template<class T>constexpr size_t /*maybe-static-ext*/= dynamic_extent;// exposition onlytemplate</*integral-constant-like*/ T>constexpr size_t /*maybe-static-ext*/<T>={ T::value}; // class template spantemplate<class ElementType, size_t Extent = dynamic_extent>class span;// partially freestanding template<class ElementType, size_t Extent>constexprboolranges::enable_view<span<ElementType, Extent>>=true;template<class ElementType, size_t Extent>constexprboolranges::enable_borrowed_range<span<ElementType, Extent>>=true; // views of object representationtemplate<class ElementType, size_t Extent> span<const byte, Extent == dynamic_extent ? dynamic_extent : sizeof(ElementType)* Extent> as_bytes(span<ElementType, Extent> s)noexcept; template<class ElementType, size_t Extent> span<byte, Extent == dynamic_extent ? dynamic_extent : sizeof(ElementType)* Extent> as_writable_bytes(span<ElementType, Extent> s)noexcept;}
[edit]Class template std::span
namespace std {template<class ElementType, size_t Extent = dynamic_extent>class span {public:// constants and typesusing element_type = ElementType;using value_type = remove_cv_t<ElementType>;using size_type = size_t;using difference_type = ptrdiff_t;using pointer = element_type*;using const_pointer =const element_type*;using reference = element_type&;using const_reference =const element_type&;using iterator =/* implementation-defined */;using const_iterator =std::const_iterator<iterator>;using reverse_iterator =std::reverse_iterator<iterator>;using const_reverse_iterator =std::const_iterator<reverse_iterator>;staticconstexpr size_type extent = Extent; // constructors, copy, and assignmentconstexpr span()noexcept;template<class It>constexprexplicit(extent != dynamic_extent) span(It first, size_type count);template<class It, class End>constexprexplicit(extent != dynamic_extent) span(It first, End last);template<size_t N>constexpr span(type_identity_t<element_type>(&arr)[N])noexcept;template<class T, size_t N>constexpr span(array<T, N>& arr)noexcept;template<class T, size_t N>constexpr span(const array<T, N>& arr)noexcept;template<class R>constexprexplicit(extent != dynamic_extent) span(R&& r);constexprexplicit(extent != dynamic_extent) span(std::initializer_list<value_type> il);constexpr span(const span& other)noexcept=default;template<class OtherElementType, size_t OtherExtent>constexprexplicit(/* see description */) span(const span<OtherElementType, OtherExtent>& s)noexcept; constexpr span& operator=(const span& other)noexcept=default; // subviewstemplate<size_t Count>constexpr span<element_type, Count> first()const;template<size_t Count>constexpr span<element_type, Count> last()const;template<size_t Offset, size_t Count = dynamic_extent>constexpr span<element_type, /* see description */> subspan()const; constexpr span<element_type, dynamic_extent> first(size_type count)const;constexpr span<element_type, dynamic_extent> last(size_type count)const;constexpr span<element_type, dynamic_extent> subspan( size_type offset, size_type count = dynamic_extent)const; // observersconstexpr size_type size()constnoexcept;constexpr size_type size_bytes()constnoexcept;constexprbool empty()constnoexcept; // element accessconstexpr reference operator[](size_type idx)const;constexpr reference at(size_type idx)const;// freestanding-deletedconstexpr reference front()const;constexpr reference back()const;constexpr pointer data()constnoexcept; // iterator supportconstexpr iterator begin()constnoexcept;constexpr iterator end()constnoexcept;constexpr const_iterator cbegin()constnoexcept{return begin();}constexpr const_iterator cend()constnoexcept{return end();}constexpr reverse_iterator rbegin()constnoexcept;constexpr reverse_iterator rend()constnoexcept;constexpr const_reverse_iterator crbegin()constnoexcept{return rbegin();}constexpr const_reverse_iterator crend()constnoexcept{return rend();} private: pointer /*data_*/;// exposition only size_type /*size_*/;// exposition only}; template<class It, class EndOrSize> span(It, EndOrSize)-> span<remove_reference_t<iter_reference_t<It>>, /*maybe-static-ext*/<EndOrSize>>;template<class T, size_t N> span(T (&)[N])-> span<T, N>;template<class T, size_t N> span(array<T, N>&)-> span<T, N>;template<class T, size_t N> span(const array<T, N>&)-> span<const T, N>;template<class R> span(R&&)-> span<remove_reference_t<ranges::range_reference_t<R>>>;}