3.8 KiB
[range.take.overview]
25 Ranges library [ranges]
25.7 Range adaptors [range.adaptors]
25.7.10 Take view [range.take]
25.7.10.1 Overview [range.take.overview]
take_view produces a view of the first N elements from another view, or all the elements if the adapted view contains fewer than N.
The name views::take denotes a range adaptor object ([range.adaptor.object]).
Let E and F be expressions, let T be remove_cvref_t<decltype((E))>, and let D be range_difference_t<decltype((E))>.
If decltype((F)) does not modelconvertible_to,views::take(E, F) is ill-formed.
Otherwise, the expression views::take(E, F) is expression-equivalent to:
-
If T is a specialization of empty_view ([range.empty.view]), then ((void)F, decay-copy(E)), except that the evaluations of E and F are indeterminately sequenced.
-
Otherwise, if T modelsrandom_access_range and sized_range and is a specialization ofspan ([views.span]),basic_string_view ([string.view]), orsubrange ([range.subrange]), thenU(ranges::begin(E), ranges::begin(E) + std::min(ranges::distance(E), F)), except that E is evaluated only once, where U is a type determined as follows:
if T is a specialization of span, then U is span<typename T::element_type>;
otherwise, if T is a specialization of basic_string_view, then U is T;
otherwise, T is a specialization of subrange, andU is subrange<iterator_t>;
-
otherwise, if T is a specialization of iota_view ([range.iota.view]) that models random_access_range and sized_range, theniota_view(ranges::begin(E),(ranges::begin(E) + std::min(ranges::distance(E), F))), except that E is evaluated only once.
-
Otherwise, if T is a specialization of repeat_view ([range.repeat.view]):
if T models sized_range, thenviews::repeat(*E.value_, std::min(ranges::distance(E), F)) except that E is evaluated only once;
otherwise, views::repeat(*E.value_, static_cast(F)).
[Example 1: vector is{0,1,2,3,4,5,6,7,8,9};for (int i : is | views::take(5)) cout << i << ' '; // prints 0 1 2 3 4 â end example]