From cppreference.com
Defined in header
template<std::input_or_output_iteratorI,std::sentinel_for<I>S=I,ranges::subrange_kindK=std::sized_sentinel_for<S,I>?ranges::subrange_kind::sized:ranges::subrange_kind::unsized>requires(K==ranges::subrange_kind::sized||!std::sized_sentinel_for<S,I>)classsubrange:publicranges::view_interface<subrange<I,S,K>> (1) (since C++20)Helper concepts
template<classFrom,classTo>concept/*uses-nonqualification-pointer-conversion*/=/* see description */; (2)(exposition only*)template<classFrom,classTo>concept/*convertible-to-non-slicing*/=/* see description */; (3)(exposition only*)1) The subrange class template combines together an iterator and a sentinel into a single
. It models
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
. Equivalent to:
template<classFrom,classTo>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<classFrom,classTo>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 Description constexprboolStoreSize [static]K==ranges::subrange_kind::sized&&
!std::sized_sentinel_for<S,I>
(exposition-only static member constant*)Ibegin_an iterator to the beginning of the subrange
(exposition-only member object*)Send_a sentinel denoting the end of the subrange
(exposition-only member object*)
<std::iter_difference_t<I>>size_
(present only if StoreSize is true)the size of the subrange
(exposition-only member object*)Member functions
creates a new subrange
(public member function)
converts the subrange to a
type
(public member function)
Observers
obtains the iterator
(public member function)
obtains the sentinel
(public member function)
checks whether the subrange is empty
(public member function)
obtains the size of the subrange
(public member function)
Iterator operations
advances the iterator by given distance
(public member function)
obtains a copy of the subrange with its iterator decremented by a given distance
(public member function)
obtains a copy of the subrange with its iterator advanced by a given distance
(public member function)
Inherited from
(C++23)
returns a constant iterator to the beginning of the range
(public member function of std::ranges::view_interface<D>)
(C++23)
returns a sentinel for the constant iterator of the range
(public member function of std::ranges::view_interface<D>)
returns whether the derived view is not empty, provided only if
is applicable to it
(public member function of std::ranges::view_interface<D>)
gets the address of derived view's data, provided only if its iterator type satisfies
(public member function of std::ranges::view_interface<D>)
returns the first element in the derived view, provided if it satisfies
(public member function of std::ranges::view_interface<D>)
returns the last element in the derived view, provided only if it satisfies
and
(public member function of std::ranges::view_interface<D>)
returns the nth element in the derived view, provided only if it satisfies
(public member function of std::ranges::view_interface<D>)
Non-member functions
Helper types
Helper templates
template<classI,classS,ranges::subrange_kindK>constexprboolranges::enable_borrowed_range<ranges::subrange<I,S,K>>=true;(since C++20)This specialization of ranges::enable_borrowed_range makes subrange satisfy
.
Example
Run this code
#include<map>#include<print>#include<ranges>voidmake_uppercase(char&v){v+='A'-'a';}voiduppercase_transform(std::multimap<int,char>&m,intk){auto[first,last]=m.equal_range(k);for(auto&[_,v]:std::ranges::subrange(first,last))make_uppercase(v);}intmain(){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
C++20 convertible-to-non-slicing might reject qualification conversions always accepts them See also
External links