[range.chunk.by]

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

## 25.7 Range adaptors [[range.adaptors]](range.adaptors#range.chunk.by)

### 25.7.31 Chunk by view [range.chunk.by]

#### [25.7.31.1](#overview) Overview [[range.chunk.by.overview]](range.chunk.by.overview)

[1](#overview-1)

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

chunk_by_view takes a view and a predicate, and
splits the view into subranges
between each pair of adjacent elements
for which the predicate returns false[.](#overview-1.sentence-1)

[2](#overview-2)

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

The name views​::​chunk_by 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​::​chunk_by(E, F) is expression-equivalent tochunk_by_view(E, F)[.](#overview-2.sentence-2)

[*Example [1](#overview-example-1)*: vector v = {1, 2, 2, 3, 0, 4, 5, 2};

for (auto r : v | views::chunk_by(ranges::less_equal{})) { cout << '['; auto sep = ""; for (auto i : r) { cout << sep << i;
 sep = ", "; } cout << "] ";}// The above prints [1, 2, 2, 3] [0, 4, 5] [2] â *end example*]

#### [25.7.31.2](#view) Class template chunk_by_view [[range.chunk.by.view]](range.chunk.by.view)

[ð](#lib:chunk_by_view)

namespace std::ranges {template<[forward_range](range.refinements#concept:forward_range "25.4.6 Other range refinements [range.refinements]") V, [indirect_binary_predicate](indirectcallable.indirectinvocable#concept:indirect_binary_predicate "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<iterator_t<V>, iterator_t<V>> Pred>requires [view](range.view#concept:view "25.4.5 Views [range.view]")<V> && is_object_v<Pred>class chunk_by_view : public view_interface<chunk_by_view<V, Pred>> { V *base_* = V(); // *exposition only**movable-box*<Pred> *pred_*; // *exposition only*// [[range.chunk.by.iter]](#iter "25.7.31.3 Class chunk_­by_­view​::​iterator"), class chunk_by_view​::​*iterator*class *iterator*; // *exposition only*public: chunk_by_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 chunk_by_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 *iterator* begin(); constexpr auto end(); constexpr iterator_t<V> *find-next*(iterator_t<V>); // *exposition only*constexpr iterator_t<V> *find-prev*(iterator_t<V>) // *exposition only*requires [bidirectional_range](range.refinements#concept:bidirectional_range "25.4.6 Other range refinements [range.refinements]")<V>; }; template<class R, class Pred> chunk_by_view(R&&, Pred) -> chunk_by_view<views::all_t<R>, Pred>;}

[ð](#lib:chunk_by_view,constructor)

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

[1](#view-1)

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

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

[ð](#lib:pred,chunk_by_view)

`constexpr const Pred& pred() const;
`

[2](#view-2)

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

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

[ð](#lib:begin,chunk_by_view)

`constexpr iterator begin();
`

[3](#view-3)

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

*Preconditions*: *pred_*.has_value() is true[.](#view-3.sentence-1)

[4](#view-4)

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

*Returns*: *iterator*(*this, ranges​::​begin(*base_*), *find-next*(ranges​::​begin(*base_*)))[.](#view-4.sentence-1)

[5](#view-5)

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

*Remarks*: In order to provide
the amortized constant-time complexity required by the [range](range.range#concept:range "25.4.2 Ranges [range.range]") concept,
this function caches the result within the chunk_by_view for use on subsequent calls[.](#view-5.sentence-1)

[ð](#lib:end,chunk_by_view)

`constexpr auto end();
`

[6](#view-6)

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

*Effects*: Equivalent to:if constexpr ([common_range](range.refinements#concept:common_range "25.4.6 Other range refinements [range.refinements]")<V>) {return *iterator*(*this, ranges::end(*base_*), ranges::end(*base_*));} else {return default_sentinel;}

[ð](#lib:find-next,chunk_by_view)

`constexpr iterator_t<V> find-next(iterator_t<V> current);
`

[7](#view-7)

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

*Preconditions*: *pred_*.has_value() is true[.](#view-7.sentence-1)

[8](#view-8)

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

*Returns*: ranges::next(ranges::adjacent_find(current, ranges::end(*base_*), not_fn(ref(**pred_*))), 1, ranges::end(*base_*))

[ð](#lib:find-prev,chunk_by_view)

`constexpr iterator_t<V> find-prev(iterator_t<V> current) requires [bidirectional_range](range.refinements#concept:bidirectional_range "25.4.6 Other range refinements [range.refinements]")<V>;
`

[9](#view-9)

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

*Preconditions*: 

- [(9.1)](#view-9.1)

  current is not equal to ranges​::​begin(*base_*)[.](#view-9.1.sentence-1)

- [(9.2)](#view-9.2)

  *pred_*.has_value() is true[.](#view-9.2.sentence-1)

[10](#view-10)

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

*Returns*: An iterator i in the range [ranges​::​begin(*base_*), current) such that:

- [(10.1)](#view-10.1)

ranges​::​adjacent_find(i, current, not_fn(ref(**pred_*))) is equal to current; and

- [(10.2)](#view-10.2)

if i is not equal to ranges​::​begin(*base_*),
then bool(invoke(**pred_*, *ranges​::​prev(i), *i)) is false[.](#view-10.sentence-1)

#### [25.7.31.3](#iter) Class chunk_by_view​::​*iterator* [[range.chunk.by.iter]](range.chunk.by.iter)

[ð](#lib:chunk_by_view::iterator)

namespace std::ranges {template<[forward_range](range.refinements#concept:forward_range "25.4.6 Other range refinements [range.refinements]") V, [indirect_binary_predicate](indirectcallable.indirectinvocable#concept:indirect_binary_predicate "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<iterator_t<V>, iterator_t<V>> Pred>requires [view](range.view#concept:view "25.4.5 Views [range.view]")<V> && is_object_v<Pred>class chunk_by_view<V, Pred>::*iterator* { chunk_by_view* *parent_* = nullptr; // *exposition only* iterator_t<V> *current_* = iterator_t<V>(); // *exposition only* iterator_t<V> *next_* = iterator_t<V>(); // *exposition only*constexpr *iterator*(chunk_by_view& parent, iterator_t<V> current, // *exposition only* iterator_t<V> next); public:using value_type = subrange<iterator_t<V>>; using difference_type = range_difference_t<V>; using iterator_category = input_iterator_tag; using iterator_concept = *see below*; *iterator*() = default; constexpr value_type operator*() const; constexpr *iterator*& operator++(); constexpr *iterator* operator++(int); constexpr *iterator*& operator--() requires [bidirectional_range](range.refinements#concept:bidirectional_range "25.4.6 Other range refinements [range.refinements]")<V>; constexpr *iterator* operator--(int) requires [bidirectional_range](range.refinements#concept:bidirectional_range "25.4.6 Other range refinements [range.refinements]")<V>; friend constexpr bool operator==(const *iterator*& x, const *iterator*& y); friend constexpr bool operator==(const *iterator*& x, default_sentinel_t); };}

[1](#iter-1)

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

*iterator*​::​iterator_concept is defined as follows:

- [(1.1)](#iter-1.1)

  If V models [bidirectional_range](range.refinements#concept:bidirectional_range "25.4.6 Other range refinements [range.refinements]"),
then iterator_concept denotes bidirectional_iterator_tag[.](#iter-1.1.sentence-1)

- [(1.2)](#iter-1.2)

  Otherwise, iterator_concept denotes forward_iterator_tag[.](#iter-1.2.sentence-1)

[ð](#lib:chunk_by_view::iterator,constructor)

`constexpr iterator(chunk_by_view& parent, iterator_t<V> current, iterator_t<V> next);
`

[2](#iter-2)

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

*Effects*: Initializes *parent_* with addressof(parent),*current_* with current, and*next_* with next[.](#iter-2.sentence-1)

[ð](#lib:operator*,chunk_by_view::iterator)

`constexpr value_type operator*() const;
`

[3](#iter-3)

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

*Preconditions*: *current_* is not equal to *next_*[.](#iter-3.sentence-1)

[4](#iter-4)

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

*Returns*: subrange(*current_*, *next_*)[.](#iter-4.sentence-1)

[ð](#lib:operator++,chunk_by_view::iterator)

`constexpr iterator& operator++();
`

[5](#iter-5)

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

*Preconditions*: *current_* is not equal to *next_*[.](#iter-5.sentence-1)

[6](#iter-6)

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

*Effects*: Equivalent to:*current_* = *next_*;*next_* = *parent_*->*find-next*(*current_*);return *this;

[ð](#lib:operator++,chunk_by_view::iterator_)

`constexpr iterator operator++(int);
`

[7](#iter-7)

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

*Effects*: Equivalent to:auto tmp = *this;++*this;return tmp;

[ð](#lib:operator--,chunk_by_view::iterator)

`constexpr iterator& operator--() requires [bidirectional_range](range.refinements#concept:bidirectional_range "25.4.6 Other range refinements [range.refinements]")<V>;
`

[8](#iter-8)

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

*Effects*: Equivalent to:*next_* = *current_*;*current_* = *parent_*->*find-prev*(*next_*);return *this;

[ð](#lib:operator--,chunk_by_view::iterator_)

`constexpr iterator operator--(int) requires [bidirectional_range](range.refinements#concept:bidirectional_range "25.4.6 Other range refinements [range.refinements]")<V>;
`

[9](#iter-9)

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

*Effects*: Equivalent to:auto tmp = *this;--*this;return tmp;

[ð](#lib:operator==,chunk_by_view::iterator)

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

[10](#iter-10)

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

*Returns*: x.*current_* == y.*current_*[.](#iter-10.sentence-1)

[ð](#lib:operator==,chunk_by_view::iterator_)

`friend constexpr bool operator==(const iterator& x, default_sentinel_t);
`

[11](#iter-11)

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

*Returns*: x.*current_* == x.*next_*[.](#iter-11.sentence-1)
