From cppreference.com
template<ranges::rangeR>requiresstd::movable<R>&&(!/*is-initializer-list*/<R>)classowning_view:publicranges::view_interface<owning_view<R>>(since C++20)owning_view is a
that has unique ownership of a
. 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
.
Data members
Member Description Rr_the underlying range
(exposition-only member object*)Member functions
constructs an owning_view by value-initializing or move-constructing the stored range
(public member function)
move-assigns the stored range
(public member function)
returns a reference to the stored range
(public member function)
returns the beginning iterator of the stored range
(public member function)
returns the sentinel of the stored range
(public member function)
checks whether the stored range is empty
(public member function)
returns the size of the stored
(public member function)
(C++26)
returns the approximate size of the stored
(public member function)
returns the pointer to the beginning of the stored
(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>)
returns whether the derived view is not empty, provided only if
is applicable to it
(public member function of std::ranges::view_interface<D>)
returns the first element in the derived view, provided if it satisfies
(public member function of std::ranges::view_interface<D>)
returns the last element in the derived view, provided only if it satisfies
and
(public member function of std::ranges::view_interface<D>)
returns the nth element in the derived view, provided only if it satisfies
(public member function of std::ranges::view_interface<D>)
std::ranges::owning_view::owning_view
owning_view()requiresstd::default_initializable<R>=default; (1) (since C++20)owning_view(owning_view&&other)=default; (2) (since C++20)constexprowning_view(R&&t); (3) (since C++20)1) Default constructor. Value-initializes
by its default member initializer (=R()).
2) Move constructor. Move constructs
from that of other.
3) Move constructs
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
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
constexprR&base()&noexcept; (1) (since C++20)constexprconstR&base()const&noexcept; (2) (since C++20)constexprR&&base()&&noexcept; (3) (since C++20)constexprconstR&&base()const&&noexcept; (4) (since C++20)Returns a reference to the stored range, keeping value category and const-qualification.
Return value
1,2)
3,4)std::move(
)
std::ranges::owning_view::begin
constexprranges::iterator_t<R>begin(); (1) (since C++20)constexprautobegin()constrequiresranges::range<constR>; (2) (since C++20)Returns ranges::begin(
).
std::ranges::owning_view::end
constexprranges::sentinel_t<R>end(); (1) (since C++20)constexprautoend()constrequiresranges::range<constR>; (2) (since C++20)Returns ranges::end(
).
std::ranges::owning_view::empty
constexprboolempty()requiresrequires{ranges::empty(r_);}; (1) (since C++20)constexprboolempty()constrequiresrequires{ranges::empty(r_);}; (2) (since C++20)Returns ranges::empty(
).
std::ranges::owning_view::size
constexprautosize()requiresranges::sized_range<R>; (1) (since C++20)constexprautosize()constrequiresranges::sized_range<constR>; (2) (since C++20)Returns ranges::size(
).
std::ranges::owning_view::reserve_hint
constexprautoreserve_hint()requiresranges::approximately_sized_range<R>; (1) (since C++26)constexprautoreserve_hint()constrequiresranges::approximately_sized_range<constR>; (2) (since C++26)Returns ranges::reserve_hint(
).
std::ranges::owning_view::data
constexprautodata()requiresranges::contiguous_range<R>; (1) (since C++20)constexprautodata()constrequiresranges::contiguous_range<constR>; (2) (since C++20)Returns ranges::data(
).
Helper templates
template<classT>constexprboolenable_borrowed_range<std::ranges::owning_view<T>>=ranges::enable_borrowed_range<T>;(since C++20)This specialization of ranges::enable_borrowed_range makes owning_view satisfy
when the underlying range satisfies it.
Notes
macroValueStdFeature
(C++26)ranges::approximately_sized_range and
Example
Run this code
#include<cassert>#include<iostream>#include<ranges>#include<string>intmain(){usingnamespacestd::literals;std::ranges::owning_viewov{"cosmos"s};// the deduced type of R is std::string;// “ov” is the only owner of this stringassert(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): "<<sizeofov<<'\n'// typically equal to sizeof(R)<<"range-for: ";for(constcharch: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 deletedov2=std::move(ov);// OKassert(ov2.size()==6);}Possible output:
sizeof(ov): 32 range-for: cosmos See also