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

8.0 KiB
Raw Permalink Blame History

[range.take.while]

25 Ranges library [ranges]

25.7 Range adaptors [range.adaptors]

25.7.11 Take while view [range.take.while]

25.7.11.1 Overview [range.take.while.overview]

1

#

Given a unary predicate pred and a view r,take_while_view produces a view of the range [ranges::begin(r), ranges::find_if_not(r, pred)).

2

#

The name views::take_while denotes a range adaptor object ([range.adaptor.object]).

Given subexpressions E and F, the expression views::take_while(E, F) is expression-equivalent to take_while_view(E, F).

3

#

[Example 1: auto input = istringstream{"0 1 2 3 4 5 6 7 8 9"};auto small = [](const auto x) noexcept { return x < 5; };auto small_ints = views::istream(input) | views::take_while(small);for (const auto i : small_ints) { cout << i << ' '; // prints 0 1 2 3 4}auto i = 0; input >> i; cout << i; // prints 6 — end example]

25.7.11.2 Class template take_while_view [range.take.while.view]

🔗

namespace std::ranges {template<view V, class Pred>requires input_range && is_object_v &&indirect_unary_predicate<const Pred, iterator_t>class take_while_view : public view_interface<take_while_view<V, Pred>> {// [range.take.while.sentinel], class template take_while_view::sentineltemplate class sentinel; // exposition only V base_ = V(); // exposition only**movable-box pred_; // exposition onlypublic: take_while_view() requires default_initializable && default_initializable = default; constexpr explicit take_while_view(V base, Pred pred); constexpr V base() const & requires copy_constructible { return base_; }constexpr V base() && { return std::move(base_); }constexpr const Pred& pred() const; constexpr auto begin() requires (simple-view){ return ranges::begin(base_); }constexpr auto begin() constrequires range &&indirect_unary_predicate<const Pred, iterator_t>{ return ranges::begin(base_); }constexpr auto end() requires (simple-view){ return sentinel(ranges::end(base_), addressof(**pred_)); }constexpr auto end() constrequires range &&indirect_unary_predicate<const Pred, iterator_t>{ return sentinel(ranges::end(base_), addressof(**pred_)); }}; template<class R, class Pred> take_while_view(R&&, Pred) -> take_while_view<views::all_t, Pred>;}

🔗

constexpr explicit take_while_view(V base, Pred pred);

1

#

Effects: Initializes base_ with std::move(base) andpred_ with std::move(pred).

🔗

constexpr const Pred& pred() const;

2

#

Effects: Equivalent to: return **pred_*;

25.7.11.3 Class template take_while_view::sentinel [range.take.while.sentinel]

🔗

namespace std::ranges {template<view V, class Pred>requires input_range && is_object_v &&indirect_unary_predicate<const Pred, iterator_t>templateclass take_while_view<V, Pred>::sentinel {using Base = maybe-const<Const, V>; // exposition only sentinel_t<Base> end_ = sentinel_t<Base>(); // exposition onlyconst Pred* pred_ = nullptr; // exposition onlypublic:sentinel() = default; constexpr explicit sentinel(sentinel_t<Base> end, const Pred* pred); constexpr sentinel(sentinel s)requires Const && convertible_to<sentinel_t, sentinel_t<Base>>; constexpr sentinel_t<Base> base() const { return end_; }friend constexpr bool operator==(const iterator_t<Base>& x, const sentinel& y); templaterequires sentinel_for<sentinel_t<Base>, iterator_t<maybe-const<OtherConst, V>>>friend constexpr bool operator==(const iterator_t<maybe-const<OtherConst, V>>& x, const sentinel& y); };}

🔗

constexpr explicit sentinel(sentinel_t<Base> end, const Pred* pred);

1

#

Effects: Initializes end_ with end and pred_ with pred.

🔗

constexpr sentinel(sentinel<!Const> s) requires Const && [convertible_to](concept.convertible#concept:convertible_to "18.4.4Concept convertible_­to[concept.convertible]")<sentinel_t<V>, sentinel_t<Base>>;

2

#

Effects: Initializes end_ with std::move(s.end_) andpred_ with s.pred_.

🔗

`friend constexpr bool operator==(const iterator_t& x, const sentinel& y);

template requires sentinel_for<sentinel_t, iterator_t<maybe-const<OtherConst, V>>> friend constexpr bool operator==(const iterator_t<maybe-const<OtherConst, V>>& x, const sentinel& y); `

3

#

Effects: Equivalent to:return y.end_ == x || !invoke(*y.pred_, *x);