views::transform
C++23Apply a function to each element lazily.
Section hub
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)...> &&
/*can-reference*/<std::invoke_result_t<F&,
/*REPEAT*/(ranges::range_reference_t<V>, N)...>>
class adjacent_transform_view
: public ranges::view_interface<adjacent_transform_view<V, F, N>>
(since C++23)
namespace views {
template< std::size_t N >
constexpr /* unspecified */ adjacent_transform = /* unspecified */;
}
(since C++23)
namespace views {
inline constexpr auto pairwise_transform = adjacent_transform<2>;
}
(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)
views::adjacent_transform only accepts foward ranges even when N is 0.
#include <array>
#include <iostream>
#include <ranges>
int main()
{
constexpr static std::array data{1, 2, 3, 4, 5, 6};
constexpr int window{3};
auto Fun = [](auto... ints) { return (... + ints); };
// Alternatively, the Fun could be any ternary (if window == 3) callable, e.g.:
// auto Fun = [](int x, int y, int z) { return x + y + z; };
constexpr auto view = data | std::views::adjacent_transform<window>(Fun);
static_assert(
view.size() == (data.size() - window + 1)
&& std::array{6, 9, 12, 15}
== std::array{view[0], view[1], view[2], view[3]}
&& view[0] == Fun(data[0], data[1], data[2])
&& view[1] == Fun(data[1], data[2], data[3])
&& view[2] == Fun(data[2], data[3], data[4])
&& view[3] == Fun(data[3], data[4], data[5])
);
for (int x : view)
std::cout << x << ' ';
std::cout << '\n';
}
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 4098 | C++23 | views::adjacent_transform<0> used to accept input-only ranges | made rejected |
This hub groups the ranges library by user task rather than by raw reference tree shape. View types and adaptor objects are presented as the same conceptual item.
Start here for the adapters most people reach for first when building pipelines.
Apply a function to each element lazily.
Keep only elements that satisfy a predicate.
Keep the first N elements from a source range.
Skip the first N elements and expose the rest.
Split a range into non-overlapping fixed-size subranges.
Flatten a range of ranges into a single lazy sequence.
These adapt shape, ownership, or projection rather than representing the “headline” pipeline steps.
Normalize a range into a view-compatible form.
Adapt iterator/sentinel pairs into a common-range shape.
Wrap an existing range by reference.
Store and expose a range with unique ownership.
Package iterator + sentinel as a view-like object.
Project tuple-like elements to their key component.
Project tuple-like elements to their value component.
Newer adapters, kept as a compact scan list with only standard badges.
A lighter-weight index of the full ranges surface, grouped by conceptual task instead of raw page-tree names.