views::transform
C++23Apply a function to each element lazily.
Section hub
template< ranges::view V >
requires (not ranges::common_range<V> and
std::copyable<ranges::iterator_t<V>>)
class common_view
: public ranges::view_interface<common_view<V>>
(since C++20)
namespace views {
inline constexpr /* unspecified */ common = /* unspecified */;
}
(since C++20)
Call signature
template< ranges::viewable_range R >
requires /* see below */
constexpr ranges::view auto common( R&& r );
(since C++20)
common_view can be useful for working with legacy algorithms that expect the iterator and sentinel are of the same type.
#include <iostream>
#include <iterator>
#include <list>
#include <numeric>
#include <ranges>
int main()
{
auto v1 = {1, 2, 3, 4, 5};
auto i1 = std::counted_iterator{v1.begin(), std::ssize(v1)};
auto r1 = std::ranges::subrange{i1, std::default_sentinel};
// auto e1 = std::accumulate(r1.begin(), r1.end(), 0); // error: "common range" required
auto c1 = std::ranges::common_view{r1};
std::cout << "accumulate: " << std::accumulate(c1.begin(), c1.end(), 0) << '\n';
// inherited from ranges::view_interface:
std::cout << "c1.front(): " << c1.front() << '\n';
std::cout << "c1.back(): " << c1.back() << '\n';
std::cout << "c1.data(): " << c1.data() << '\n';
std::cout << "c1[0]: " << c1[0] << '\n';
auto v2 = std::list{1, 2, 3, 4, 5};
auto i2 = std::counted_iterator{v2.begin(), std::ssize(v2)};
auto r2 = std::ranges::subrange{i2, std::default_sentinel};
// auto e2 = std::accumulate(r2.begin(), r2.end(), 0); // error: "common range" required
auto c2 = std::ranges::common_view{ r2 };
std::cout << "accumulate: " << std::accumulate(c2.begin(), c2.end(), 0) << '\n';
// inherited from ranges::view_interface:
std::cout << "c2.front(): " << c2.front() << '\n';
// auto e3 = c2.back(); // error: "bidirectional range" required
// auto e4 = c2.data(); // error: "contiguous range" required
// auto e5 = c2[0]; // error: "random access range" required
}
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 3494 | C++20 | common_view was never a borrowed_range | it is a borrowed_range if its underlying view is |
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.