24 Ranges library [ranges]

24.7 Range adaptors [range.adaptors]

24.7.20 Zip transform view [range.zip.transform]

24.7.20.1 Overview [range.zip.transform.overview]

zip_­transform_­view takes an invocable object and any number of views and produces a view whose element is the result of applying the invocable object to the elements of all views.
The name views​::​zip_­transform denotes a customization point object ([customization.point.object]).
Let F be a subexpression, and let Es... be a pack of subexpressions.
  • If Es is an empty pack, let FD be decay_­t<decltype((F))>.
    • If copy_­constructible<FD> && regular_­invocable<FD&> is false, or if decay_­t<invoke_­result_­t<FD&>> is not an object type, views​::​zip_­transform(F, Es...) is ill-formed.
    • Otherwise, the expression views​::​zip_­transform(F, Es...) is expression-equivalent to ((void)F, auto(views::empty<decay_t<invoke_result_t<FD&>>>))
  • Otherwise, the expression views​::​zip_­transform(F, Es...) is expression-equivalent to zip_­transform_­view(F, Es...).
[Example 1: vector v1 = {1, 2}; vector v2 = {4, 5, 6}; for (auto i : views::zip_transform(plus(), v1, v2)) { cout << i << ' '; // prints: 5 7 } — end example]

24.7.20.2 Class template zip_­transform_­view [range.zip.transform.view]

namespace std::ranges { template<copy_­constructible F, input_­range... Views> requires (view<Views> && ...) && (sizeof...(Views) > 0) && is_object_v<F> && regular_­invocable<F&, range_reference_t<Views>...> && can-reference<invoke_result_t<F&, range_reference_t<Views>...>> class zip_transform_view : public view_interface<zip_transform_view<F, Views...>> { copyable-box<F> fun_; // exposition only zip_view<Views...> zip_; // exposition only using InnerView = zip_view<Views...>; // exposition only template<bool Const> using ziperator = iterator_t<maybe-const<Const, InnerView>>; // exposition only template<bool Const> using zentinel = sentinel_t<maybe-const<Const, InnerView>>; // exposition only // [range.zip.transform.iterator], class template zip_­transform_­view​::​iterator template<bool> class iterator; // exposition only // [range.zip.transform.sentinel], class template zip_­transform_­view​::​sentinel template<bool> class sentinel; // exposition only public: zip_transform_view() = default; constexpr explicit zip_transform_view(F fun, Views... views); constexpr auto begin() { return iterator<false>(*this, zip_.begin()); } constexpr auto begin() const requires range<const InnerView> && regular_­invocable<const F&, range_reference_t<const Views>...> { return iterator<true>(*this, zip_.begin()); } constexpr auto end() { if constexpr (common_­range<InnerView>) { return iterator<false>(*this, zip_.end()); } else { return sentinel<false>(zip_.end()); } } constexpr auto end() const requires range<const InnerView> && regular_­invocable<const F&, range_reference_t<const Views>...> { if constexpr (common_­range<const InnerView>) { return iterator<true>(*this, zip_.end()); } else { return sentinel<true>(zip_.end()); } } constexpr auto size() requires sized_­range<InnerView> { return zip_.size(); } constexpr auto size() const requires sized_­range<const InnerView> { return zip_.size(); } }; template<class F, class... Rs> zip_transform_view(F, Rs&&...) -> zip_transform_view<F, views::all_t<Rs>...>; }
constexpr explicit zip_transform_view(F fun, Views... views);
Effects: Initializes fun_­ with std​::​move(fun) and zip_­ with std​::​move(views)....

24.7.20.3 Class template zip_­transform_­view​::​iterator [range.zip.transform.iterator]

namespace std::ranges { template<copy_­constructible F, input_­range... Views> requires (view<Views> && ...) && (sizeof...(Views) > 0) && is_object_v<F> && regular_­invocable<F&, range_reference_t<Views>...> && can-reference<invoke_result_t<F&, range_reference_t<Views>...>> template<bool Const> class zip_transform_view<F, Views...>::iterator { using Parent = maybe-const<Const, zip_transform_view>; // exposition only using Base = maybe-const<Const, InnerView>; // exposition only Parent* parent_ = nullptr; // exposition only ziperator<Const> inner_; // exposition only constexpr iterator(Parent& parent, ziperator<Const> inner); // exposition only public: using iterator_category = see below; // not always present using iterator_concept = typename ziperator<Const>::iterator_concept; using value_type = remove_cvref_t<invoke_result_t<maybe-const<Const, F>&, range_reference_t<maybe-const<Const, Views>>...>>; using difference_type = range_difference_t<Base>; iterator() = default; constexpr iterator(iterator<!Const> i) requires Const && convertible_­to<ziperator<false>, ziperator<Const>>; constexpr decltype(auto) operator*() const noexcept(see below); constexpr iterator& operator++(); constexpr void operator++(int); constexpr iterator operator++(int) requires forward_­range<Base>; 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) requires equality_­comparable<ziperator<Const>>; 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<ziperator<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<ziperator<Const>, ziperator<Const>>; }; }
The member typedef-name iterator​::​iterator_­category is defined if and only if Base models forward_­range.
In that case, iterator​::​iterator_­category is defined as follows:
  • If invoke_result_t<maybe-const<Const, F>&, range_reference_t<maybe-const<Const, Views>>...> is not an lvalue reference, iterator_­category denotes input_­iterator_­tag.
  • Otherwise, let Cs denote the pack of types iterator_­traits<iterator_­t<maybe-const<Const, Views>>>​::​iterator_­category....
    • If (derived_­from<Cs, random_­access_­iterator_­tag> && ...) is true, iterator_­category denotes random_­access_­iterator_­tag.
    • Otherwise, if (derived_­from<Cs, bidirectional_­iterator_­tag> && ...) is true, iterator_­category denotes bidirectional_­iterator_­tag.
    • Otherwise, if (derived_­from<Cs, forward_­iterator_­tag> && ...) is true, iterator_­category denotes forward_­iterator_­tag.
    • Otherwise, iterator_­category denotes input_­iterator_­tag.
constexpr iterator(Parent& parent, ziperator<Const> inner);
Effects: Initializes parent_­ with addressof(parent) and inner_­ with std​::​move(inner).
constexpr iterator(iterator<!Const> i) requires Const && convertible_­to<ziperator<false>, ziperator<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, …, (sizeof...(Views)-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 void operator++(int);
Effects: Equivalent to: ++*this.
constexpr iterator operator++(int) requires forward_­range<Base>;
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([&]<class... Is>(const Is&... iters) -> decltype(auto) { return invoke(*parent_->fun_, iters[iter_difference_t<Is>(n)]...); }, inner_.current_);
friend constexpr bool operator==(const iterator& x, const iterator& y) requires equality_­comparable<ziperator<Const>>; 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<ziperator<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<ziperator<Const>, ziperator<Const>>;
Effects: Equivalent to: return x.inner_­ - y.inner_­;

24.7.20.4 Class template zip_­transform_­view​::​sentinel [range.zip.transform.sentinel]

namespace std::ranges { template<copy_­constructible F, input_­range... Views> requires (view<Views> && ...) && (sizeof...(Views) > 0) && is_object_v<F> && regular_­invocable<F&, range_reference_t<Views>...> && can-reference<invoke_result_t<F&, range_reference_t<Views>...>> template<bool Const> class zip_transform_view<F, Views...>::sentinel { zentinel<Const> inner_; // exposition only constexpr explicit sentinel(zentinel<Const> inner); // exposition only public: sentinel() = default; constexpr sentinel(sentinel<!Const> i) requires Const && convertible_­to<zentinel<false>, zentinel<Const>>; template<bool OtherConst> requires sentinel_­for<zentinel<Const>, ziperator<OtherConst>> friend constexpr bool operator==(const iterator<OtherConst>& x, const sentinel& y); template<bool OtherConst> requires sized_­sentinel_­for<zentinel<Const>, ziperator<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<zentinel<Const>, ziperator<OtherConst>> friend constexpr range_difference_t<maybe-const<OtherConst, InnerView>> operator-(const sentinel& x, const iterator<OtherConst>& y); }; }
constexpr explicit sentinel(zentinel<Const> inner);
Effects: Initializes inner_­ with inner.
constexpr sentinel(sentinel<!Const> i) requires Const && convertible_­to<zentinel<false>, zentinel<Const>>;
Effects: Initializes inner_­ with std​::​move(i.inner_­).
template<bool OtherConst> requires sentinel_­for<zentinel<Const>, ziperator<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<zentinel<Const>, ziperator<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<zentinel<Const>, ziperator<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_­;