Section hub

std::ranges::views::filter, std::ranges::filter_view

  1. A range adaptor that represents view of an underlying sequence without the elements that fail to satisfy a predicate.

# Declarations

template< ranges::input_range V,
std::indirect_unary_predicate<ranges::iterator_t<V>> Pred >
requires ranges::view<V> && std::is_object_v<Pred>
class filter_view
: public ranges::view_interface<filter_view<V, Pred>>

(since C++20)

namespace views {
inline constexpr /* unspecified */ filter = /* unspecified */;
}

(since C++20)

Call signature
template< ranges::viewable_range R, class Pred >
requires /* see below */
constexpr ranges::view auto filter( R&& r, Pred&& pred );

(since C++20)

template< class Pred >
constexpr /* range adaptor closure */ filter( Pred&& pred );

(since C++20)

# Parameters

  • base: range to filter
  • pred: predicate to filter out elements

# Example

#include <iostream>
#include <ranges>
 
int main()
{
    auto even = [](int i) { return 0 == i % 2; };
    auto square = [](int i) { return i * i; };
 
    for (int i : std::views::iota(0, 6)
               | std::views::filter(even)
               | std::views::transform(square))
        std::cout << i << ' ';
    std::cout << '\n';
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 3714(P2711R1)C++20the multi-parameter constructor was not explicitmade explicit
P2325R3C++20if Pred is not default_initializable, the default constructorconstructs a filter_view which does not contain a Predthe filter_view is alsonot default_initializable

# See also

This hub groups the ranges library by user task rather than by raw reference tree shape. View types and adaptor objects are presented as the same conceptual item.

Core adapters

Start here for the adapters most people reach for first when building pipelines.

Utility views

These adapt shape, ownership, or projection rather than representing the “headline” pipeline steps.

New in C++23 / C++26

Newer adapters, kept as a compact scan list with only standard badges.

All entities by category

A lighter-weight index of the full ranges surface, grouped by conceptual task instead of raw page-tree names.

74 entities