[range.take.while] (original) (raw)

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]

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)).

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

[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<int>(input) | views::take_while(small);for (const auto i : small_ints) { cout << i << ' '; } auto i = 0; input >> i; cout << i; — _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<V> && is_object_v<Pred> && indirect_unary_predicate<const Pred, iterator_t<V>> class take_while_view : public view_interface<take_while_view<V, Pred>> { template<bool> class sentinel; V base_ = V(); movable-box<Pred> pred_; public: take_while_view() requires default_initializable<V> && default_initializable<Pred> = default;constexpr explicit take_while_view(V base, Pred pred);constexpr V base() const & requires copy_constructible<V> { return base_; } constexpr V base() && { return std::move(base_); } constexpr const Pred& pred() const;constexpr auto begin() requires (_simple-view_<V>) { return ranges::begin(base_); } constexpr auto begin() const requires range<const V> && indirect_unary_predicate<const Pred, iterator_t<const V>> { return ranges::begin(base_); } constexpr auto end() requires (_simple-view_<V>) { return sentinel<false>(ranges::end(base_), addressof(*pred_)); } constexpr auto end() const requires range<const V> && indirect_unary_predicate<const Pred, iterator_t<const V>> { return sentinel<true>(ranges::end(base_), addressof(*pred_)); } };template<class R, class Pred> take_while_view(R&&, Pred) -> take_while_view<views::all_t<R>, Pred>;}

constexpr explicit take_while_view(V base, Pred pred);

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

constexpr const Pred& pred() const;

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<V> && is_object_v<Pred> && indirect_unary_predicate<const Pred, iterator_t<V>> template<bool Const> class take_while_view<V, Pred>::sentinel { using Base = maybe-const<Const, V>; sentinel_t<_Base_> end_ = sentinel_t<_Base_>(); const Pred* pred_ = nullptr; public: sentinel() = default;constexpr explicit sentinel(sentinel_t<_Base_> end, const Pred* pred);constexpr sentinel(sentinel<!Const> s) requires Const && convertible_to<sentinel_t<V>, sentinel_t<_Base_>>;constexpr sentinel_t<_Base_> base() const { return end_; } friend constexpr bool operator==(const iterator_t<_Base_>& x, const sentinel& y);template<bool OtherConst = !Const> requires 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);

Effects: Initializes end_ with end and pred_ with pred.

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

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

friend constexpr bool operator==(const iterator_t<_Base_>& x, const _sentinel_& y);template<bool OtherConst = !Const> requires [sentinel_for](iterator.concept.sentinel#concept:sentinel%5Ffor "24.3.4.7 Concept sentinel_­for [iterator.concept.sentinel]")<sentinel_t<_Base_>, iterator_t<_maybe-const_<OtherConst, V>>> friend constexpr bool operator==(const iterator_t<_maybe-const_<OtherConst, V>>& x,const _sentinel_& y);

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