[range.reverse]

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

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

### 25.7.21 Reverse view [range.reverse]

#### [25.7.21.1](#overview) Overview [[range.reverse.overview]](range.reverse.overview)

[1](#overview-1)

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

reverse_view takes a bidirectional view and produces
another view that iterates the same elements in reverse order[.](#overview-1.sentence-1)

[2](#overview-2)

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

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

Given a subexpression E, the expressionviews​::​reverse(E) is expression-equivalent to:

- [(2.1)](#overview-2.1)

  If the type of E is
 a (possibly cv-qualified) specialization of reverse_view,
 then E.base()[.](#overview-2.1.sentence-1)

- [(2.2)](#overview-2.2)

  Otherwise, if the type of E is cv subrange<reverse_iterator<I>, reverse_iterator<I>, K> for some iterator type I and
 value K of type subrange_kind,
  * [(2.2.1)](#overview-2.2.1)

if K is subrange_kind​::​sized, thensubrange<I, I, K>(E.end().base(), E.begin().base(), E.size());

  * [(2.2.2)](#overview-2.2.2)

otherwise, subrange<I, I, K>(E.end().base(), E.begin().base())[.](#overview-2.2.sentence-1)

   However, in either case E is evaluated only once[.](#overview-2.2.sentence-2)

- [(2.3)](#overview-2.3)

  Otherwise, reverse_view{E}[.](#overview-2.3.sentence-1)

[3](#overview-3)

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

[*Example [1](#overview-example-1)*: vector<int> is {0,1,2,3,4};for (int i : is | views::reverse) cout << i << ' '; // prints 4 3 2 1 0 â *end example*]

#### [25.7.21.2](#view) Class template reverse_view [[range.reverse.view]](range.reverse.view)

[ð](#lib:reverse_view)

namespace std::ranges {template<[view](range.view#concept:view "25.4.5 Views [range.view]") V>requires [bidirectional_range](range.refinements#concept:bidirectional_range "25.4.6 Other range refinements [range.refinements]")<V>class reverse_view : public view_interface<reverse_view<V>> {private: V *base_* = V(); // *exposition only*public: reverse_view() requires [default_initializable](concept.default.init#concept:default_initializable "18.4.12 Concept default_­initializable [concept.default.init]")<V> = default; constexpr explicit reverse_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 reverse_iterator<iterator_t<V>> begin(); constexpr reverse_iterator<iterator_t<V>> begin() requires [common_range](range.refinements#concept:common_range "25.4.6 Other range refinements [range.refinements]")<V>; constexpr auto begin() const requires [common_range](range.refinements#concept:common_range "25.4.6 Other range refinements [range.refinements]")<const V>; constexpr reverse_iterator<iterator_t<V>> end(); constexpr auto end() const requires [common_range](range.refinements#concept:common_range "25.4.6 Other range refinements [range.refinements]")<const V>; 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> reverse_view(R&&) -> reverse_view<views::all_t<R>>;}

[ð](#lib:reverse_view,constructor)

`constexpr explicit reverse_view(V base);
`

[1](#view-1)

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

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

[ð](#lib:begin,reverse_view)

`constexpr reverse_iterator<iterator_t<V>> begin();
`

[2](#view-2)

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

*Returns*: make_reverse_iterator(ranges::next(ranges::begin(*base_*), ranges::end(*base_*)))

[3](#view-3)

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

*Remarks*: In order to provide the amortized constant time complexity required by
the [range](range.range#concept:range "25.4.2 Ranges [range.range]") concept, this function caches the result within thereverse_view for use on subsequent calls[.](#view-3.sentence-1)

[ð](#lib:begin,reverse_view_)

`constexpr reverse_iterator<iterator_t<V>> begin() requires [common_range](range.refinements#concept:common_range "25.4.6 Other range refinements [range.refinements]")<V>;
constexpr auto begin() const requires [common_range](range.refinements#concept:common_range "25.4.6 Other range refinements [range.refinements]")<const V>;
`

[4](#view-4)

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

*Effects*: Equivalent to: return make_reverse_iterator(ranges​::​end(*base_*));

[ð](#lib:end,reverse_view)

`constexpr reverse_iterator<iterator_t<V>> end();
constexpr auto end() const requires [common_range](range.refinements#concept:common_range "25.4.6 Other range refinements [range.refinements]")<const V>;
`

[5](#view-5)

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

*Effects*: Equivalent to: return make_reverse_iterator(ranges​::​begin(*base_*));
