[range.common]

# 25 Ranges library [[ranges]](./#ranges)

## 25.7 Range adaptors [[range.adaptors]](range.adaptors#range.common)

### 25.7.20 Common view [range.common]

#### [25.7.20.1](#overview) Overview [[range.common.overview]](range.common.overview)

[1](#overview-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L9754)

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[.](#overview-1.sentence-1)

[2](#overview-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L9759)

[*Note [1](#overview-note-1)*:

common_view is useful for calling legacy algorithms that expect
a range's iterator and sentinel types to be the same[.](#overview-2.sentence-1)

â *end note*]

[3](#overview-3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L9765)

The name views​::​common denotes a
range adaptor object ([[range.adaptor.object]](range.adaptor.object "25.7.2 Range adaptor objects"))[.](#overview-3.sentence-1)

Given a subexpression E,
the expression views​::​common(E) is expression-equivalent to:

- [(3.1)](#overview-3.1)

  views​::​all(E),
 if decltype((E)) models [common_range](range.refinements#concept:common_range "25.4.6 Other range refinements [range.refinements]") and views​::​all(E) is a well-formed expression[.](#overview-3.1.sentence-1)

- [(3.2)](#overview-3.2)

  Otherwise, common_view{E}[.](#overview-3.2.sentence-1)

[4](#overview-4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L9779)

[*Example [1](#overview-example-1)*: // Legacy algorithm:template<class ForwardIterator> size_t count(ForwardIterator first, ForwardIterator last);

template<[forward_range](range.refinements#concept:forward_range "25.4.6 Other range refinements [range.refinements]") R>void my_algo(R&& r) {auto&& common = views::common(r); auto cnt = count(common.begin(), common.end()); // ...} â *end example*]

#### [25.7.20.2](#view) Class template common_view [[range.common.view]](range.common.view)

[ð](#lib:common_view)

namespace std::ranges {template<[view](range.view#concept:view "25.4.5 Views [range.view]") V>requires (![common_range](range.refinements#concept:common_range "25.4.6 Other range refinements [range.refinements]")<V> && [copyable](concepts.object#concept:copyable "18.6 Object concepts [concepts.object]")<iterator_t<V>>)class common_view : public view_interface<common_view<V>> {private: V *base_* = V(); // *exposition only*public: common_view() requires [default_initializable](concept.default.init#concept:default_initializable "18.4.12 Concept default_­initializable [concept.default.init]")<V> = default; constexpr explicit common_view(V r); constexpr V base() const & requires [copy_constructible](concept.copyconstructible#concept:copy_constructible "18.4.14 Concept copy_­constructible [concept.copyconstructible]")<V> { return *base_*; }constexpr V base() && { return std::move(*base_*); }constexpr auto begin() requires (![*simple-view*](range.utility.helpers#concept:simple-view "25.5.2 Helper concepts [range.utility.helpers]")<V>) {if constexpr ([random_access_range](range.refinements#concept:random_access_range "25.4.6 Other range refinements [range.refinements]")<V> && [sized_range](range.sized#concept:sized_range "25.4.4 Sized ranges [range.sized]")<V>)return ranges::begin(*base_*); elsereturn common_iterator<iterator_t<V>, sentinel_t<V>>(ranges::begin(*base_*)); }constexpr auto begin() const requires [range](range.range#concept:range "25.4.2 Ranges [range.range]")<const V> {if constexpr ([random_access_range](range.refinements#concept:random_access_range "25.4.6 Other range refinements [range.refinements]")<const V> && [sized_range](range.sized#concept:sized_range "25.4.4 Sized ranges [range.sized]")<const V>)return ranges::begin(*base_*); elsereturn common_iterator<iterator_t<const V>, sentinel_t<const V>>(ranges::begin(*base_*)); }constexpr auto end() requires (![*simple-view*](range.utility.helpers#concept:simple-view "25.5.2 Helper concepts [range.utility.helpers]")<V>) {if constexpr ([random_access_range](range.refinements#concept:random_access_range "25.4.6 Other range refinements [range.refinements]")<V> && [sized_range](range.sized#concept:sized_range "25.4.4 Sized ranges [range.sized]")<V>)return ranges::begin(*base_*) + ranges::distance(*base_*); elsereturn common_iterator<iterator_t<V>, sentinel_t<V>>(ranges::end(*base_*)); }constexpr auto end() const requires [range](range.range#concept:range "25.4.2 Ranges [range.range]")<const V> {if constexpr ([random_access_range](range.refinements#concept:random_access_range "25.4.6 Other range refinements [range.refinements]")<const V> && [sized_range](range.sized#concept:sized_range "25.4.4 Sized ranges [range.sized]")<const V>)return ranges::begin(*base_*) + ranges::distance(*base_*); elsereturn common_iterator<iterator_t<const V>, sentinel_t<const V>>(ranges::end(*base_*)); }constexpr auto size() requires [sized_range](range.sized#concept:sized_range "25.4.4 Sized ranges [range.sized]")<V> {return ranges::size(*base_*); }constexpr auto size() const requires [sized_range](range.sized#concept:sized_range "25.4.4 Sized ranges [range.sized]")<const V> {return ranges::size(*base_*); }constexpr auto reserve_hint() requires [approximately_sized_range](range.approximately.sized#concept:approximately_sized_range "25.4.3 Approximately sized ranges [range.approximately.sized]")<V> {return ranges::reserve_hint(*base_*); }constexpr auto reserve_hint() const requires [approximately_sized_range](range.approximately.sized#concept:approximately_sized_range "25.4.3 Approximately sized ranges [range.approximately.sized]")<const V> {return ranges::reserve_hint(*base_*); }}; template<class R> common_view(R&&) -> common_view<views::all_t<R>>;}

[ð](#lib:common_view,constructor)

`constexpr explicit common_view(V base);
`

[1](#view-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L9872)

*Effects*: Initializes *base_* with std​::​move(base)[.](#view-1.sentence-1)
