Scroll to navigation

std::ranges::subrange(3) C++ Standard Libary std::ranges::subrange(3)

NAME

std::ranges::subrange - std::ranges::subrange

Synopsis

Defined in header <ranges> template<

std::input_or_output_iterator I, std::sentinel_for<I> S = I, ranges::subrange_kind K = std::sized_sentinel_for<S, I> ? ranges::subrange_kind::sized : ranges::subrange_kind::unsized (1) (since C++20) > requires (K == ranges::subrange_kind::sized || !std::sized_sentinel_for<S, I>) class subrange

: public ranges::view_interface<subrange<I, S, K>> Helper concepts template<class From, class To>

concept /*uses-nonqualification-pointer-conversion*/ = (2) (exposition only*)

/* see description */; template<class From, class To> (3) (exposition only*) concept /*convertible-to-non-slicing*/ = /* see description */;

1) The subrange class template combines together an iterator and a sentinel into a single view. It models sized_range whenever the final template parameter is subrange_kind::sized (which happens when std::sized_sentinel_for<S, I> is satisfied or when size is passed explicitly as a constructor argument). 2) Determines whether From is convertible to To without qualification conversions. Equivalent to:

template<class From, class To> concept /*uses-nonqualification-pointer-conversion*/ =
std::is_pointer_v<From> && std::is_pointer_v<To> &&
!std::convertible_to<std::remove_pointer_t<From>(*)[],
std::remove_pointer_t<To>(*)[]>;

3) Determines whether From is convertible to To without derived-to-base conversion:

template<class From, class To> concept /*convertible-to-non-slicing*/ =
std::convertible_to<From, To> &&
!/*uses-nonqualification-pointer-conversion*/
<std::decay_t<From>, std::decay_t<To>>;


Data members

Member Definition
K == ranges::subrange_kind::sized
&& constexpr bool StoreSize [static] !std::sized_sentinel_for<S, I>
(exposition-only static member
constant*)
an iterator to the beginning of I begin_ the subrange
(exposition-only member object*)
a sentinel denoting the end of the S end_ subrange
(exposition-only member object*) make-unsigned-like-t <std::iter_difference_t<I>> size_ the size of the subrange (present only if StoreSize is true) (exposition-only member object*)

Member functions

constructor creates a new subrange
(public member function) operator PairLike converts the subrange to a pair-like type
(public member function)

Observers

begin obtains the iterator
(public member function) end obtains the sentinel
(public member function) empty checks whether the subrange is empty
(public member function) size obtains the size of the subrange
(public member function)

Iterator operations

advance advances the iterator by given distance
(public member function)
obtains a copy of the subrange with its iterator decremented by a given prev distance
(public member function)
obtains a copy of the subrange with its iterator advanced by a given next distance
(public member function)
Inherited from std::ranges::view_interface cbegin returns a constant iterator to the beginning of the range. (C++23) (public member function of std::ranges::view_interface<D>) cend returns a sentinel for the constant iterator of the range. (C++23) (public member function of std::ranges::view_interface<D>)
returns whether the derived view is not empty. Provided if ranges::empty operator bool is applicable to it.
(public member function of std::ranges::view_interface<D>)
gets the address of derived view's data. Provided if its iterator type data satisfies contiguous_iterator.
(public member function of std::ranges::view_interface<D>)
returns the first element in the derived view. Provided if it satisfies front forward_range.
(public member function of std::ranges::view_interface<D>)
returns the last element in the derived view. Provided if it satisfies back bidirectional_range and common_range.
(public member function of std::ranges::view_interface<D>)
returns the n^th element in the derived view. Provided if it satisfies operator[] random_access_range.
(public member function of std::ranges::view_interface<D>)


Deduction guides

Non-member functions

get(std::ranges::subrange) obtains iterator or sentinel from a std::ranges::subrange (C++20) (function template)

Helper types

ranges::subrange_kind specifies whether a std::ranges::subrange models (C++20) std::ranges::sized_range
(enum) std::tuple_size<std::ranges::subrange> obtains the size of a std::ranges::subrange (C++20) (class template specialization) std::tuple_element<std::ranges::subrange> obtains the type of the iterator or the sentinel (C++20) of a std::ranges::subrange
(class template specialization)


Helper templates

template< class I, class S, ranges::subrange_kind K > constexpr bool ranges::enable_borrowed_range<ranges::subrange<I, S, K>> = (since C++20) true;

This specialization of ranges::enable_borrowed_range makes subrange satisfy borrowed_range.

Example

// Run this code

#include <map> #include <print> #include <ranges>

void make_uppercase(char& v) {
v += 'A' - 'a'; }

void uppercase_transform(std::multimap<int, char>& m, int k) {
auto [first, last] = m.equal_range(k);
for (auto& [_, v] : std::ranges::subrange(first, last))
make_uppercase(v); }

int main() {
std::multimap<int, char> mm{{4, 'a'}, {3, '-'}, {4, 'b'}, {5, '-'}, {4, 'c'}};
std::println("Before: {}", mm);
uppercase_transform(mm, 4);
std::println("After: {}", mm); }

Output:

Before: {3: '-', 4: 'a', 4: 'b', 4: 'c', 5: '-'} After: {3: '-', 4: 'A', 4: 'B', 4: 'C', 5: '-'}


Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.


DR Applied to Behavior as published Correct behavior LWG 3470 C++20 convertible-to-non-slicing might reject always accepts
qualification conversions them

See also

ranges::view_interface helper class template for defining a view, using the curiously (C++20) recurring template pattern
(class template)

External links

Read/write all values of a std::multimap with a given key in C++20 — SO

2026.11.10 http://cppreference.com