23 Ranges library [ranges]

23.8 Range adaptors [range.adaptors]

23.8.10 Common view [range.common]

23.8.10.1 Overview [range.common.overview]

common_­view takes a View which has different types for its iterator and sentinel and turns it into a View of the same elements with an iterator and sentinel of the same type.
[Note
:
common_­view is useful for calling legacy algorithms that expect a range's iterator and sentinel types to be the same.
end note
]
[Example
:
// Legacy algorithm:
template<class ForwardIterator>
size_t count(ForwardIterator first, ForwardIterator last);

template<ForwardRange R>
void my_algo(R&& r) {
  auto&& common = common_view{r};
  auto cnt = count(common.begin(), common.end());
  // ...
}
end example
]

23.8.10.2 Class template common_­view [range.common.view]

namespace std::ranges {
  template<View V>
    requires (!CommonRange<V>)
  class common_view : public view_interface<common_view<V>> {
  private:
    V base_ = V();  // exposition only
  public:
    common_view() = default;

    constexpr explicit common_view(V r);

    template<ViewableRange R>
      requires (!CommonRange<R> && Constructible<V, all_view<R>>)
    constexpr explicit common_view(R&& r);

    constexpr V base() const;

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

    constexpr auto begin() {
      if constexpr (RandomAccessRange<V> && SizedRange<V>)
        return ranges::begin(base_);
      else
        return common_iterator<iterator_t<V>, sentinel_t<V>>(ranges::begin(base_));
    }

    constexpr auto begin() const requires Range<const V> {
      if constexpr (RandomAccessRange<const V> && SizedRange<const V>)
        return ranges::begin(base_);
      else
        return common_iterator<iterator_t<const V>, sentinel_t<const V>>(ranges::begin(base_));
    }

    constexpr auto end() {
      if constexpr (RandomAccessRange<V> && SizedRange<V>)
        return ranges::begin(base_) + ranges::size(base_);
      else
        return common_iterator<iterator_t<V>, sentinel_t<V>>(ranges::end(base_));
    }

    constexpr auto end() const requires Range<const V> {
      if constexpr (RandomAccessRange<const V> && SizedRange<const V>)
        return ranges::begin(base_) + ranges::size(base_);
      else
        return common_iterator<iterator_t<const V>, sentinel_t<const V>>(ranges::end(base_));
    }
  };

  template<class R>
    common_view(R&&) -> common_view<all_view<R>>;
}
constexpr explicit common_view(V base);
Effects: Initializes base_­ with std::move(base).
template<ViewableRange R> requires (!CommonRange<R> && Constructible<V, all_view<R>>) constexpr explicit common_view(R&& r);
Effects: Initializes base_­ with view::all(std::forward<R>(r)).
constexpr V base() const;
Effects: Equivalent to: return base_­;

23.8.10.3 view​::​common [range.common.adaptor]

The name view::common denotes a range adaptor object.
For some subexpression E, the expression view::common(E) is expression-equivalent to:
  • view::all(E), if decltype((E)) models CommonRange and view::all(E) is a well-formed expression.
  • Otherwise, common_­view{E}.