[range.filter.view]

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

## 25.7 Range adaptors [[range.adaptors]](range.adaptors#range.filter.view)

### 25.7.8 Filter view [[range.filter]](range.filter#view)

#### 25.7.8.2 Class template filter_view [range.filter.view]

[ð](#lib:filter_view)

namespace std::ranges {template<[input_range](range.refinements#concept:input_range "25.4.6 Other range refinements [range.refinements]") V, [indirect_unary_predicate](indirectcallable.indirectinvocable#concept:indirect_unary_predicate "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<iterator_t<V>> Pred>requires [view](range.view#concept:view "25.4.5 Views [range.view]")<V> && is_object_v<Pred>class filter_view : public view_interface<filter_view<V, Pred>> {private: V *base_* = V(); // *exposition only**movable-box*<Pred> *pred_*; // *exposition only*// [[range.filter.iterator]](range.filter.iterator "25.7.8.3 Class filter_­view​::​iterator"), class filter_view​::​*iterator*class *iterator*; // *exposition only*// [[range.filter.sentinel]](range.filter.sentinel "25.7.8.4 Class filter_­view​::​sentinel"), class filter_view​::​*sentinel*class *sentinel*; // *exposition only*public: filter_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 filter_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() {if constexpr ([common_range](range.refinements#concept:common_range "25.4.6 Other range refinements [range.refinements]")<V>)return *iterator*{*this, ranges::end(*base_*)}; elsereturn *sentinel*{*this}; }}; template<class R, class Pred> filter_view(R&&, Pred) -> filter_view<views::all_t<R>, Pred>;}

[ð](#lib:filter_view,constructor)

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

[1](#1)

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

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

[ð](#lib:pred,filter_view)

`constexpr const Pred& pred() const;
`

[2](#2)

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

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

[ð](#lib:begin,filter_view)

`constexpr iterator begin();
`

[3](#3)

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

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

[4](#4)

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

*Returns*: {*this, ranges​::​find_if(*base_*, ref(**pred_*))}[.](#4.sentence-1)

[5](#5)

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

*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
when filter_view models [forward_range](range.refinements#concept:forward_range "25.4.6 Other range refinements [range.refinements]"),
this function caches the result within thefilter_view for use on subsequent calls[.](#5.sentence-1)
