Section hub

std::ranges::views::slide, std::ranges::slide_view

  1. slide_view is a range adaptor that takes a view and a number n and produces a view whose mth element (a “window”) is a view over the mth through (m + n - 1)th elements of the original view.

# Declarations

template< ranges::forward_range V >
requires ranges::view<V>
class slide_view
: public ranges::view_interface<slide_view<V>>

(since C++23)

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

(since C++23)

Call signature
template< ranges::viewable_range R >
constexpr ranges::view auto slide( R&& r, ranges::range_difference_t<R> n );

(since C++23)

template< class DifferenceType >
constexpr /* range adaptor object */ slide( DifferenceType&& n );

(since C++23)

Helper concepts
template< class V >
concept /*slide-caches-nothing*/ =
ranges::random_access_range<V> && ranges::sized_range<V>;

(exposition only*)

template< class V >
concept /*slide-caches-last*/ =
!/*slide-caches-nothing*/<V> &&
ranges::bidirectional_range<V> && ranges::common_range<V>;

(exposition only*)

template< class V >
concept /*slide-caches-first*/ =
!/*slide-caches-nothing*/<V> && !/*slide-caches-last*/<V>;

(exposition only*)

# Notes

There are similarities between ranges::adjacent_view and ranges::slide_view:

The following table shows the differences between these adaptors:

# Example

#include <algorithm>
#include <iostream>
#include <ranges>
 
auto print_subrange = [](std::ranges::viewable_range auto&& r)
{
    std::cout << '[';
    for (char space[]{0,0}; auto elem : r)
        std::cout << space << elem, *space = ' ';
    std::cout << "] ";
};
 
int main()
{
    const auto v = {1, 2, 3, 4, 5, 6};
 
    std::cout << "All sliding windows of width:\n";
    for (const unsigned width : std::views::iota(1U, 1U + v.size()))
    {
        auto const windows = v | std::views::slide(width);
        std::cout << "W = " << width << ": ";
        std::ranges::for_each(windows, print_subrange);
        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