Standard library header <span>

From cppreference.com
< cpplrm; | header

This header is part of the container library.

Classes

(C++20)
a non-owning view over a contiguous sequence of objects
(class template)

Functions

compares the elements of two spans
(function template)
converts a span into a view of its underlying bytes
(function template)

Constants

a constant of type ptrdiff_t
(constant)

Synopis

namespace std {
  // constants
  inline constexpr ptrdiff_t dynamic_extent = -1;

  // class template span
  template<class T, ptrdiff_t Extent = dynamic_extent>
    class span;

  // comparison operators
  template<class T, ptrdiff_t X, class U, ptrdiff_t Y>
    constexpr bool operator==(span<T, X> l, span<U, Y> r);
  template<class T, ptrdiff_t X, class U, ptrdiff_t Y>
    constexpr bool operator!=(span<T, X> l, span<U, Y> r);
  template<class T, ptrdiff_t X, class U, ptrdiff_t Y>
    constexpr bool operator<(span<T, X> l, span<U, Y> r);	
  template<class T, ptrdiff_t X, class U, ptrdiff_t Y>
    constexpr bool operator<=(span<T, X> l, span<U, Y> r);	
  template<class T, ptrdiff_t X, class U, ptrdiff_t Y>
    constexpr bool operator>(span<T, X> l, span<U, Y> r);	
  template<class T, ptrdiff_t X, class U, ptrdiff_t Y>
    constexpr bool operator>=(span<T, X> l, span<U, Y> r);

  // views of object representation
  template<class T, ptrdiff_t Extent>
    span<const byte,
         Extent == dynamic_extent ? dynamic_extent
                                  : static_cast<ptrdiff_t>(sizeof(T)) * Extent>
      as_bytes(span<T, Extent> s) noexcept;

  template<class T, ptrdiff_t Extent>
    span<byte,
         Extent == dynamic_extent ? dynamic_extent
                                  : static_cast<ptrdiff_t>(sizeof(T)) * Extent>
      as_writable_bytes(span<T, Extent> s) noexcept;
}

Class std::span

namespace std {
  template<class T, ptrdiff_t Extent = dynamic_extent>
  class span {
  public:
    // constants and types
    using element_type           = T;
    using value_type             = remove_cv_t<T>;
    using pointer                = T*;
    using reference              = T&;
    using index_type             = ptrdiff_t;
    using difference_type        = ptrdiff_t;
    using iterator               = /* implementation-defined */;
    using const_iterator         = /* implementation-defined */;
    using reverse_iterator       = reverse_iterator<iterator>;
    using const_reverse_iterator = reverse_iterator<const_iterator>;
    static constexpr ptrdiff_t extent = Extent;

    // constructors, copy, and assignment
    constexpr span() noexcept;
    constexpr span(pointer ptr, index_type count);
    constexpr span(pointer firstElem, pointer lastElem);
    template<size_t N>
      constexpr span(element_type (&arr)[N]) noexcept;
    template <size_t N>
      constexpr span(array<value_type, N>& arr) noexcept;
    template <size_t N>
      constexpr span(const array<value_type, N>& arr) noexcept;
    template <class Container>
      constexpr span(Container& cont);
    template <class Container>
      constexpr span(const Container& cont);
    constexpr span(const span& other) noexcept = default;
    template <class U, ptrdiff_t N>
      constexpr span(const span<U, N>& s) noexcept;

    constexpr span& operator=(const span& other) noexcept = default;

    ~span() noexcept = default;

    // subviews
    template<ptrdiff_t Count>
      constexpr span<element_type, Count> first() const;
    constexpr span<element_type, dynamic_extent> first(ptrdiff_t count) const;
    template<ptrdiff_t Count>
      constexpr span<element_type, Count> last() const;
    constexpr span<element_type, dynamic_extent> last(ptrdiff_t count) const;
    template<ptrdiff_t Offset,
             ptrdiff_t Count = dynamic_extent>
      constexpr span<element_type, E/* see below */> subspan() const;
    constexpr span<element_type, dynamic_extent> 
      subspan(ptrdiff_t offset, ptrdiff_t count = dynamic_extent) const;

    // observers
    constexpr index_type size() const noexcept;
    constexpr index_type size_bytes() const noexcept;
    constexpr bool empty() const noexcept;

    // element access
    constexpr reference operator[](index_type idx) const;
    constexpr reference operator()(index_type idx) const;
    constexpr pointer data() const noexcept;

    // iterator support
    constexpr iterator begin() const noexcept;
    constexpr iterator end() const noexcept;
    constexpr reverse_iterator rbegin() const noexcept;
    constexpr reverse_iterator rend() const noexcept;

    constexpr const_iterator cbegin() const noexcept;
    constexpr const_iterator cend() const noexcept;
    constexpr const_reverse_iterator crbegin() const noexcept;
    constexpr const_reverse_iterator crend() const noexcept;

  private:
    pointer data_;    // exposition only
    index_type size_; // exposition only
  };

  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 Container>
    span(Container&) -> span<typename Container::value_type>;
  template<class Container>
    span(const Container&) -> span<const typename Container::value_type>;
}