Section hub

std::ranges::views::elements, std::ranges::elements_view

  1. Accepts a view of tuple-like values, and issues a view with a value type of the Nth element of the adapted view’s value-type.

# Declarations

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*)

# Example

#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';
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 3494C++20elements_view was never a borrowed_rangeit is a borrowed_rangeif its underlying view is
LWG 3502C++20dangling reference could be obtained from elements_viewsuch usage is forbidden

# See also

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.

Core adapters

Start here for the adapters most people reach for first when building pipelines.

Utility views

These adapt shape, ownership, or projection rather than representing the “headline” pipeline steps.

New in C++23 / C++26

Newer adapters, kept as a compact scan list with only standard badges.

All entities by category

A lighter-weight index of the full ranges surface, grouped by conceptual task instead of raw page-tree names.

74 entities