std::ranges::owning_view
Min standard notice:
Header: <ranges>
owning_view is a view that has unique ownership of a range. It is move-only and stores that range within it.
# Declarations
template< ranges::range R >
requires std::movable<R> && (!/*is-initializer-list*/<R>)
class owning_view
: public ranges::view_interface<owning_view<R>>
(since C++20)
# Parameters
other: another owning_view to move fromt: range to move from
# Return value
*this
# Example
#include <cassert>
#include <iostream>
#include <ranges>
#include <string>
int main()
{
using namespace std::literals;
std::ranges::owning_view ov{"cosmos"s}; // the deduced type of R is std::string;
// `ov` is the only owner of this string
assert(
ov.empty() == false &&
ov.size() == 6 &&
ov.size() == ov.base().size() &&
ov.front() == 'c' &&
ov.front() == *ov.begin() &&
ov.back() == 's' &&
ov.back() == *(ov.end() - 1) &&
ov.data() == ov.base()
);
std::cout << "sizeof(ov): " << sizeof ov << '\n' // typically equal to sizeof(R)
<< "range-for: ";
for (const char ch : ov)
std::cout << ch;
std::cout << '\n';
std::ranges::owning_view<std::string> ov2;
assert(ov2.empty());
// ov2 = ov; // compile-time error: copy assignment operator is deleted
ov2 = std::move(ov); // OK
assert(ov2.size() == 6);
}