22 Containers library [containers]

22.7 Views [views]

22.7.1 General [views.general]

The header <span> defines the view span.

22.7.2 Header <span> synopsis [span.syn]

namespace std {
  // constants
  inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max();

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

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

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

22.7.3 Class template span [views.span]

22.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.
All member functions of span have constant time complexity.
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;        // see [span.iterators]
    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, size_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<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;

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

    // [span.elem], element access
    constexpr reference operator[](index_type idx) const;
    constexpr reference front() const;
    constexpr reference back() 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;

    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>;
}
ElementType is required to be a complete object type that is not an abstract class type.

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

constexpr span() noexcept;
Constraints: Extent == dynamic_­extent || Extent == 0 is true.
Ensures: size() == 0 && data() == nullptr.
constexpr span(pointer ptr, index_type count);
Expects: [ptr, ptr + count) is a valid range.
If extent is not equal to dynamic_­extent, then count is 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);
Expects: [first, last) is a valid range.
If extent is not equal to dynamic_­extent, then last - first is 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;
Constraints:
  • extent == dynamic_­extent || N == extent is true, and
  • remove_­pointer_­t<decltype(data(arr))>(*)[] is convertible to ElementType(*)[].
Effects: Constructs a span that is a view over the supplied array.
Ensures: size() == N && data() == data(arr).
template<class Container> constexpr span(Container& cont); template<class Container> constexpr span(const Container& cont);
Constraints:
  • extent == dynamic_­extent is true,
  • 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(*)[].
Expects: [data(cont), data(cont) + size(cont)) is a valid range.
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.
constexpr span(const span& other) noexcept = default;
Ensures: other.size() == size() && other.data() == data().
template<class OtherElementType, size_t OtherExtent> constexpr span(const span<OtherElementType, OtherExtent>& s) noexcept;
Constraints:
  • Extent == dynamic_­extent || Extent == OtherExtent is true, and
  • OtherElementType(*)[] is convertible to ElementType(*)[].
Effects: Constructs a span that is a view over the range [s.data(), s.data() + s.size()).
Ensures: size() == s.size() && data() == s.data().
constexpr span& operator=(const span& other) noexcept = default;
Ensures: size() == other.size() && data() == other.data().

22.7.3.3 Subviews [span.sub]

template<size_t Count> constexpr span<element_type, Count> first() const;
Expects: Count <= size() is true.
Effects: Equivalent to: return {data(), Count};
template<size_t Count> constexpr span<element_type, Count> last() const;
Expects: Count <= size() is true.
Effects: Equivalent to: return {data() + (size() - Count), Count};
template<size_t Offset, size_t Count = dynamic_extent> constexpr span<element_type, see below> subspan() const;
Expects:
Offset <= size() && (Count == dynamic_extent || Offset + Count <= size())
is true.
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;
Expects: count <= size() is true.
Effects: Equivalent to: return {data(), count};
constexpr span<element_type, dynamic_extent> last(index_type count) const;
Expects: count <= size() is true.
Effects: Equivalent to: return {data() + (size() - count), count};
constexpr span<element_type, dynamic_extent> subspan( index_type offset, index_type count = dynamic_extent) const;
Expects:
offset <= size() && (count == dynamic_extent || offset + count <= size())
is true.
Effects: Equivalent to:
return {data() + offset, count == dynamic_extent ? size() - offset : count};

22.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);
[[nodiscard]] constexpr bool empty() const noexcept;
Effects: Equivalent to: return size() == 0;

22.7.3.5 Element access [span.elem]

constexpr reference operator[](index_type idx) const;
Expects: idx < size() is true.
Effects: Equivalent to: return *(data() + idx);
constexpr reference front() const;
Expects: empty() is false.
Effects: Equivalent to: return *data();
constexpr reference back() const;
Expects: empty() is false.
Effects: Equivalent to: return *(data() + (size() - 1));
constexpr pointer data() const noexcept;
Effects: Equivalent to: return data_­;

22.7.3.6 Iterator support [span.iterators]

using iterator = implementation-defined; using const_iterator = implementation-defined;
The types model contiguous_­iterator ([iterator.concept.contiguous]), meet the Cpp17RandomAccessIterator requirements ([random.access.iterators]), and meet the requirements for constexpr iterators ([iterator.requirements.general]).
All requirements on container iterators ([container.requirements]) apply to span​::​iterator and span​::​const_­iterator as well.
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());

22.7.3.7 Views of object representation [span.objectrep]

template<class ElementType, size_t Extent> span<const byte, Extent == dynamic_extent ? dynamic_extent : 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, size_t Extent> span<byte, Extent == dynamic_extent ? dynamic_extent : sizeof(ElementType) * Extent> as_writable_bytes(span<ElementType, Extent> s) noexcept;
Constraints: is_­const_­v<ElementType> is false.
Effects: Equivalent to: return {reinterpret_­cast<byte*>(s.data()), s.size_­bytes()};

22.7.3.8 Tuple interface [span.tuple]

template<class ElementType, size_t Extent> struct tuple_size<span<ElementType, Extent>> : integral_constant<size_t, Extent> { };
template<size_t I, class ElementType, size_t Extent> struct tuple_element<I, span<ElementType, Extent>> { using type = ElementType; };
Mandates: Extent != dynamic_­extent && I < Extent is true.
template<size_t I, class ElementType, size_t Extent> constexpr ElementType& get(span<ElementType, Extent> s) noexcept;
Mandates: Extent != dynamic_­extent && I < Extent is true.
Returns: A reference to the element of s, where indexing is zero-based.