views::transform
C++23Apply a function to each element lazily.
Section hub
template< ranges::input_range V, std::size_t N >
requires ranges::view<V> &&
/*has-tuple-element*/<ranges::range_value_t<V>, N> &&
/*has-tuple-element*/<std::remove_reference_t<
ranges::range_reference_t<V>>, N> &&
/*returnable-element*/<ranges::range_reference_t<V>, N>
class elements_view
: public ranges::view_interface<elements_view<V, N>>;
(since C++20)
namespace views {
template< std::size_t N >
constexpr /* unspecified */ elements = /* unspecified */;
}
(since C++20)
Call signature
template< ranges::viewable_range R >
requires /* see below */
constexpr ranges::view auto elements<N>( R&& r );
(since C++20)
Helper concepts
template< class T, std::size_t N >
concept /*has-tuple-element*/ =
requires(T t) {
typename std::tuple_size<T>::type;
requires N < std::tuple_size_v<T>;
typename std::tuple_element_t<N, T>;
{ std::get<N>(t) } -> std::convertible_to<
const std::tuple_element_t<N, T>&>;
};
(until C++23) (exposition only*)
template< class T, std::size_t N >
concept /*has-tuple-element*/ =
/*tuple-like*/<T> && N < std::tuple_size_v<T>
(since C++23) (exposition only*)
template< class T, std::size_t N >
concept returnable-element =
std::is_reference_v<T> || std::move_constructible<
std::tuple_element_t<N, T>>;
(exposition only*)
#include <iostream>
#include <ranges>
#include <string>
#include <tuple>
#include <vector>
int main()
{
const std::vector<std::tuple<int, char, std::string>> vt
{
{1, 'A', "α"},
{2, 'B', "β"},
{3, 'C', "γ"},
{4, 'D', "δ"},
{5, 'E', "ε"},
};
for (int const e : std::views::elements<0>(vt))
std::cout << e << ' ';
std::cout << '\n';
for (char const e : vt | std::views::elements<1>)
std::cout << e << ' ';
std::cout << '\n';
for (std::string const& e : std::views::elements<2>(vt))
std::cout << e << ' ';
std::cout << '\n';
}
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 3494 | C++20 | elements_view was never a borrowed_range | it is a borrowed_rangeif its underlying view is |
| LWG 3502 | C++20 | dangling reference could be obtained from elements_view | such usage is forbidden |
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.