21 Containers library [containers]

21.7 Views [views]

21.7.1 General [views.general]

The header <span> defines the view span.

21.7.2 Header <span> synopsis [span.syn]

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

  // [views.span], class template span
  template<class ElementType, ptrdiff_t Extent = dynamic_extent>
    class span;

  // [span.comparison], 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);

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

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

21.7.3 Class template span [views.span]

21.7.3.1 Overview [span.overview]

A span is a view over a contiguous sequence of objects, the storage of which is owned by some other object.
The iterator types span::iterator and span::const_­iterator are random access iterators ([random.access.iterators]), contiguous iterators ([iterator.requirements.general]), and constexpr iterators ([iterator.requirements.general]).
All requirements on container iterators ([container.requirements]) apply to span::iterator and span::const_­iterator as well.
All member functions of span have constant time complexity.
namespace std {
  template<class ElementType, ptrdiff_t Extent = dynamic_extent>
  class span {
  public:
    // constants and types
    using element_type = ElementType;
    using value_type = remove_cv_t<ElementType>;
    using index_type = ptrdiff_t;
    using difference_type = ptrdiff_t;
    using pointer = element_type*;
    using reference = 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;

    // [span.cons], 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, ptrdiff_t OtherExtent>
      constexpr span(const span<OtherElementType, OtherExtent>& s) noexcept;

    ~span() noexcept = default;

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

    // [span.sub], subviews
    template<ptrdiff_t Count>
      constexpr span<element_type, Count> first() const;
    template<ptrdiff_t Count>
      constexpr span<element_type, Count> last() const;
    template<ptrdiff_t Offset, ptrdiff_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;

    // [span.obs], observers
    constexpr index_type size() const noexcept;
    constexpr index_type size_bytes() const noexcept;
    constexpr bool empty() const noexcept;

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

    // [span.iterators], 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;

  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>;
}
ElementType is required to be a complete object type that is not an abstract class type.
If Extent is negative and not equal to dynamic_­extent, the program is ill-formed.

21.7.3.2 Constructors, copy, and assignment [span.cons]

constexpr span() noexcept;
Ensures: size() == 0 && data() == nullptr.
Remarks: This constructor shall not participate in overload resolution unless Extent <= 0 is true.
constexpr span(pointer ptr, index_type count);
Requires: [ptr, ptr + count) shall be a valid range.
If extent is not equal to dynamic_­extent, then count shall be equal to extent.
Effects: Constructs a span that is a view over the range [ptr, ptr + count).
Ensures: size() == count && data() == ptr.
Throws: Nothing.
constexpr span(pointer first, pointer last);
Requires: [first, last) shall be a valid range.
If extent is not equal to dynamic_­extent, then last - first shall be equal to extent.
Effects: Constructs a span that is a view over the range [first, last).
Ensures: size() == last - first && data() == first.
Throws: Nothing.
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;
Effects: Constructs a span that is a view over the supplied array.
Ensures: size() == N && data() == data(arr).
Remarks: These constructors shall not participate in overload resolution unless:
  • extent == dynamic_­extent || N == extent is true, and
  • remove_­pointer_­t<decltype(data(arr))>(*)[] is convertible to ElementType(*)[].
template<class Container> constexpr span(Container& cont); template<class Container> constexpr span(const Container& cont);
Requires: [data(cont), data(cont) + size(cont)) shall be a valid range.
If extent is not equal to dynamic_­extent, then size(cont) shall be equal to extent.
Effects: Constructs a span that is a view over the range [data(cont), data(cont) + size(cont)).
Ensures: size() == size(cont) && data() == data(cont).
Throws: What and when data(cont) and size(cont) throw.
Remarks: These constructors shall not participate in overload resolution unless:
  • Container is not a specialization of span,
  • Container is not a specialization of array,
  • is_­array_­v<Container> is false,
  • data(cont) and size(cont) are both well-formed, and
  • remove_­pointer_­t<decltype(data(cont))>(*)[] is convertible to ElementType(*)[].
constexpr span(const span& other) noexcept = default;
Ensures: other.size() == size() && other.data() == data().
template<class OtherElementType, ptrdiff_t OtherExtent> constexpr span(const span<OtherElementType, OtherExtent>& s) noexcept;
Effects: Constructs a span that is a view over the range [s.data(), s.data() + s.size()).
Ensures: size() == s.size() && data() == s.data().
Remarks: This constructor shall not participate in overload resolution unless:
  • Extent == dynamic_­extent || Extent == OtherExtent is true, and
  • OtherElementType(*)[] is convertible to ElementType(*)[].
constexpr span& operator=(const span& other) noexcept = default;
Ensures: size() == other.size() && data() == other.data().

21.7.3.3 Subviews [span.sub]

template<ptrdiff_t Count> constexpr span<element_type, Count> first() const;
Requires: 0 <= Count && Count <= size().
Effects: Equivalent to: return {data(), Count};
template<ptrdiff_t Count> constexpr span<element_type, Count> last() const;
Requires: 0 <= Count && Count <= size().
Effects: Equivalent to: return {data() + (size() - Count), Count};
template<ptrdiff_t Offset, ptrdiff_t Count = dynamic_extent> constexpr span<element_type, see below> subspan() const;
Requires:
(0 <= Offset && Offset <= size())
&& (Count == dynamic_extent || Count >= 0 && Offset + Count <= size())
Effects: Equivalent to:
return span<ElementType, see below>(
  data() + Offset, Count != dynamic_extent ? Count : size() - Offset);
Remarks: The second template argument of the returned span type is:
Count != dynamic_extent ? Count
                        : (Extent != dynamic_extent ? Extent - Offset
                                                    : dynamic_extent)
constexpr span<element_type, dynamic_extent> first(index_type count) const;
Requires: 0 <= count && count <= size().
Effects: Equivalent to: return {data(), count};
constexpr span<element_type, dynamic_extent> last(index_type count) const;
Requires: 0 <= count 0 && count <= size().
Effects: Equivalent to: return {data() + (size() - count), count};
constexpr span<element_type, dynamic_extent> subspan( index_type offset, index_type count = dynamic_extent) const;
Requires:
(0 <= offset && offset <= size())
&& (count == dynamic_extent || count >= 0 && offset + count <= size())
Effects: Equivalent to:
return {data() + offset, count == dynamic_extent ? size() - offset : count};

21.7.3.4 Observers [span.obs]

constexpr index_type size() const noexcept;
Effects: Equivalent to: return size_­;
constexpr index_type size_bytes() const noexcept;
Effects: Equivalent to: return size() * sizeof(element_­type);
constexpr bool empty() const noexcept;
Effects: Equivalent to: return size() == 0;

21.7.3.5 Element access [span.elem]

constexpr reference operator[](index_type idx) const; constexpr reference operator()(index_type idx) const;
Requires: 0 <= idx && idx < size().
Effects: Equivalent to: return *(data() + idx);
constexpr pointer data() const noexcept;
Effects: Equivalent to: return data_­;

21.7.3.6 Iterator support [span.iterators]

constexpr iterator begin() const noexcept;
Returns: An iterator referring to the first element in the span.
If empty() is true, then it returns the same value as end().
constexpr iterator end() const noexcept;
Returns: An iterator which is the past-the-end value.
constexpr reverse_iterator rbegin() const noexcept;
Effects: Equivalent to: return reverse_­iterator(end());
constexpr reverse_iterator rend() const noexcept;
Returns: Equivalent to: return reverse_­iterator(begin());
constexpr const_iterator cbegin() const noexcept;
Returns: A constant iterator referring to the first element in the span.
If empty() is true, then it returns the same value as cend().
constexpr const_iterator cend() const noexcept;
Returns: A constant iterator which is the past-the-end value.
constexpr const_reverse_iterator crbegin() const noexcept;
Effects: Equivalent to: return const_­reverse_­iterator(cend());
constexpr const_reverse_iterator crend() const noexcept;
Effects: Equivalent to: return const_­reverse_­iterator(cbegin());

21.7.3.7 Comparison operators [span.comparison]

template<class T, ptrdiff_t X, class U, ptrdiff_t Y> constexpr bool operator==(span<T, X> l, span<U, Y> r);
Effects: Equivalent to: return equal(l.begin(), l.end(), r.begin(), r.end());
template<class T, ptrdiff_t X, class U, ptrdiff_t Y> constexpr bool operator!=(span<T, X> l, span<U, Y> r);
Effects: Equivalent to: return !(l == r);
template<class T, ptrdiff_t X, class U, ptrdiff_t Y> constexpr bool operator<(span<T, X> l, span<U, Y> r);
Effects: Equivalent to:
return lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
template<class T, ptrdiff_t X, class U, ptrdiff_t Y> constexpr bool operator>(span<T, X> l, span<U, Y> r);
Effects: Equivalent to: return (r < l);
template<class T, ptrdiff_t X, class U, ptrdiff_t Y> constexpr bool operator<=(span<T, X> l, span<U, Y> r);
Effects: Equivalent to: return !(r < l);
template<class T, ptrdiff_t X, class U, ptrdiff_t Y> constexpr bool operator>=(span<T, X> l, span<U, Y> r);
Effects: Equivalent to: return !(l < r);

21.7.3.8 Views of object representation [span.objectrep]

template<class ElementType, ptrdiff_t Extent> span<const byte, Extent == dynamic_extent ? dynamic_extent : static_cast<ptrdiff_t>(sizeof(ElementType)) * Extent> as_bytes(span<ElementType, Extent> s) noexcept;
Effects: Equivalent to: return {reinterpret_­cast<const byte*>(s.data()), s.size_­bytes()};
template<class ElementType, ptrdiff_t Extent> span<byte, Extent == dynamic_extent ? dynamic_extent : static_cast<ptrdiff_t>(sizeof(ElementType)) * Extent> as_writable_bytes(span<ElementType, Extent> s) noexcept;
Effects: Equivalent to: return {reinterpret_­cast<byte*>(s.data()), s.size_­bytes()};
Remarks: This function shall not participate in overload resolution unless is_­const_­v<ElementType> is false.