Section hub

std::ranges::views::take, std::ranges::take_view

  1. A range adaptor that represents view of the elements from an underlying sequence, starting at the beginning and ending at a given bound.

# Declarations

template< ranges::view V >
class take_view
: public ranges::view_interface<take_view<V>>

(since C++20)

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

(since C++20)

Call signature
template< ranges::viewable_range R >
requires /* see below */
constexpr ranges::view auto
take( R&& r, ranges::range_difference_t<R> count );

(since C++20)

template< class DifferenceType >
constexpr /* range adaptor closure */ take( DifferenceType&& count );

(since C++20)

# Example

#include <algorithm>
#include <iostream>
#include <ranges>
 
int main()
{
    namespace views = std::views;
    auto print = [](char x){ std::cout << x; };
 
    for (const char nums[]{'1', '2', '3'};
         int n : views::iota(0, 5))
    {
        std::cout << "take(" << n << "): ";
        // safely takes only upto min(n, nums.size()) elements:
        std::ranges::for_each(nums | views::take(n), print);
        std::cout << '\n';
    }
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 3407C++20views::take sometimes failed toconstruct a sized random access rangethe result type is adjusted sothat construction is always valid
LWG 3494C++20take_view was never a borrowed_rangeit is a borrowed_range if its underlying view is

# 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