|
Range primitives | |||||||
|
Range concepts | |||||||||||||||||||
|
Range factories | |||||||||
|
Range adaptors | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
Helper items | |||||||||||||||||
|
Defined in header <ranges> | ||
template<ranges::view V> requires(notranges::common_range<V> and | (1) | (since C++20) |
namespace views{ inlineconstexpr/* unspecified */ common=/* unspecified */; | (2) | (since C++20) |
Call signature | ||
template<ranges::viewable_range R> requires/* see below */ | (since C++20) | |
view
with different types for iterator/sentinel pair into aview
that is also acommon_range
. Acommon_view
always has the same iterator/sentinel type.common_range
;Contents |
Member | Description |
V base_ (private) | the underlying view (exposition-only member object*) |
constructs acommon_view (public member function)[edit] | |
returns a copy of the underlying (adapted) view (public member function)[edit] | |
returns an iterator to the beginning (public member function)[edit] | |
returns an iterator to the end (public member function)[edit] | |
returns the number of elements, provided only if the underlying (adapted) range satisfiessized_range (public member function)[edit] | |
(C++26) | returns the approximate size of the resultingapproximately_sized_range (public member function)[edit] |
Inherited fromstd::ranges::view_interface | |
returns whether the derived view is empty, provided only if it satisfiessized_range orforward_range (public member function of std::ranges::view_interface<D> )[edit] | |
(C++23) | returns a constant iterator to the beginning of the range (public member function of std::ranges::view_interface<D> )[edit] |
(C++23) | returns a sentinel for the constant iterator of the range (public member function of std::ranges::view_interface<D> )[edit] |
returns whether the derived view is not empty, provided only ifranges::empty is applicable to it (public member function of std::ranges::view_interface<D> )[edit] | |
gets the address of derived view's data, provided only if its iterator type satisfiescontiguous_iterator (public member function of std::ranges::view_interface<D> )[edit] | |
returns the first element in the derived view, provided if it satisfiesforward_range (public member function of std::ranges::view_interface<D> )[edit] | |
returns the last element in the derived view, provided only if it satisfiesbidirectional_range andcommon_range (public member function of std::ranges::view_interface<D> )[edit] | |
returns then th element in the derived view, provided only if it satisfiesrandom_access_range (public member function of std::ranges::view_interface<D> )[edit] |
template<class T> constexprbool enable_borrowed_range<std::ranges::common_view<T>>= | (since C++20) | |
This specialization ofranges::enable_borrowed_range makescommon_view
satisfyborrowed_range
when the underlying view satisfies it.
common_view
can be useful for working with legacy algorithms that expect the iterator and sentinel are of the same type.
#include <initializer_list>#include <iostream>#include <iterator>#include <list>#include <numeric>#include <ranges> int main(){auto v1={1,2,3,4,5};auto i1=std::counted_iterator{v1.begin(),std::ssize(v1)};auto r1= std::ranges::subrange{i1,std::default_sentinel};// auto e1 = std::accumulate(r1.begin(), r1.end(), 0); // error: "common range" requiredauto c1= std::ranges::common_view{r1};std::cout<<"accumulate: "<<std::accumulate(c1.begin(), c1.end(),0)<<'\n'; // inherited from ranges::view_interface:std::cout<<"c1.front(): "<< c1.front()<<'\n';std::cout<<"c1.back(): "<< c1.back()<<'\n';std::cout<<"c1.data(): "<< c1.data()<<'\n';std::cout<<"c1[0]: "<< c1[0]<<'\n'; auto v2=std::list{1,2,3,4,5};auto i2=std::counted_iterator{v2.begin(),std::ssize(v2)};auto r2= std::ranges::subrange{i2,std::default_sentinel};// auto e2 = std::accumulate(r2.begin(), r2.end(), 0); // error: "common range" requiredauto c2= std::ranges::common_view{ r2};std::cout<<"accumulate: "<<std::accumulate(c2.begin(), c2.end(),0)<<'\n'; // inherited from ranges::view_interface:std::cout<<"c2.front(): "<< c2.front()<<'\n';// auto e3 = c2.back(); // error: "bidirectional range" required// auto e4 = c2.data(); // error: "contiguous range" required// auto e5 = c2[0]; // error: "random access range" required}
Possible output:
accumulate: 15c1.front(): 1c1.back(): 5c1.data(): 0x7f19937f00d0c1[0]: 1accumulate: 15c2.front(): 1
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 3494 | C++20 | common_view was never aborrowed_range | it is aborrowed_range if its underlying view is |
(C++20) | specifies that a range has identical iterator and sentinel types (concept)[edit] |
(C++20) | adapts an iterator type and its sentinel into a common iterator type (class template)[edit] |