24 Ranges library [ranges]

24.7 Range adaptors [range.adaptors]

24.7.16 Elements view [range.elements]

24.7.16.1 Overview [range.elements.overview]

elements_­view takes a view of tuple-like values and a size_­t, and produces a view with a value-type of the element of the adapted view's value-type.
The name views​::​elements<N> denotes a range adaptor object ([range.adaptor.object]).
Given a subexpression E and constant expression N, the expression views​::​elements<N>(E) is expression-equivalent to elements_­view<views​::​all_­t<decltype((E))>, N>{E}.
[Example 1: auto historical_figures = map{ {"Lovelace"sv, 1815}, {"Turing"sv, 1912}, {"Babbage"sv, 1791}, {"Hamilton"sv, 1936} }; auto names = historical_figures | views::elements<0>; for (auto&& name : names) { cout << name << ' '; // prints Babbage Hamilton Lovelace Turing } auto birth_years = historical_figures | views::elements<1>; for (auto&& born : birth_years) { cout << born << ' '; // prints 1791 1936 1815 1912 } — end example]
keys_­view is an alias for elements_­view<views​::​all_­t<R>, 0>, and is useful for extracting keys from associative containers.
[Example 2: auto names = keys_view{historical_figures}; for (auto&& name : names) { cout << name << ' '; // prints Babbage Hamilton Lovelace Turing } — end example]
values_­view is an alias for elements_­view<views​::​all_­t<R>, 1>, and is useful for extracting values from associative containers.
[Example 3: auto is_even = [](const auto x) { return x % 2 == 0; }; cout << ranges::count_if(values_view{historical_figures}, is_even); // prints 2 — end example]

24.7.16.2 Class template elements_­view [range.elements.view]

namespace std::ranges { template<class T, size_t N> concept has-tuple-element = // exposition only requires(T t) { typename tuple_size<T>::type; requires N < tuple_size_v<T>; typename tuple_element_t<N, T>; { get<N>(t) } -> convertible_­to<const tuple_element_t<N, T>&>; }; template<class T, size_t N> concept returnable-element = // exposition only is_reference_v<T> || move_constructible<tuple_element_t<N, T>>; template<input_­range V, size_t N> requires view<V> && has-tuple-element<range_value_t<V>, N> && has-tuple-element<remove_reference_t<range_reference_t<V>>, N> && returnable-element<range_reference_t<V>, N> class elements_view : public view_interface<elements_view<V, N>> { public: elements_view() = default; constexpr explicit elements_view(V base); constexpr V base() const& requires copy_­constructible<V> { return base_; } constexpr V base() && { return std::move(base_); } constexpr auto begin() requires (!simple-view<V>) { return iterator<false>(ranges::begin(base_)); } constexpr auto begin() const requires range<const V> { return iterator<true>(ranges::begin(base_)); } constexpr auto end() requires (!simple-view<V> && !common_­range<V>) { return sentinel<false>{ranges::end(base_)}; } constexpr auto end() requires (!simple-view<V> && common_­range<V>) { return iterator<false>{ranges::end(base_)}; } constexpr auto end() const requires range<const V> { return sentinel<true>{ranges::end(base_)}; } constexpr auto end() const requires common_­range<const V> { return iterator<true>{ranges::end(base_)}; } constexpr auto size() requires sized_­range<V> { return ranges::size(base_); } constexpr auto size() const requires sized_­range<const V> { return ranges::size(base_); } private: // [range.elements.iterator], class template elements_­view​::​iterator template<bool> struct iterator; // exposition only // [range.elements.sentinel], class template elements_­view​::​sentinel template<bool> struct sentinel; // exposition only V base_ = V(); // exposition only }; }
constexpr explicit elements_view(V base);
Effects: Initializes base_­ with std​::​move(base).

24.7.16.3 Class template elements_­view​::​iterator [range.elements.iterator]

namespace std::ranges { template<input_­range V, size_t N> requires view<V> && has-tuple-element<range_value_t<V>, N> && has-tuple-element<remove_reference_t<range_reference_t<V>>, N> && returnable-element<range_reference_t<V>, N> template<bool Const> class elements_view<V, N>::iterator { // exposition only using Base = maybe-const<Const, V>; // exposition only iterator_t<Base> current_ = iterator_t<Base>(); // exposition only static constexpr decltype(auto) get-element(const iterator_t<Base>& i); // exposition only public: using iterator_concept = see below; using iterator_category = see below; // not always present using value_type = remove_cvref_t<tuple_element_t<N, range_value_t<Base>>>; using difference_type = range_difference_t<Base>; iterator() = default; constexpr explicit iterator(iterator_t<Base> current); constexpr iterator(iterator<!Const> i) requires Const && convertible_­to<iterator_t<V>, iterator_t<Base>>; constexpr iterator_t<Base> base() const& requires copyable<iterator_t<Base>>; constexpr iterator_t<Base> base() &&; constexpr decltype(auto) operator*() const { return get-element(current_); } constexpr iterator& operator++(); constexpr void operator++(int); constexpr iterator operator++(int) requires forward_­range<Base>; constexpr iterator& operator--() requires bidirectional_­range<Base>; constexpr iterator operator--(int) requires bidirectional_­range<Base>; constexpr iterator& operator+=(difference_type x) requires random_­access_­range<Base>; constexpr iterator& operator-=(difference_type x) requires random_­access_­range<Base>; constexpr decltype(auto) operator[](difference_type n) const requires random_­access_­range<Base> { return get-element(current_ + n); } friend constexpr bool operator==(const iterator& x, const iterator& y) requires equality_­comparable<iterator_t<Base>>; friend constexpr bool operator<(const iterator& x, const iterator& y) requires random_­access_­range<Base>; friend constexpr bool operator>(const iterator& x, const iterator& y) requires random_­access_­range<Base>; friend constexpr bool operator<=(const iterator& x, const iterator& y) requires random_­access_­range<Base>; friend constexpr bool operator>=(const iterator& x, const iterator& y) requires random_­access_­range<Base>; friend constexpr auto operator<=>(const iterator& x, const iterator& y) requires random_­access_­range<Base> && three_­way_­comparable<iterator_t<Base>>; friend constexpr iterator operator+(const iterator& x, difference_type y) requires random_­access_­range<Base>; friend constexpr iterator operator+(difference_type x, const iterator& y) requires random_­access_­range<Base>; friend constexpr iterator operator-(const iterator& x, difference_type y) requires random_­access_­range<Base>; friend constexpr difference_type operator-(const iterator& x, const iterator& y) requires sized_­sentinel_­for<iterator_t<Base>, iterator_t<Base>>; }; }
The member typedef-name iterator_­concept is defined as follows:
The member typedef-name iterator_­category is defined if and only if Base models forward_­range.
In that case, iterator_­category is defined as follows: Let C denote the type iterator_­traits<iterator_­t<Base>>​::​iterator_­category.
  • If get<N>(*current_­) is an rvalue, iterator_­category denotes input_­iterator_­tag.
  • Otherwise, if C models derived_­from<random_­access_­iterator_­tag>, iterator_­category denotes random_­access_­iterator_­tag.
  • Otherwise, iterator_­category denotes C.
static constexpr decltype(auto) get-element(const iterator_t<Base>& i); // exposition only
Effects: Equivalent to: if constexpr (is_reference_v<range_reference_t<Base>>) { return get<N>(*i); } else { using E = remove_cv_t<tuple_element_t<N, range_reference_t<Base>>>; return static_cast<E>(get<N>(*i)); }
constexpr explicit iterator(iterator_t<Base> current);
Effects: Initializes current_­ with std​::​move(current).
constexpr iterator(iterator<!Const> i) requires Const && convertible_­to<iterator_t<V>, iterator_t<Base>>;
Effects: Initializes current_­ with std​::​move(i.current_­).
constexpr iterator_t<Base> base() const& requires copyable<iterator_t<Base>>;
Effects: Equivalent to: return current_­;
constexpr iterator_t<Base> base() &&;
Effects: Equivalent to: return std​::​move(current_­);
constexpr iterator& operator++();
Effects: Equivalent to: ++current_; return *this;
constexpr void operator++(int);
Effects: Equivalent to: ++current_­.
constexpr iterator operator++(int) requires forward_­range<Base>;
Effects: Equivalent to: auto temp = *this; ++current_; return temp;
constexpr iterator& operator--() requires bidirectional_­range<Base>;
Effects: Equivalent to: --current_; return *this;
constexpr iterator operator--(int) requires bidirectional_­range<Base>;
Effects: Equivalent to: auto temp = *this; --current_; return temp;
constexpr iterator& operator+=(difference_type n); requires random_­access_­range<Base>;
Effects: Equivalent to: current_ += n; return *this;
constexpr iterator& operator-=(difference_type n) requires random_­access_­range<Base>;
Effects: Equivalent to: current_ -= n; return *this;
friend constexpr bool operator==(const iterator& x, const iterator& y) requires equality_­comparable<Base>;
Effects: Equivalent to: return x.current_­ == y.current_­;
friend constexpr bool operator<(const iterator& x, const iterator& y) requires random_­access_­range<Base>;
Effects: Equivalent to: return x.current_­ < y.current_­;
friend constexpr bool operator>(const iterator& x, const iterator& y) requires random_­access_­range<Base>;
Effects: Equivalent to: return y < x;
friend constexpr bool operator<=(const iterator& x, const iterator& y) requires random_­access_­range<Base>;
Effects: Equivalent to: return !(y < x);
friend constexpr bool operator>=(const iterator& x, const iterator& y) requires random_­access_­range<Base>;
Effects: Equivalent to: return !(x < y);
friend constexpr auto operator<=>(const iterator& x, const iterator& y) requires random_­access_­range<Base> && three_­way_­comparable<iterator_t<Base>>;
Effects: Equivalent to: return x.current_­ <=> y.current_­;
friend constexpr iterator operator+(const iterator& x, difference_type y) requires random_­access_­range<Base>;
Effects: Equivalent to: return iterator{x} += y;
friend constexpr iterator operator+(difference_type x, const iterator& y) requires random_­access_­range<Base>;
Effects: Equivalent to: return y + x;
friend constexpr iterator operator-(const iterator& x, difference_type y) requires random_­access_­range<Base>;
Effects: Equivalent to: return iterator{x} -= y;
friend constexpr difference_type operator-(const iterator& x, const iterator& y) requires sized_­sentinel_­for<iterator_t<Base>, iterator_t<Base>>;
Effects: Equivalent to: return x.current_­ - y.current_­;

24.7.16.4 Class template elements_­view​::​sentinel [range.elements.sentinel]

namespace std::ranges { template<input_­range V, size_t N> requires view<V> && has-tuple-element<range_value_t<V>, N> && has-tuple-element<remove_reference_t<range_reference_t<V>>, N> && returnable-element<range_reference_t<V>, N> template<bool Const> class elements_view<V, N>::sentinel { // exposition only private: using Base = maybe-const<Const, V>; // exposition only sentinel_t<Base> end_ = sentinel_t<Base>(); // exposition only public: sentinel() = default; constexpr explicit sentinel(sentinel_t<Base> end); constexpr sentinel(sentinel<!Const> other) requires Const && convertible_­to<sentinel_t<V>, sentinel_t<Base>>; constexpr sentinel_t<Base> base() const; template<bool OtherConst> requires sentinel_­for<sentinel_t<Base>, iterator_t<maybe-const<OtherConst, V>>> friend constexpr bool operator==(const iterator<OtherConst>& x, const sentinel& y); template<bool OtherConst> requires sized_­sentinel_­for<sentinel_t<Base>, iterator_t<maybe-const<OtherConst, V>>> friend constexpr range_difference_t<Base> operator-(const iterator<OtherConst>& x, const sentinel& y); template<bool OtherConst> requires sized_­sentinel_­for<sentinel_t<Base>, iterator_t<maybe-const<OtherConst, V>>> friend constexpr range_difference_t<maybe-const<OtherConst, V>> operator-(const sentinel& x, const iterator<OtherConst>& y); }; }
constexpr explicit sentinel(sentinel_t<Base> end);
Effects: Initializes end_­ with end.
constexpr sentinel(sentinel<!Const> other) requires Const && convertible_­to<sentinel_t<V>, sentinel_t<Base>>;
Effects: Initializes end_­ with std​::​move(other.end_­).
constexpr sentinel_t<Base> base() const;
Effects: Equivalent to: return end_­;
template<bool OtherConst> requires sentinel_­for<sentinel_t<Base>, iterator_t<maybe-const<OtherConst, V>>> friend constexpr bool operator==(const iterator<OtherConst>& x, const sentinel& y);
Effects: Equivalent to: return x.current_­ == y.end_­;
template<bool OtherConst> requires sized_­sentinel_­for<sentinel_t<Base>, iterator_t<maybe-const<OtherConst, V>>> friend constexpr range_difference_t<Base> operator-(const iterator<OtherConst>& x, const sentinel& y);
Effects: Equivalent to: return x.current_­ - y.end_­;
template<bool OtherConst> requires sized_­sentinel_­for<sentinel_t<Base>, iterator_t<maybe-const<OtherConst, V>>> friend constexpr range_difference_t<maybe-const<OtherConst, V>> operator-(const sentinel& x, const iterator<OtherConst>& y);
Effects: Equivalent to: return x.end_­ - y.current_­;