24 Ranges library [ranges]

24.7 Range adaptors [range.adaptors]

24.7.8 Drop view [range.drop]

24.7.8.1 Overview [range.drop.overview]

drop_­view produces a view excluding the first N elements from another view, or an empty range if the adapted view contains fewer than N elements.
The name views​::​drop denotes a range adaptor object ([range.adaptor.object]).
Let E and F be expressions, let T be remove_­cvref_­t<decltype((E))>, and let D be range_­difference_­t<decltype((E))>.
If decltype((F)) does not model convertible_­to<D>, views​::​drop(E, F) is ill-formed.
Otherwise, the expression views​::​drop(E, F) is expression-equivalent to:
  • If T is a specialization of ranges​::​empty_­view ([range.empty.view]), then ((void) F, decay-copy(E)).
  • Otherwise, if T models random_­access_­range and sized_­range and is then T{ranges​::​begin(E) + min<D>(ranges​::​size(E), F), ranges​::​end(E)}, except that E is evaluated only once.
  • Otherwise, ranges​::​drop_­view{E, F}.
Example
:
auto ints = views::iota(0) | views::take(10);
auto latter_half = drop_view{ints, 5};
for (auto i : latter_half) {
  cout << i << ' ';                             // prints 5 6 7 8 9
}
— end example
 ]

24.7.8.2 Class template drop_­view [range.drop.view]

namespace std::ranges {
  template<view V>
  class drop_view : public view_interface<drop_view<V>> {
  public:
    drop_view() = default;
    constexpr drop_view(V base, range_difference_t<V> count);

    constexpr V base() const& requires copy_constructible<V> { return base_; }
    constexpr V base() && { return std::move(base_); }

    constexpr auto begin()
      requires (!(simple-view<V> && random_access_range<V>));
    constexpr auto begin() const
      requires random_access_range<const V>;

    constexpr auto end()
      requires (!simple-view<V>)
    { return ranges::end(base_); }

    constexpr auto end() const
      requires range<const V>
    { return ranges::end(base_); }

    constexpr auto size()
      requires sized_range<V>
    {
      const auto s = ranges::size(base_);
      const auto c = static_cast<decltype(s)>(count_);
      return s < c ? 0 : s - c;
    }

    constexpr auto size() const
      requires sized_range<const V>
    {
      const auto s = ranges::size(base_);
      const auto c = static_cast<decltype(s)>(count_);
      return s < c ? 0 : s - c;
    }
  private:
    V base_ = V();                              // exposition only
    range_difference_t<V> count_ = 0;           // exposition only
  };

  template<class R>
    drop_view(R&&, range_difference_t<R>) -> drop_view<views::all_t<R>>;
}
constexpr drop_view(V base, range_difference_t<V> count);
Preconditions: count >= 0 is true.
Effects: Initializes base_­ with std​::​move(base) and count_­ with count.
constexpr auto begin() requires (!(simple-view<V> && random_­access_­range<V>)); constexpr auto begin() const requires random_access_range<const V>;
Returns: ranges​::​next(ranges​::​begin(base_­), count_­, ranges​::​end(base_­)).
Remarks: In order to provide the amortized constant-time complexity required by the range concept when drop_­view models forward_­range, the first overload caches the result within the drop_­view for use on subsequent calls.
Note
:
Without this, applying a reverse_­view over a drop_­view would have quadratic iteration complexity.
— end note
 ]