cpp / latest / ranges / owning_view.html /

std::ranges::owning_view

Defined in header <ranges>
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)

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

Typical implementations of owning_view have only one non-static data member: the underlying range of type R. The member is shown as r_ here (the name is exposition-only).

Member functions

(constructor)
(C++20)
constructs a owning_view by value-initializing or move-constructing the stored range
(public member function)
operator=
(C++20)
move-assigns the stored range
(public member function)
base
(C++20)
returns a reference to the stored range
(public member function)
begin
(C++20)
returns the beginning iterator of the stored range
(public member function)
end
(C++20)
returns the sentinel of the stored range
(public member function)
empty
(C++20)
checks whether the stored range is empty
(public member function)
size
(C++20)
returns the size of the stored sized_range
(public member function)
data
(C++20)
returns the pointer to the beginning of the stored contiguous_range
(public member function)
Inherited from std::ranges::view_interface
(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

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)
owning_view( const owning_view& ) = delete;
(4) (since C++20)
1) Default constructor. Value-initializes the stored range by its default member initializer ( = R()).
2) Move constructor. Move constructs the stored range from that of other.
3) Move constructs the stored range from t.
4) Copy constructor is deleted. owning_view is move-only.

Parameters

other - another owning_view to move from
t - range to move from

std::ranges::owning_view::operator=

owning_view& operator=( owning_view&& other ) = default;
(1) (since C++20)
owning_view& operator=( const owning_view& ) = delete;
(2) (since C++20)
1) Move assignment operator. Move assigns the stored range from that of other.
2) Copy assignment operator is deleted. owning_view is move-only.

Parameters

other - another owning_view to move from

Return value

*this.

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.

1-2) Equivalent to return r_;.
3-4) Equivalent to return std::move(r_);.

std::ranges::owning_view::begin

constexpr ranges::iterator_t<R> begin();
(1) (since C++20)
constexpr auto begin() const requires ranges::range<const R>;
(2) (since C++20)

Equivalent to return ranges::begin(r_);.

std::ranges::owning_view::end

constexpr ranges::sentinel_t<R> end();
(1) (since C++20)
constexpr auto end() const requires ranges::range<const R>;
(2) (since C++20)

Equivalent to return ranges::end(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)

Equivalent to return ranges::empty(r_);.

std::ranges::owning_view::size

constexpr auto size() requires ranges::sized_range<R>;
(1) (since C++20)
constexpr auto size() const requires ranges::sized_range<const R>;
(2) (since C++20)

Equivalent to return ranges::size(r_);.

std::ranges::owning_view::data

constexpr auto data() requires ranges::contiguous_range<R>;
(1) (since C++20)
constexpr auto data() const requires ranges::contiguous_range<const R>;
(2) (since C++20)

Equivalent to return ranges::data(r_);.

Helper templates

template<class T>
inline constexpr bool enable_borrowed_range<std::ranges::owning_view<T>> =
    std::ranges::enable_borrowed_range<T>;
(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 <ranges>
#include <string>
#include <cassert>
#include <iostream>
 
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
    std::cout
        << std::boolalpha
        << "call empty() : " << ov.empty() << '\n'
        << "call size()  : " << ov.size() << '\n'
        << "call front() : " << ov.front() << '\n' // same as *(ov.begin())
        << "call back()  : " << ov.back() << '\n' // equals to *(ov.end()-1)
        << "call data()  : " << ov.data() << '\n'
        << "call base()  : " << ov.base().size() << '\n' // ~> ov.size()
        << "sizeof(ov)   : " << sizeof(ov) << '\n' // typically equals to sizeof(R)
        << "range-for    : \"";
 
    for (const char c: ov)
        std::cout << c;
    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(ov.empty());
    assert(ov2.size() == 6);
}

Possible output:

call empty() : false
call size()  : 6
call front() : c
call back()  : s
call data()  : cosmos
call base()  : 6
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