Files
2025-10-25 03:02:53 +03:00

92 lines
3.8 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[range.take.overview]
# 25 Ranges library [[ranges]](./#ranges)
## 25.7 Range adaptors [[range.adaptors]](range.adaptors#range.take.overview)
### 25.7.10 Take view [[range.take]](range.take#overview)
#### 25.7.10.1 Overview [range.take.overview]
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L5864)
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[.](#1.sentence-1)
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L5869)
The name views::take denotes a
range adaptor object ([[range.adaptor.object]](range.adaptor.object "25.7.2Range adaptor objects"))[.](#2.sentence-1)
Let E and F be expressions,
let T be remove_cvref_t<decltype((E))>, and
let D be range_difference_t<decltype((E))>[.](#2.sentence-2)
If decltype((F)) does not model[convertible_to](concept.convertible#concept:convertible_to "18.4.4Concept convertible_­to[concept.convertible]")<D>,views::take(E, F) is ill-formed[.](#2.sentence-3)
Otherwise, the expression views::take(E, F) is expression-equivalent to:
- [(2.1)](#2.1)
If T is a specialization
of empty_view ([[range.empty.view]](range.empty.view "25.6.2.2Class template empty_­view")),
then ((void)F, *decay-copy*(E)),
except that the evaluations of E and F are indeterminately sequenced[.](#2.1.sentence-1)
- [(2.2)](#2.2)
Otherwise, if T models[random_access_range](range.refinements#concept:random_access_range "25.4.6Other range refinements[range.refinements]") and [sized_range](range.sized#concept:sized_range "25.4.4Sized ranges[range.sized]") and is a specialization ofspan ([[views.span]](views.span "23.7.2.2Class template span")),basic_string_view ([[string.view]](string.view "27.3String view classes")), orsubrange ([[range.subrange]](range.subrange "25.5.4Sub-ranges")),
thenU(ranges::begin(E),
ranges::begin(E) + std::min<D>(ranges::distance(E), F)),
except that E is evaluated only once,
where U is a type determined as follows:
* [(2.2.1)](#2.2.1)
if T is a specialization of span,
then U is span<typename T::element_type>;
* [(2.2.2)](#2.2.2)
otherwise, if T is a specialization of basic_string_view,
then U is T;
* [(2.2.3)](#2.2.3)
otherwise, T is a specialization of subrange, andU is subrange<iterator_t<T>>;
- [(2.3)](#2.3)
otherwise, if T is
a specialization of iota_view ([[range.iota.view]](range.iota.view "25.6.4.2Class template iota_­view"))
that models [random_access_range](range.refinements#concept:random_access_range "25.4.6Other range refinements[range.refinements]") and [sized_range](range.sized#concept:sized_range "25.4.4Sized ranges[range.sized]"),
theniota_view(*ranges::begin(E),*(ranges::begin(E) + std::min<D>(ranges::distance(E), F))),
except that E is evaluated only once[.](#2.3.sentence-1)
- [(2.4)](#2.4)
Otherwise, if T is
a specialization of repeat_view ([[range.repeat.view]](range.repeat.view "25.6.5.2Class template repeat_­view")):
* [(2.4.1)](#2.4.1)
if T models [sized_range](range.sized#concept:sized_range "25.4.4Sized ranges[range.sized]"),
thenviews::repeat(*E.*value_*, std::min<D>(ranges::distance(E), F)) except that E is evaluated only once;
* [(2.4.2)](#2.4.2)
otherwise, views::repeat(*E.*value_*, static_cast<D>(F))[.](#2.4.sentence-1)
- [(2.5)](#2.5)
Otherwise, take_view(E, F)[.](#2.5.sentence-1)
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L5938)
[*Example [1](#example-1)*: vector<int> 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*]