From cppreference.com
Defined in header
template<ranges::viewV>requires(notranges::common_range<V>andstd::copyable<ranges::iterator_t<V>>)classcommon_view:publicranges::view_interface<common_view<V>> (1) (since C++20)namespaceviews{inlineconstexpr/* unspecified */common=/* unspecified */;} (2) (since C++20)Call signature
template<ranges::viewable_rangeR>requires/* see below */constexprranges::viewautocommon(R&&r);(since C++20)1) Adapts a given
with different types for iterator/sentinel pair into a
that is also a
. A common_view always has the same iterator/sentinel type.
Data members
Member Description Vbase_(private)the underlying view
(exposition-only member object*)Member functions
constructs a common_view
(public member function)
returns a copy of the underlying (adapted) view
(public member function)
returns an iterator to the beginning
(public member function)
returns an iterator to the end
(public member function)
returns the number of elements, provided only if the underlying (adapted) range satisfies
(public member function)
(C++26)
returns the approximate size of the resulting
(public member function)
Inherited from
returns whether the derived view is empty, provided only if it satisfies
or
(public member function of std::ranges::view_interface<D>)
(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>)
Helper templates
template<classT>constexprboolenable_borrowed_range<std::ranges::common_view<T>>=ranges::enable_borrowed_range<T>;(since C++20)This specialization of
makes common_view satisfy
when the underlying view satisfies it.
Notes
common_view can be useful for working with legacy algorithms that expect the iterator and sentinel are of the same type.
Example
Run this code
#include<initializer_list>#include<iostream>#include<iterator>#include<list>#include<numeric>#include<ranges>intmain(){autov1={1,2,3,4,5};autoi1=std::counted_iterator{v1.begin(),std::ssize(v1)};autor1=std::ranges::subrange{i1,std::default_sentinel};// auto e1 = std::accumulate(r1.begin(), r1.end(), 0); // error: "common range" requiredautoc1=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';autov2=std::list{1,2,3,4,5};autoi2=std::counted_iterator{v2.begin(),std::ssize(v2)};autor2=std::ranges::subrange{i2,std::default_sentinel};// auto e2 = std::accumulate(r2.begin(), r2.end(), 0); // error: "common range" requiredautoc2=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: 15 c1.front(): 1 c1.back(): 5 c1.data(): 0x7f19937f00d0 c1[0]: 1 accumulate: 15 c2.front(): 1 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 common_view was never a borrowed_rangeit is a borrowed_range if its underlying view is See also