std::ranges::views::adjacent_transform, std::ranges::adjacent_transform_view, std::ranges::views::pairwise_transform

< cpp‎ | ranges
Ranges library
Range access
Range conversions
Range primitives

Dangling iterator handling
Range concepts


Range generators
Range adaptor objects
Range adaptor closure objects
Helper items
Defined in header <ranges>
template< ranges::forward_range V, std::move_constructible F, std::size_t N >

  requires ranges::view<V> && (N > 0) && std::is_object_v<F> &&
           std::regular_invocable<F&, __REPEAT(ranges::range_reference_t<V>, N)...> &&
               __REPEAT(ranges::range_reference_t<V>, N)...>>
class adjacent_transform_view :

  public ranges::view_interface<adjacent_transform_view<V, F, N>>
(1) (since C++23)
namespace views {

template< std::size_t N >
    inline constexpr /* unspecified */ adjacent_transform = /* unspecified */;

(2) (since C++23)
namespace views {

    inline constexpr auto pairwise_transform = adjacent_transform<2>;

(3) (since C++23)
Call signature
template< ranges::viewable_range R, class F >

    requires /* see below */

constexpr ranges::view auto adjacent_transform<N>( R&& r, F&& fun );
(since C++23)
template< class F >
constexpr /*range adaptor closure*/ adjacent_transform<N>( F&& fun );
(since C++23)
1) adjacent_transform_view is a range adaptor that takes a view and an invocable object fun, and produces a view whose ith element is a value that is the result of applying fun to each element in [i, i + N) of the original view. F always has arity N.
Let S be the size of the original view. Then the size of produced view is:
  • S - N + 1, if S >= N,
  • 0 otherwise, and the resulting view is empty.
2) The name views::adjacent_transform<N> denotes a RangeAdaptorObject. Given subexpressions e and f, and a constant expression N, the expression views::adjacent_transform<N>(e, f) is expression-equivalent to:

adjacent_transform_view always models forward_range, and models bidirectional_range, random_access_range, or sized_range, if adapted view type models the corresponding concept.


[edit] Expression-equivalent

Expression e is expression-equivalent to expression f, if

  • e and f have the same effects, and
  • either both are constant subexpressions or else neither is a constant subexpression, and
  • either both are potentially-throwing or else neither is potentially-throwing (i.e. noexcept(e) == noexcept(f)).

[edit] Data members

Typical implementations of adjacent_transform_view hold two non-static data members:

  • fun_ of type __movable_box<F>,
  • inner_ of type ranges::adjacent_view<V,N>

These names are for exposition only.

[edit] Member functions

constructs a adjacent_transform_view
(public member function) [edit]
returns an iterator to the beginning
(public member function) [edit]
returns an iterator or a sentinel to the end
(public member function) [edit]
returns the number of elements. Provided only if the underlying (adapted) range satisfies sized_range.
(public member function) [edit]
Inherited from std::ranges::view_interface
Returns whether the derived view is empty. Provided if it satisfies sized_range or forward_range.
(public member function of std::ranges::view_interface<D>) [edit]
Returns whether the derived view is not empty. Provided if ranges::empty is applicable to it.
(public member function of std::ranges::view_interface<D>) [edit]
Returns the first element in the derived view. Provided if it satisfies forward_range.
(public member function of std::ranges::view_interface<D>) [edit]
Returns the last element in the derived view. Provided if it satisfies bidirectional_range and common_range.
(public member function of std::ranges::view_interface<D>) [edit]
Returns the nth element in the derived view. Provided if it satisfies random_access_range.
(public member function of std::ranges::view_interface<D>) [edit]

[edit] Deduction guides


[edit] Member types

Member type Definition
InnerView (private) ranges::adjacent_view<V, N>. The name is for exposition only.
inner_iterator (private)
inner_sentinel (private)

[edit] Nested classes

the iterator type
(exposition-only member class template)
the sentinel type used when adjacent_transform_view is not a common_range
(exposition-only member class template)

[edit] Notes

Feature-test macro Value Std Comment
__cpp_lib_ranges_zip 202110L (C++23)


[edit] Example

A link to test: Compiler Explorer/g++-13

#include <array>
#include <iostream>
#include <ranges>
int main()
    constexpr static auto source = {1, 2, 3, 4, 5, 6};
    constexpr int window {3};
    auto Fun = [](auto... ints) { return (... + ints); };
    // Alternatively, the Fun could be any ternary (in case window = 3) callable, e.g.:
    // auto Fun = [](int x, int y, int z) { return x + y + z; };
    constexpr auto view = source | std::views::adjacent_transform< window >(Fun);
        view.size() == (source.size() - window + 1)
        std::array{view[0], view[1], view[2], view[3]} == std::array{6, 9, 12, 15}
    for (int x : view)
        std::cout << x << ' ';
    std::cout << '\n';


6 9 12 15

[edit] References

  • C++23 standard (ISO/IEC 14882:2023):
  • 26.7.26 Adjacent transform view [range.adjacent.transform]

[edit] See also

a view consisting of tuples of references to adjacent elements of the adapted view
(class template) (range adaptor object) [edit]
a view of a sequence that applies a transformation function to each element
(class template) (range adaptor object) [edit]
a view consisting of tuples of results of application of a transformation function to corresponding elements of the adapted views
(class template) (customization point object) [edit]
applies a function to a range of elements
(niebloid) [edit]