CKTIS / C++
std::views::all — a view of an entire range
std::views::all adapts a range to the view interface while retaining every element. Its result can be a copy or move of an existing view, a reference view, or an owning view; that choice determines how data lifetime must be managed.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
#include <ranges>
std::views::all(r)
r | std::views::allstd::views::all, declared in <ranges>, is a range adaptor object. The expressions std::views::all(r) and r | std::views::all produce a view of every element without filtering or transforming their values. The argument must satisfy std::ranges::viewable_range.
Selection follows a fixed order: if the argument's type after removing references and qualifiers models std::ranges::view, the result is an object of that type, copied or moved according to the value category. Otherwise, it produces std::ranges::ref_view if that can be constructed; failing that, it produces std::ranges::owning_view. Not every view can be passed as an lvalue: a move-only view may require std::move.
For an lvalue std::vector<int>, the result refers to the existing vector; for a temporary std::vector<int>, it takes ownership of the vector. A ref_view does not extend the source's lifetime. The source must remain alive while the view is used, and the container's iterator invalidation rules still apply. For the vector passed as an lvalue, all does not copy the elements. In contrast, passing a copyable view as an lvalue can copy both the view and the elements it stores: a copy of std::ranges::single_view<int> stores its own int.
The example demonstrates both cases: writing through borrowed changes values, whereas owned stores its own vector. The expected output is two lines containing 9. The repository uses the C++23 flag; production runtime compatibility has not been attested.
Examples
#include <iostream>
#include <ranges>
#include <type_traits>
#include <vector>
int main() {
std::vector<int> values{1, 2, 3};
auto borrowed = values | std::views::all;
auto owned = std::views::all(std::vector<int>{4, 5});
static_assert(std::is_same_v<decltype(borrowed),
std::ranges::ref_view<std::vector<int>>>);
static_assert(std::is_same_v<decltype(owned),
std::ranges::owning_view<std::vector<int>>>);
borrowed.front() = 9;
std::cout << values.front() << '\n';
int sum = 0;
for (int value : owned) {
sum += value;
}
std::cout << sum << '\n';
}
Result
9 9
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.