std::ranges::views::common, std::ranges::common_view
From cppreference.com
| Defined in header <ranges>
|
||
template< ranges::view V >
requires (not ranges::common_range<V> and
std::copyable<ranges::iterator_t<V>>)
class common_view
: public ranges::view_interface<common_view<V>>
|
(1) | (since C++20) |
namespace views {
inline constexpr /* unspecified */ common = /* unspecified */;
}
|
(2) | (since C++20) |
| Call signature |
||
template< ranges::viewable_range R >
requires /* see below */
constexpr ranges::view auto common( R&& r );
|
(since C++20) | |
1) Adapts a given view with different types for iterator/sentinel pair into a view that is also a common_range. A
common_view always has the same iterator/sentinel type.2) RangeAdaptorObject. Let
e be a subexpression. Then the expression views::common(e) is expression-equivalent to:
views::all(e), if it is a well-formed expression anddecltype((e))models common_range;common_view{e}otherwise.
Data members
| Member | Description |
V base_ (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 sized_range (public member function) | |
(C++26) |
returns the approximate size of the resulting approximately_sized_range (public member function) |
Inherited from ranges::view_interface | |
| returns whether the derived view is empty, provided only if it satisfies sized_range or forward_range (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 ranges::empty 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 contiguous_iterator (public member function of std::ranges::view_interface<D>)
| |
| returns the first element in the derived view, provided if it satisfies forward_range (public member function of std::ranges::view_interface<D>)
| |
| returns the last element in the derived view, provided only if it satisfies bidirectional_range and common_range (public member function of std::ranges::view_interface<D>)
| |
returns the nth element in the derived view, provided only if it satisfies random_access_range (public member function of std::ranges::view_interface<D>)
| |
Deduction guides
Helper templates
template< class T >
constexpr bool enable_borrowed_range<std::ranges::common_view<T>> =
ranges::enable_borrowed_range<T>;
|
(since C++20) | |
This specialization of ranges::enable_borrowed_range makes common_view satisfy borrowed_range 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>
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" required
auto c1 = std::ranges::common_view{r1};
std::cout << "accumulate: " << std::accumulate(c1.begin(), c1.end(), 0)