22 Containers library [containers]

22.7 Views [views]

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 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;        // 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 size_type extent = Extent;

    // [span.cons], constructors, copy, and assignment
    constexpr span() noexcept;
    template<class It>
      constexpr span(It first, size_type count);
    tmeplate<class It, class End>
      constexpr span(It first, End 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 R>
      constexpr span(R&& r);
    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(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;

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

    // [span.elem], element access
    constexpr reference operator[](size_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;

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

  template<class It, class End>
    span(It, End) -> span<remove_reference_t<iter_reference_t<It>>>;
  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>>>;
}
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.
Postconditions: size() == 0 && data() == nullptr.
template<class It> constexpr span(It first, size_type count);
Constraints: Let U be remove_­reference_­t<iter_­reference_­t<It>>.
  • It satisfies contiguous_­iterator.
  • is_­convertible_­v<U(*)[], element_­type(*)[]> is true.
    Note
    :
    The intent is to allow only qualification conversions of the iterator reference type to element_­type.
    — end note
     ]
Preconditions:
  • [first, first + count) is a valid range.
  • It models contiguous_­iterator.
  • If extent is not equal to dynamic_­extent, then count is equal to extent.
Effects: Initializes data_­ with to_­address(first) and size_­ with count.
Throws: Nothing.
template<class It, class End> constexpr span(It first, End last);
Constraints: Let U be remove_­reference_­t<iter_­reference_­t<It>>.
  • is_­convertible_­v<U(*)[], element_­type(*)[]> is true.
    Note
    :
    The intent is to allow only qualification conversions of the iterator reference type to element_­type.
    — end note
     ]
  • It satisfies contiguous_­iterator.
  • End satisfies sized_­sentinel_­for<It>.
  • is_­convertible_­v<End, size_­t> is false.
Preconditions:
  • If extent is not equal to dynamic_­extent, then last - first is equal to extent.
  • [first, last) is a valid range.
  • It models contiguous_­iterator.
  • End models sized_­sentinel_­for<It>.
Effects: Initializes data_­ with to_­address(first) and size_­ with last - 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.
Postconditions: size() == N && data() == data(arr).
template<class R> constexpr span(R&& r);
Constraints: Let U be remove_­reference_­t<ranges​::​range_­reference_­t<R>>.
  • extent == dynamic_­extent is true.
  • R satisfies ranges​::​contiguous_­range and ranges​::​sized_­range.
  • Either R satisfies safe_­range or is_­const_­v<element_­type> is true.
  • remove_­cvref_­t<R> is not a specialization of span.
  • remove_­cvref_­t<R> is not a specialization of array.
  • is_­array_­v<remove_­cvref_­t<R>> is false.
  • is_­convertible_­v<U(*)[], element_­type(*)[]> is true.
    Note
    :
    The intent is to allow only qualification conversions of the iterator reference type to element_­type.
    — end note
     ]
Preconditions:
  • R models ranges​::​contiguous_­range and ranges​::​sized_­range.
  • If is_­const_­v<element_­type> is false, R models safe_­range.
Effects: Initializes data_­ with ranges​::​data(r) and size_­ with ranges​::​size(r).
Throws: What and when ranges​::​data(r) and ranges​::​size(r) throw.
constexpr span(const span& other) noexcept = default;
Postconditions: 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()).
Postconditions: size() == s.size() && data() == s.data().
constexpr span& operator=(const span& other) noexcept = default;
Postconditions: size() == other.size() && data() == other.data().

22.7.3.3 Deduction guides [span.deduct]

template<class It, class End> span(It, End) -> span<remove_reference_t<iter_reference_t<It>>>;
Constraints: It satisfies contiguous_­iterator.
template<class R> span(R&&) -> span<remove_reference_t<ranges::range_reference_t<R>>>;
Constraints: R satisfies ranges​::​contiguous_­range.

22.7.3.4 Subviews [span.sub]

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

22.7.3.5 Observers [span.obs]

constexpr size_type size() const noexcept;
Effects: Equivalent to: return size_­;
constexpr size_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.6 Element access [span.elem]

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

22.7.3.7 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;
Effects: 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.8 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.9 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: s[I].