Section hub

std::ranges::views::take_while, std::ranges::take_while_view

  1. A range adaptor that represents view of the elements from an underlying sequence, starting at the beginning and ending at the first element for which the predicate returns false.

# Declarations

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

(since C++20)

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

(since C++20)

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

(since C++20)

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

(since C++20)

# Example

#include <iostream>
#include <ranges>
 
int main()
{
    for (int year : std::views::iota(2020)
                  | std::views::take_while([](int y){ return y < 2026; }))
        std::cout << year << ' ';
    std::cout << '\n';
 
    const char note[]{"Today is yesterday's tomorrow!..."};
    auto not_dot = [](char c){ return c != '.'; };
    for (char x : std::ranges::take_while_view(note, not_dot))
        std::cout << x;
    std::cout << '\n';
}

# 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