6.3 KiB
[range.common]
25 Ranges library [ranges]
25.7 Range adaptors [range.adaptors]
25.7.20 Common view [range.common]
25.7.20.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 1:
common_view is useful for calling legacy algorithms that expect a range's iterator and sentinel types to be the same.
â end note]
The name views::common denotes a range adaptor object ([range.adaptor.object]).
Given a subexpression E, the expression views::common(E) is expression-equivalent to:
-
views::all(E), if decltype((E)) models common_range and views::all(E) is a well-formed expression.
-
Otherwise, common_view{E}.
[Example 1: // Legacy algorithm:template size_t count(ForwardIterator first, ForwardIterator last);
template<forward_range R>void my_algo(R&& r) {auto&& common = views::common(r); auto cnt = count(common.begin(), common.end()); // ...} â end example]
25.7.20.2 Class template common_view [range.common.view]
namespace std::ranges {template<view V>requires ( && copyable<iterator_t>)class common_view : public view_interface<common_view> {private: V base_ = V(); // exposition onlypublic: common_view() requires default_initializable = default; constexpr explicit common_view(V r); constexpr V base() const & requires copy_constructible { return base_; }constexpr V base() && { return std::move(base_); }constexpr auto begin() requires (
) {if constexpr (random_access_range && sized_range)return ranges::begin(base_); elsereturn common_iterator<iterator_t, sentinel_t>(ranges::begin(base_)); }constexpr auto begin() const requires range {if constexpr (random_access_range && sized_range)return ranges::begin(base_); elsereturn common_iterator<iterator_t, sentinel_t>(ranges::begin(base_)); }constexpr auto end() requires (
) {if constexpr (random_access_range && sized_range)return ranges::begin(base_) + ranges::distance(base_); elsereturn common_iterator<iterator_t, sentinel_t>(ranges::end(base_)); }constexpr auto end() const requires range {if constexpr (random_access_range && sized_range)return ranges::begin(base_) + ranges::distance(base_); elsereturn common_iterator<iterator_t, sentinel_t>(ranges::end(base_)); }constexpr auto size() requires sized_range {return ranges::size(base_); }constexpr auto size() const requires sized_range {return ranges::size(base_); }constexpr auto reserve_hint() requires approximately_sized_range {return ranges::reserve_hint(base_); }constexpr auto reserve_hint() const requires approximately_sized_range {return ranges::reserve_hint(base_); }}; template common_view(R&&) -> common_view<views::all_t>;}
constexpr explicit common_view(V base);
Effects: Initializes base_ with std::move(base).