[range.take.while]

# 25 Ranges library [[ranges]](./#ranges)

## 25.7 Range adaptors [[range.adaptors]](range.adaptors#range.take.while)

### 25.7.11 Take while view [range.take.while]

#### [25.7.11.1](#overview) Overview [[range.take.while.overview]](range.take.while.overview)

[1](#overview-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L6166)

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))[.](#overview-1.sentence-1)

[2](#overview-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L6172)

The name views​::​take_while denotes
a range adaptor object ([[range.adaptor.object]](range.adaptor.object "25.7.2 Range adaptor objects"))[.](#overview-2.sentence-1)

Given subexpressions E and F,
the expression views​::​take_while(E, F) is expression-equivalent to take_while_view(E, F)[.](#overview-2.sentence-2)

[3](#overview-3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L6180)

[*Example [1](#overview-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 << ' '; // prints 0 1 2 3 4}auto i = 0;
input >> i;
cout << i; // prints 6 â *end example*]

#### [25.7.11.2](#view) Class template take_while_view [[range.take.while.view]](range.take.while.view)

[ð](#lib:take_while_view)

namespace std::ranges {template<[view](range.view#concept:view "25.4.5 Views [range.view]") V, class Pred>requires [input_range](range.refinements#concept:input_range "25.4.6 Other range refinements [range.refinements]")<V> && is_object_v<Pred> &&[indirect_unary_predicate](indirectcallable.indirectinvocable#concept:indirect_unary_predicate "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<const Pred, iterator_t<V>>class take_while_view : public view_interface<take_while_view<V, Pred>> {// [[range.take.while.sentinel]](#sentinel "25.7.11.3 Class template take_­while_­view​::​sentinel"), class template take_while_view​::​*sentinel*template<bool> class *sentinel*; // *exposition only* V *base_* = V(); // *exposition only**movable-box*<Pred> *pred_*; // *exposition only*public: take_while_view() requires [default_initializable](concept.default.init#concept:default_initializable "18.4.12 Concept default_­initializable [concept.default.init]")<V> && [default_initializable](concept.default.init#concept:default_initializable "18.4.12 Concept default_­initializable [concept.default.init]")<Pred> = default; constexpr explicit take_while_view(V base, Pred pred); constexpr V base() const & requires [copy_constructible](concept.copyconstructible#concept:copy_constructible "18.4.14 Concept copy_­constructible [concept.copyconstructible]")<V> { return *base_*; }constexpr V base() && { return std::move(*base_*); }constexpr const Pred& pred() const; constexpr auto begin() requires (![*simple-view*](range.utility.helpers#concept:simple-view "25.5.2 Helper concepts [range.utility.helpers]")<V>){ return ranges::begin(*base_*); }constexpr auto begin() constrequires [range](range.range#concept:range "25.4.2 Ranges [range.range]")<const V> &&[indirect_unary_predicate](indirectcallable.indirectinvocable#concept:indirect_unary_predicate "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<const Pred, iterator_t<const V>>{ return ranges::begin(*base_*); }constexpr auto end() requires (![*simple-view*](range.utility.helpers#concept:simple-view "25.5.2 Helper concepts [range.utility.helpers]")<V>){ return *sentinel*<false>(ranges::end(*base_*), addressof(**pred_*)); }constexpr auto end() constrequires [range](range.range#concept:range "25.4.2 Ranges [range.range]")<const V> &&[indirect_unary_predicate](indirectcallable.indirectinvocable#concept:indirect_unary_predicate "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<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>;}

[ð](#lib:take_while_view,constructor)

`constexpr explicit take_while_view(V base, Pred pred);
`

[1](#view-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L6250)

*Effects*: Initializes *base_* with std​::​move(base) and*pred_* with std​::​move(pred)[.](#view-1.sentence-1)

[ð](#lib:pred,take_while_view)

`constexpr const Pred& pred() const;
`

[2](#view-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L6262)

*Effects*: Equivalent to: return **pred_*;

#### [25.7.11.3](#sentinel) Class template take_while_view​::​*sentinel* [[range.take.while.sentinel]](range.take.while.sentinel)

[ð](#lib:take_while_view::sentinel)

namespace std::ranges {template<[view](range.view#concept:view "25.4.5 Views [range.view]") V, class Pred>requires [input_range](range.refinements#concept:input_range "25.4.6 Other range refinements [range.refinements]")<V> && is_object_v<Pred> &&[indirect_unary_predicate](indirectcallable.indirectinvocable#concept:indirect_unary_predicate "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<const Pred, iterator_t<V>>template<bool Const>class take_while_view<V, Pred>::*sentinel* {using *Base* = *maybe-const*<Const, V>; // *exposition only* sentinel_t<*Base*> *end_* = sentinel_t<*Base*>(); // *exposition only*const Pred* *pred_* = nullptr; // *exposition only*public:*sentinel*() = default; constexpr explicit *sentinel*(sentinel_t<*Base*> end, const Pred* pred); constexpr *sentinel*(*sentinel*<!Const> s)requires Const && [convertible_to](concept.convertible#concept:convertible_to "18.4.4 Concept convertible_­to [concept.convertible]")<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](iterator.concept.sentinel#concept:sentinel_for "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); };}

[ð](#lib:take_while_view::sentinel,constructor)

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

[1](#sentinel-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L6306)

*Effects*: Initializes *end_* with end and *pred_* with pred[.](#sentinel-1.sentence-1)

[ð](#lib:take_while_view::sentinel,constructor_)

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

[2](#sentinel-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L6318)

*Effects*: Initializes *end_* with std​::​move(s.*end_*) and*pred_* with s.*pred_*[.](#sentinel-2.sentence-1)

[ð](#lib:operator==,take_while_view::sentinel)

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

template<bool OtherConst = !Const>
  requires [sentinel_for](iterator.concept.sentinel#concept:sentinel_for "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);
`

[3](#sentinel-3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L6335)

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