std::ranges::view::reverse,std::ranges::reverse_view (3) - Linux Man Pages

std::ranges::view::reverse,std::ranges::reverse_view: std::ranges::view::reverse,std::ranges::reverse_view


std::ranges::view::reverse,std::ranges::reverse_view - std::ranges::view::reverse,std::ranges::reverse_view


template< View V >
requires BidirectionalRange<V> (1) (since C++20)
class reverse_view : public ranges::view_interface<reverse_view<V>>
namespace view {
inline constexpr /*unspecified*/ reverse = /*unspecified*/; (2) (since C++20)

1) A range adaptor that represents a view of underlying View with reversed order.
2) The expression view::reverse(E) is expression-equivalent to (has the same effect as) one of the following expressions, except that E is evaluated only once:

* E.base(), if the type of E is a (possibly cv-qualified) specialization of reverse_view;
* otherwise, if the type of E is (possibly cv-qualified) ranges::subrange<std::reverse_iterator<I>, std::reverse_iterator<I>, K> for some iterator type I and value K of type ranges::subrange_kind:

      * ranges::subrange<I, I, K>(E.end().base(), E.begin().base(), E.size()), if K is ranges::subrange_kind::sized;
      * otherwise ranges::subrange<I, I, K>(E.end().base(), E.begin().base());

* otherwise ranges::reverse_­view{E}.

In other words, view::reverse unwraps reversed views if possible.

Data members


V base_ = V(); /* exposition-only */

the underlying view

Member functions


reverse_view() = default; (1)
constexpr reverse_view(V base); (2)
template<ViewableRange R>
requires BidirectionalRange<R> && Constructible<V, all_view<R>> (3)
constexpr reverse_view(R&& r);

1) Value-initializes base_
2) Initializes base_ with std::move(base).
3) Initializes base_ with view::all(std::forward<R>(r)).


r - range to reverse


constexpr V base() const;

Equivalent to return base_;


constexpr std::reverse_iterator<ranges::iterator_t<V>> begin(); (1)
constexpr std::reverse_iterator<ranges::iterator_t<V>> begin() (2)
requires CommonRange<V>;
constexpr std::reverse_iterator<ranges::iterator_t<const V>> begin() const (2)
requires CommonRange<const V>;

1) Returns std::make_reverse_iterator(ranges::next(ranges::begin(base_), ranges::end(base_))). In order to provide the amortized constant time complexity required by the Range concept, this function caches the result within the reverse_view object for use on subsequent calls.
2) Equivalent to return std::make_­reverse_­iterator(ranges::end(base_­));.


constexpr std::reverse_iterator<ranges::iterator_t<V>> end()
requires CommonRange<V>;
constexpr std::reverse_iterator<ranges::iterator_t<const V>> end() const
requires CommonRange<const V>;

Equivalent to return std::make_­reverse_­iterator(ranges::begin(base_­));.


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

Returns the size of the view if the view is bounded.

Deduction guides

template<class R>
reverse_view(R&&) -> reverse_view<all_view<R>>;


// Run this code

  #include <array>
  #include <ranges>
  #include <iostream>

  int main()
      std::array<int, 6> il {3, 1, 4, 1, 5, 9};
      std::ranges::reverse_view rv {il};
      for (int i : rv)
          std::cout << i << ' ';


  9 5 1 4 1 3

See also

                 iterator adaptor for reverse-order traversal
reverse_iterator (class template)