std::ranges::views::common, std::ranges::common_view - cppreference.com (original) (raw)
| Defined in header | ||
|---|---|---|
| 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) |
- Adapts a given view with different types for iterator/sentinel pair into a view that is also a common_range. A
common_viewalways has the same iterator/sentinel type.
Contents
- 1 Data members
- 2 Member functions
- 3 Deduction guides
- 4 Helper templates
- 5 Notes
- 6 Example
- 7 Defect reports
- 8 See also
[edit] Data members
| Member | Description |
|---|---|
| V base_ (private) | the underlying view(exposition-only member object*) |
[edit] Member functions
| (constructor) | constructs a common_view (public member function) [edit] |
|---|---|
| base | returns a copy of the underlying (adapted) view (public member function) [edit] |
| begin | returns an iterator to the beginning (public member function) [edit] |
| end | returns an iterator to the end (public member function) [edit] |
| size | returns the number of elements, provided only if the underlying (adapted) range satisfies sized_range (public member function) [edit] |
| reserve_hint(C++26) | returns the approximate size of the resulting approximately_sized_range (public member function) [edit] |
| Inherited from std::ranges::view_interface | |
| empty | 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) [edit] |
| cbegin(C++23) | returns a constant iterator to the beginning of the range (public member function of std::ranges::view_interface) [edit] |
| cend(C++23) | returns a sentinel for the constant iterator of the range (public member function of std::ranges::view_interface) [edit] |
| operator bool | 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) [edit] |
| data | 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) [edit] |
| front | returns the first element in the derived view, provided if it satisfies forward_range (public member function of std::ranges::view_interface) [edit] |
| back | 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) [edit] |
| operator[] | returns the nth element in the derived view, provided only if it satisfies random_access_range (public member function of std::ranges::view_interface) [edit] |
[edit] Deduction guides
[edit] Helper templates
This specialization of ranges::enable_borrowed_range makes common_view satisfy borrowed_range when the underlying view satisfies it.
[edit] Notes
common_view can be useful for working with legacy algorithms that expect the iterator and sentinel are of the same type.
[edit] Example
#include #include #include #include #include #include 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) << '\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" required auto 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: 15 c1.front(): 1 c1.back(): 5 c1.data(): 0x7f19937f00d0 c1[0]: 1 accumulate: 15 c2.front(): 1
[edit] 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 3494 | C++20 | common_view was never a borrowed_range | it is a borrowed_range if its underlying view is |