24 Ranges library [ranges]

24.7 Range adaptors [range.adaptors]

24.7.22 Adjacent transform view [range.adjacent.transform]

24.7.22.1 Overview [range.adjacent.transform.overview]

adjacent_­transform_­view takes an invocable object and a view and produces a view whose element is the result of applying the invocable object to the through elements of the original view.
If the original view has fewer than N elements, the resulting view is empty.
The name views​::​adjacent_­transform<N> denotes a range adaptor object ([range.adaptor.object]).
Given subexpressions E and F and a constant expression N:
  • If N is equal to 0, views​::​adjacent_­transform<N>(E, F) is expression-equivalent to ((void)E, views​::​zip_­transform(F)), except that the evaluations of E and F are indeterminately sequenced.
  • Otherwise, the expression views​::​adjacent_­transform<N>(E, F) is expression-equivalent to adjacent_­transform_­view<views​::​all_­t<decltype((E))>, decay_­t<decltype((F))>, N>(E, F).
[Example 1: vector v = {1, 2, 3, 4}; for (auto i : v | views::adjacent_transform<2>(std::multiplies())) { cout << i << ' '; // prints: 2 6 12 } — end example]

24.7.22.2 Class template adjacent_­transform_­view [range.adjacent.transform.view]

namespace std::ranges { template<forward_­range V, copy_­constructible F, size_t N> requires view<V> && (N > 0) && is_object_v<F> && regular_­invocable<F&, REPEAT(range_reference_t<V>, N)...> && can-reference<invoke_result_t<F&, REPEAT(range_reference_t<V>, N)...>> class adjacent_transform_view : public view_interface<adjacent_transform_view<V, F, N>> { copyable-box<F> fun_; // exposition only adjacent_view<V, N> inner_; // exposition only using InnerView = adjacent_view<V, N>; // exposition only template<bool Const> using inner-iterator = iterator_t<maybe-const<Const, InnerView>>; // exposition only template<bool Const> using inner-sentinel = sentinel_t<maybe-const<Const, InnerView>>; // exposition only // [range.adjacent.transform.iterator], class template adjacent_­transform_­view​::​iterator template<bool> class iterator; // exposition only // [range.adjacent.transform.sentinel], class template adjacent_­transform_­view​::​sentinel template<bool> class sentinel; // exposition only public: adjacent_transform_view() = default; constexpr explicit adjacent_transform_view(V base, F fun); constexpr auto begin() { return iterator<false>(*this, inner_.begin()); } constexpr auto begin() const requires range<const InnerView> && regular_­invocable<const F&, REPEAT(range_reference_t<const V>, N)...> { return iterator<true>(*this, inner_.begin()); } constexpr auto end() { if constexpr (common_­range<InnerView>) { return iterator<false>(*this, inner_.end()); } else { return sentinel<false>(inner_.end()); } } constexpr auto end() const requires range<const InnerView> && regular_­invocable<const F&, REPEAT(range_reference_t<const V>, N)...> { if constexpr (common_­range<const InnerView>) { return iterator<true>(*this, inner_.end()); } else { return sentinel<true>(inner_.end()); } } constexpr auto size() requires sized_­range<InnerView> { return inner_.size(); } constexpr auto size() const requires sized_­range<const InnerView> { return inner_.size(); } }; }
constexpr explicit adjacent_transform_view(V base, F fun);
Effects: Initializes fun_­ with std​::​move(fun) and inner_­ with std​::​move(base).

24.7.22.3 Class template adjacent_­transform_­view​::​iterator [range.adjacent.transform.iterator]

namespace std::ranges { template<forward_­range V, copy_­constructible F, size_t N> requires view<V> && (N > 0) && is_object_v<F> && regular_­invocable<F&, REPEAT(range_reference_t<V>, N)...> && can-reference<invoke_result_t<F&, REPEAT(range_reference_t<V>, N)...>> template<bool Const> class adjacent_transform_view<F, V...>::iterator { using Parent = maybe-const<Const, adjacent_transform_view>; // exposition only using Base = maybe-const<Const, V>; // exposition only Parent* parent_ = nullptr; // exposition only inner-iterator<Const> inner_; // exposition only constexpr iterator(Parent& parent, inner-iterator<Const> inner); // exposition only public: using iterator_category = see below; using iterator_concept = typename inner-iterator<Const>::iterator_concept; using value_type = remove_cvref_t<invoke_result_t<maybe-const<Const, F>&, REPEAT(range_reference_t<Base>, N)...>>; using difference_type = range_difference_t<Base>; iterator() = default; constexpr iterator(iterator<!Const> i) requires Const && convertible_­to<inner-iterator<false>, inner-iterator<Const>>; constexpr decltype(auto) operator*() const noexcept(see below); constexpr iterator& operator++(); constexpr iterator operator++(int); constexpr iterator& operator--() requires bidirectional_­range<Base>; constexpr iterator operator--(int) requires bidirectional_­range<Base>; constexpr iterator& operator+=(difference_type x) requires random_­access_­range<Base>; constexpr iterator& operator-=(difference_type x) requires random_­access_­range<Base>; constexpr decltype(auto) operator[](difference_type n) const requires random_­access_­range<Base>; friend constexpr bool operator==(const iterator& x, const iterator& y); friend constexpr bool operator<(const iterator& x, const iterator& y) requires random_­access_­range<Base>; friend constexpr bool operator>(const iterator& x, const iterator& y) requires random_­access_­range<Base>; friend constexpr bool operator<=(const iterator& x, const iterator& y) requires random_access_range<Base>; friend constexpr bool operator>=(const iterator& x, const iterator& y) requires random_­access_­range<Base>; friend constexpr auto operator<=>(const iterator& x, const iterator& y) requires random_­access_­range<Base> && three_­way_­comparable<inner-iterator<Const>>; friend constexpr iterator operator+(const iterator& i, difference_type n) requires random_­access_­range<Base>; friend constexpr iterator operator+(difference_type n, const iterator& i) requires random_­access_­range<Base>; friend constexpr iterator operator-(const iterator& i, difference_type n) requires random_­access_­range<Base>; friend constexpr difference_type operator-(const iterator& x, const iterator& y) requires sized_­sentinel_­for<inner-iterator<Const>, inner-iterator<Const>>; }; }
The member typedef-name iterator​::​iterator_­category is defined as follows:
  • If invoke_­result_­t<maybe-const<Const, F>&, REPEAT(range_­reference_­t<Base>, N)...> is
    not an lvalue reference, iterator_­category denotes input_­iterator_­tag.
  • Otherwise, let C denote the type iterator_­traits<iterator_­t<Base>>​::​iterator_­category.
    • If derived_­from<C, random_­access_­iterator_­tag> is true, iterator_­category denotes random_­access_­iterator_­tag.
    • Otherwise, if derived_­from<C, bidirectional_­iterator_­tag> is true, iterator_­category denotes bidirectional_­iterator_­tag.
    • Otherwise, if derived_­from<C, forward_­iterator_­tag> is true, iterator_­category denotes forward_­iterator_­tag.
    • Otherwise, iterator_­category denotes input_­iterator_­tag.
constexpr iterator(Parent& parent, inner-iterator<Const> inner);
Effects: Initializes parent_­ with addressof(parent) and inner_­ with std​::​move(inner).
constexpr iterator(iterator<!Const> i) requires Const && convertible_­to<inner-iterator<false>, inner-iterator<Const>>;
Effects: Initializes parent_­ with i.parent_­ and inner_­ with std​::​move(i.inner_­).
constexpr decltype(auto) operator*() const noexcept(see below);
Effects: Equivalent to: return apply([&](const auto&... iters) -> decltype(auto) { return invoke(*parent_->fun_, *iters...); }, inner_.current_);
Remarks: Let Is be the pack 0, 1, …, (N-1).
The exception specification is equivalent to: noexcept(invoke(*parent_->fun_, *std::get<Is>(inner_.current_)...))
constexpr iterator& operator++();
Effects: Equivalent to: ++inner_; return *this;
constexpr iterator operator++(int);
Effects: Equivalent to: auto tmp = *this; ++*this; return tmp;
constexpr iterator& operator--() requires bidirectional_­range<Base>;
Effects: Equivalent to: --inner_; return *this;
constexpr iterator operator--(int) requires bidirectional_­range<Base>;
Effects: Equivalent to: auto tmp = *this; --*this; return tmp;
constexpr iterator& operator+=(difference_type x) requires random_­access_­range<Base>;
Effects: Equivalent to: inner_ += x; return *this;
constexpr iterator& operator-=(difference_type x) requires random_­access_­range<Base>;
Effects: Equivalent to: inner_ -= x; return *this;
constexpr decltype(auto) operator[](difference_type n) const requires random_­access_­range<Base>;
Effects: Equivalent to: return apply([&](const auto&... iters) -> decltype(auto) { return invoke(*parent_->fun_, iters[n]...); }, inner_.current_);
friend constexpr bool operator==(const iterator& x, const iterator& y); friend constexpr bool operator<(const iterator& x, const iterator& y) requires random_­access_­range<Base>; friend constexpr bool operator>(const iterator& x, const iterator& y) requires random_­access_­range<Base>; friend constexpr bool operator<=(const iterator& x, const iterator& y) requires random_­access_­range<Base>; friend constexpr bool operator>=(const iterator& x, const iterator& y) requires random_­access_­range<Base>; friend constexpr auto operator<=>(const iterator& x, const iterator& y) requires random_­access_­range<Base> && three_­way_­comparable<inner-iterator<Const>>;
Let op be the operator.
Effects: Equivalent to: return x.inner_­ op y.inner_­;
friend constexpr iterator operator+(const iterator& i, difference_type n) requires random_­access_­range<Base>; friend constexpr iterator operator+(difference_type n, const iterator& i) requires random_­access_­range<Base>;
Effects: Equivalent to: return iterator(*i.parent_­, i.inner_­ + n);
friend constexpr iterator operator-(const iterator& i, difference_type n) requires random_­access_­range<Base>;
Effects: Equivalent to: return iterator(*i.parent_­, i.inner_­ - n);
friend constexpr difference_type operator-(const iterator& x, const iterator& y) requires sized_­sentinel_­for<inner-iterator<Const>, inner-iterator<Const>>;
Effects: Equivalent to: return x.inner_­ - y.inner_­;

24.7.22.4 Class template adjacent_­transform_­view​::​sentinel [range.adjacent.transform.sentinel]

namespace std::ranges { template<forward_­range V, copy_­constructible F, size_t N> requires view<V> && (N > 0) && is_object_v<F> && regular_­invocable<F&, REPEAT(range_reference_t<V>, N)...> && can-reference<invoke_result_t<F&, REPEAT(range_reference_t<V>, N)...>> template<bool Const> class adjacent_transform_view<V, F, N>::sentinel { inner-sentinel<Const> inner_; // exposition only constexpr explicit sentinel(inner-sentinel<Const> inner); // exposition only public: sentinel() = default; constexpr sentinel(sentinel<!Const> i) requires Const && convertible_­to<inner-sentinel<false>, inner-sentinel<Const>>; template<bool OtherConst> requires sentinel_­for<inner-sentinel<Const>, inner-iterator<OtherConst>> friend constexpr bool operator==(const iterator<OtherConst>& x, const sentinel& y); template<bool OtherConst> requires sized_­sentinel_­for<inner-sentinel<Const>, inner-iterator<OtherConst>> friend constexpr range_difference_t<maybe-const<OtherConst, InnerView>> operator-(const iterator<OtherConst>& x, const sentinel& y); template<bool OtherConst> requires sized_­sentinel_­for<inner-sentinel<Const>, inner-iterator<OtherConst>> friend constexpr range_difference_t<maybe-const<OtherConst, InnerView>> operator-(const sentinel& x, const iterator<OtherConst>& y); }; }
constexpr explicit sentinel(inner-sentinel<Const> inner);
Effects: Initializes inner_­ with inner.
constexpr sentinel(sentinel<!Const> i) requires Const && convertible_­to<inner-sentinel<false>, inner-sentinel<Const>>;
Effects: Initializes inner_­ with std​::​move(i.inner_­).
template<bool OtherConst> requires sentinel_­for<inner-sentinel<Const>, inner-iterator<OtherConst>> friend constexpr bool operator==(const iterator<OtherConst>& x, const sentinel& y);
Effects: Equivalent to return x.inner_­ == y.inner_­;
template<bool OtherConst> requires sized_­sentinel_­for<inner-sentinel<Const>, inner-iterator<OtherConst>> friend constexpr range_difference_t<maybe-const<OtherConst, InnerView>> operator-(const iterator<OtherConst>& x, const sentinel& y); template<bool OtherConst> requires sized_­sentinel_­for<inner-sentinel<Const>, inner-iterator<OtherConst>> friend constexpr range_difference_t<maybe-const<OtherConst, InnerView>> operator-(const sentinel& x, const iterator<OtherConst>& y);
Effects: Equivalent to return x.inner_­ - y.inner_­;