std::ranges::owning_view - cppreference.com (original) (raw)
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.
Contents
- 1 Data members
- 2 Member functions
- 3 std::ranges::owning_view::owning_view
- 4 std::ranges::owning_view::operator=
- 5 std::ranges::owning_view::base
- 6 std::ranges::owning_view::begin
- 7 std::ranges::owning_view::end
- 8 std::ranges::owning_view::empty
- 9 std::ranges::owning_view::size
- 10 std::ranges::owning_view::reserve_hint
- 11 std::ranges::owning_view::data
[edit] Data members
Member | Description |
---|---|
R r_ | the underlying range(exposition-only member object*) |
[edit] Member functions
(constructor) | constructs an owning_view by value-initializing or move-constructing the stored range (public member function) |
---|---|
operator= | move-assigns the stored range (public member function) |
base | returns a reference to the stored range (public member function) |
begin | returns the beginning iterator of the stored range (public member function) |
end | returns the sentinel of the stored range (public member function) |
empty | checks whether the stored range is empty (public member function) |
size | returns the size of the stored sized_range (public member function) |
reserve_hint(C++26) | returns the approximate size of the stored approximately_sized_range (public member function) |
data | returns the pointer to the beginning of the stored contiguous_range (public member function) |
Inherited from std::ranges::view_interface | |
cbegin(C++23) | returns a constant iterator to the beginning of the range (public member function of std::ranges::view_interface) [edit] |
cend(C++23) | returns a sentinel for the constant iterator of the range (public member function of std::ranges::view_interface) [edit] |
operator bool | returns whether the derived view is not empty, provided only if ranges::empty is applicable to it (public member function of std::ranges::view_interface) [edit] |
front | returns the first element in the derived view, provided if it satisfies forward_range (public member function of std::ranges::view_interface) [edit] |
back | returns the last element in the derived view, provided only if it satisfies bidirectional_range and common_range (public member function of std::ranges::view_interface) [edit] |
operator[] | returns the nth element in the derived view, provided only if it satisfies random_access_range (public member function of std::ranges::view_interface) [edit] |
std::ranges::owning_view::owning_view
owning_view() requires std::default_initializable<R> = default; | (1) | (since C++20) |
---|---|---|
owning_view( owning_view&& other ) = default; | (2) | (since C++20) |
constexpr owning_view( R&& t ); | (3) | (since C++20) |
Default constructor. Value-initializes
_[r](#r)_
by its default member initializer (= R()).Move constructor. Move constructs
_[r](#r)_
from that of other.Move constructs
_[r](#r)_
from t.
Parameters
other | - | another owning_view to move from |
---|---|---|
t | - | range to move from |
Notes
owning_view
does not explicitly define a copy constructor. owning_view
is move-only.
std::ranges::owning_view::operator=
| owning_view& operator=( owning_view&& other ) = default; | | (since C++20) | | ------------------------------------------------------------ | | ------------- |
Move assignment operator. Move assigns _[r](#r)_
from that of other.
Parameters
other | - | another owning_view to move from |
---|
Return value
*this
Notes
owning_view
does not explicitly define a copy assignment operator. owning_view
is move-only.
std::ranges::owning_view::base
constexpr R& base() & noexcept; | (1) | (since C++20) |
---|---|---|
constexpr const R& base() const & noexcept; | (2) | (since C++20) |
constexpr R&& base() && noexcept; | (3) | (since C++20) |
constexpr const R&& base() const && noexcept; | (4) | (since C++20) |
Returns a reference to the stored range, keeping value category and const-qualification.
Return value
1,2) _[r](#r)_
3,4) std::move(_[r](#r)_
)
std::ranges::owning_view::empty
constexpr bool empty() requires requires { ranges::empty(r_); }; | (1) | (since C++20) |
---|---|---|
constexpr bool empty() const requires requires { ranges::empty(r_); }; | (2) | (since C++20) |
Returns ranges::empty(_[r](#r)_
).
std::ranges::owning_view::reserve_hint
constexpr auto reserve_hint() requires ranges::approximately_sized_range<R>; | (1) | (since C++26) |
---|---|---|
constexpr auto reserve_hint() const requires ranges::approximately_sized_range<const R>; | (2) | (since C++26) |
Returns ranges::reserve_hint(_[r](#r)_
).
[edit] Helper templates
This specialization of ranges::enable_borrowed_range makes owning_view
satisfy borrowed_range when the underlying range satisfies it.
[edit] Notes
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_ranges_reserve_hint | 202502L | (C++26) | ranges::approximately_sized_range and reserve_hint |
[edit] Example
#include #include #include #include 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