On this page
std::ranges::owning_view
Defined in header <ranges> |
||
---|---|---|
|
(since C++20) |
owning_view
is a view
that has unique ownership of a range
. It is move-only and stores that range
within it.
The constant /*is-initializer-list*/<R>
in the requires-clause is true
if and only if std::remove_cvref_t<R>
is a specialization of std::initializer_list
.
Data members
Member name | Definition |
---|---|
r_ (private) |
The underlying range of type R .(exposition-only member object*) |
Member functions
(C++20)
|
constructs an owning_view by value-initializing or move-constructing the stored range (public member function) |
(C++20)
|
move-assigns the stored range (public member function) |
(C++20)
|
returns a reference to the stored range (public member function) |
(C++20)
|
returns the beginning iterator of the stored range (public member function) |
(C++20)
|
returns the sentinel of the stored range (public member function) |
(C++20)
|
checks whether the stored range is empty (public member function) |
(C++20)
|
returns the size of the stored sized_range (public member function) |
(C++20)
|
returns the pointer to the beginning of the stored contiguous_range (public member function) |
Inherited from |
|
(C++23)
|
returns a constant iterator to the beginning of the range. (public member function of std::ranges::view_interface<D> ) |
(C++23)
|
returns a sentinel for the constant iterator of the range. (public member function of std::ranges::view_interface<D> ) |
(C++20)
|
returns whether the derived view is not empty. Provided if ranges::empty is applicable to it. (public member function of std::ranges::view_interface<D> ) |
(C++20)
|
returns the first element in the derived view. Provided if it satisfies forward_range . (public member function of std::ranges::view_interface<D> ) |
(C++20)
|
returns the last element in the derived view. Provided if it satisfies bidirectional_range and common_range . (public member function of std::ranges::view_interface<D> ) |
(C++20)
|
returns the nth element in the derived view. Provided if it satisfies random_access_range . (public member function of std::ranges::view_interface<D> ) |
std::ranges::owning_view::owning_view
|
(1) | (since C++20) |
|
(2) | (since C++20) |
|
(3) | (since C++20) |
|
(4) | (since C++20) |
= R()
).
other
.
t
.
owning_view
is move-only.
Parameters
other | - | another owning_view to move from |
t | - | range to move from |
std::ranges::owning_view::operator=
|
(1) | (since C++20) |
|
(2) | (since C++20) |
other
.
owning_view
is move-only.
Parameters
other | - | another owning_view to move from |
Return value
*this
std::ranges::owning_view::base
|
(1) | (since C++20) |
|
(2) | (since C++20) |
|
(3) | (since C++20) |
|
(4) | (since C++20) |
Returns a reference to the stored range, keeping value category and const-qualification.
return r_;
.
return std::move(r_);
.
std::ranges::owning_view::begin
|
(1) | (since C++20) |
|
(2) | (since C++20) |
Equivalent to return ranges::begin(r_);
.
std::ranges::owning_view::end
|
(1) | (since C++20) |
|
(2) | (since C++20) |
Equivalent to return ranges::end(r_);
.
std::ranges::owning_view::empty
|
(1) | (since C++20) |
|
(2) | (since C++20) |
Equivalent to return ranges::empty(r_);
.
std::ranges::owning_view::size
|
(1) | (since C++20) |
|
(2) | (since C++20) |
Equivalent to return ranges::size(r_);
.
std::ranges::owning_view::data
|
(1) | (since C++20) |
|
(2) | (since C++20) |
Equivalent to return ranges::data(r_);
.
Helper templates
|
(since C++20) |
This specialization of std::ranges::enable_borrowed_range
makes owning_view
satisfy borrowed_range
when the underlying range satisfies it.
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);
}
Possible output:
sizeof(ov): 32
range-for: cosmos
See also
(C++20)
|
a view of the elements of some other range (class template) |
(C++20)
|
a view that includes all elements of a range (alias template) (range adaptor object) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/ranges/owning_view