Section hub

std::ranges::views::common, std::ranges::common_view

  1. Adapts a given view with different types for iterator/sentinel pair into a view that is also a common_range. A common_view always has the same iterator/sentinel type.

# Declarations

template< ranges::view V >
requires (not ranges::common_range<V> and
std::copyable<ranges::iterator_t<V>>)
class common_view
: public ranges::view_interface<common_view<V>>

(since C++20)

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

(since C++20)

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

(since C++20)

# Notes

common_view can be useful for working with legacy algorithms that expect the iterator and sentinel are of the same type.

# Example

#include <iostream>
#include <iterator>
#include <list>
#include <numeric>
#include <ranges>
 
int main()
{
    auto v1 = {1, 2, 3, 4, 5};
    auto i1 = std::counted_iterator{v1.begin(), std::ssize(v1)};
    auto r1 = std::ranges::subrange{i1, std::default_sentinel};
//  auto e1 = std::accumulate(r1.begin(), r1.end(), 0); // error: "common range" required
    auto c1 = std::ranges::common_view{r1};
    std::cout << "accumulate: " << std::accumulate(c1.begin(), c1.end(), 0) << '\n';
 
    // inherited from ranges::view_interface:
    std::cout << "c1.front(): " << c1.front() << '\n';
    std::cout << "c1.back(): " << c1.back() << '\n';
    std::cout << "c1.data(): " << c1.data() << '\n';
    std::cout << "c1[0]: " << c1[0] << '\n';
 
    auto v2 = std::list{1, 2, 3, 4, 5};
    auto i2 = std::counted_iterator{v2.begin(), std::ssize(v2)};
    auto r2 = std::ranges::subrange{i2, std::default_sentinel};
//  auto e2 = std::accumulate(r2.begin(), r2.end(), 0); // error: "common range" required
    auto c2 = std::ranges::common_view{ r2 };
    std::cout << "accumulate: " << std::accumulate(c2.begin(), c2.end(), 0) << '\n';
 
    // inherited from ranges::view_interface:
    std::cout << "c2.front(): " << c2.front() << '\n';
//  auto e3 = c2.back(); // error: "bidirectional range" required
//  auto e4 = c2.data(); // error: "contiguous range" required
//  auto e5 = c2[0];     // error: "random access range" required
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 3494C++20common_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