Standard library header <span>

From cppreference.com
< cpp‎ | 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

converts a span into a view of its underlying bytes
(function template)

Constants

a constant of type size_t signifying that the span has dynamic extent
(constant)

Synopsis

namespace std {
  // constants
  inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max();
 
  // class template span
  template<class ElementType, size_t Extent = dynamic_extent>
    class span;
 
  // views of object representation
  template<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;
 
  // tuple interface
  template<class T> class tuple_size;
  template<size_t I, class T> class tuple_element;
  template<class ElementType, size_t Extent>
    struct tuple_size<span<ElementType, Extent>>;
  template<class ElementType>
    struct tuple_size<span<ElementType, dynamic_extent>>;       // not defined
  template<size_t I, class ElementType, size_t Extent>
    struct tuple_element<I, span<ElementType, Extent>>;
  template<size_t I, class ElementType, size_t Extent>
    constexpr ElementType& get(span<ElementType, Extent>) noexcept;
}

Class std::span

namespace std {
  template<class ElementType, size_t Extent = dynamic_extent>
  class span {
  public:
    // constants and types
    using element_type = ElementType;
    using value_type = remove_cv_t<ElementType>;
    using index_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 = /* implementation-defined */;
    using reverse_iterator = std::reverse_iterator<iterator>;
    using const_reverse_iterator = std::reverse_iterator<const_iterator>;
    static constexpr index_type extent = Extent;
 
    // constructors, copy, and assignment
    constexpr span() noexcept;
    constexpr span(pointer ptr, index_type count);
    constexpr span(pointer first, pointer last);
    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 OtherElementType, size_t OtherExtent>
      constexpr span(const span<OtherElementType, OtherExtent>& s) noexcept;
 
    ~span() noexcept = default;
 
    constexpr span& operator=(const span& other) noexcept = default;
 
    // subviews
    template<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 below> subspan() const;
 
    constexpr span<element_type, dynamic_extent> first(index_type count) const;
    constexpr span<element_type, dynamic_extent> last(index_type count) const;
    constexpr span<element_type, dynamic_extent> subspan(
      index_type offset, index_type count = dynamic_extent) const;
 
    // observers
    constexpr index_type size() const noexcept;
    constexpr index_type size_bytes() const noexcept;
    [[nodiscard]] constexpr bool empty() const noexcept;
 
    // element access
    constexpr reference operator[](index_type idx) const;
    constexpr reference front() const;
    constexpr reference back() const;
    constexpr pointer data() const noexcept;
 
    // iterator support
    constexpr iterator begin() const noexcept;
    constexpr iterator end() const noexcept;
    constexpr const_iterator cbegin() const noexcept;
    constexpr const_iterator cend() const noexcept;
    constexpr reverse_iterator rbegin() const noexcept;
    constexpr reverse_iterator rend() const noexcept;
    constexpr const_reverse_iterator crbegin() const noexcept;
    constexpr const_reverse_iterator crend() const noexcept;
 
    friend constexpr iterator begin(span s) noexcept { return s.begin(); }
    friend constexpr iterator end(span s) noexcept { return s.end(); }
 
  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>;
}