std::ranges::views::elements, std::ranges::elements_view
Defined in header <ranges>
|
||
template< ranges::input_range V, std::size_t N > requires |
(1) | (since C++20) |
namespace views { inline constexpr /*unspecified*/ elements = /*unspecified*/; |
(2) | (since C++20) |
Helper concept |
||
template< class T, std::size_t N > concept __HasTupleElement = // exposition only |
(3) | (since C++20) |
N
is the non-type template parameter.elements_view models the concepts random_access_range, bidirectional_range, forward_range, input_range, common_range, and sized_range when the underlying view V models respective concepts.
Contents |
[edit] Expression-equivalent
Expression e is expression-equivalent to expression f, if e and f have the same effects, either are both potentially-throwing or are both not potentially-throwing (i.e. noexcept(e) == noexcept(f)), and either are both constant subexpressions or are both not constant subexpressions.
[edit] Member functions
constructs a elements_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 or a sentinel to the end (public member function) | |
returns the number of elements. Provided only if the underlying (adapted) range satisfies sized_range (public member function) |
[edit] Deduction guides
[edit] Nested classes
the iterator type (public member class) | |
the sentinel type (public member class) |
[edit] Helper templates
template<class T, std::size_t N> inline constexpr bool enable_borrowed_range<std::ranges::elements_view<T, N>> = |
(since C++20) | |
This specialization of std::ranges::enable_borrowed_range makes elements_view
satisfy borrowed_range when the underlying view satisfies it.
[edit] Example
#include <iostream> #include <ranges> #include <tuple> #include <vector> int main() { const std::vector<std::tuple<int, char, std::string>> vt { {1, 'A', "α"}, {2, 'B', "β"}, {3, 'C', "γ"}, {4, 'D', "δ"}, {5, 'E', "ε"}, }; for (int const e: std::views::elements<0>(vt)) { std::cout << e << ' '; } std::cout << '\n'; for (char const e: std::views::elements<1>(vt)) { std::cout << e << ' '; } std::cout << '\n'; for (std::string const& e: std::views::elements<2>(vt)) { std::cout << e << ' '; } std::cout << '\n'; }
Output:
1 2 3 4 5 A B C D E α β γ δ ε
[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 | elements_view was never a borrowed_range
|
it is a borrowed_range if its underlying view is
|
[edit] See also
(C++20) |
takes a view consisting of pair-like values and produces a view of the first elements of each pair (class template) (range adaptor object) |
takes a view consisting of pair-like values and produces a view of the second elements of each pair (class template) (range adaptor object) | |
BLAS-like slice of a valarray: starting index, length, stride (class) |