std::ranges::owning_view(3)
π std::ranges::owning_view π
Okay so this is the std::ranges::owning_view(3) page from the C++ Standard Libary manual (yes, that spelling is literally in the header bestie π). Itβs giving βI own this range, I do not share itβ energy, and honestly? Iconic. π
πΈ NAME πΈ
std::ranges::owning_view β std::ranges::owning_view
β¨ Synopsis β¨
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) β yup, this whole vibe is C++20 coded.
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.
π§ Member functions π§
constructor(C++20)β constructs anowning_viewby 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 storedsized_range(public member function).data(C++20)β returns the pointer to the beginning of the storedcontiguous_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 ofstd::ranges::view_interface<D>)cend(C++23)β returns a sentinel for the constant iterator of the range. (public member function ofstd::ranges::view_interface<D>)operator bool(C++20)β returns whether the derived view is not empty. Provided ifranges::emptyis applicable to it. (public member function ofstd::ranges::view_interface<D>)front(C++20)β returns the first element in the derived view. Provided if it satisfiesforward_range. (public member function ofstd::ranges::view_interface<D>)back(C++20)β returns the last element in the derived view. Provided if it satisfiesbidirectional_rangeandcommon_range. (public member function ofstd::ranges::view_interface<D>)operator[](C++20)β returns then^thelement in the derived view. Provided if it satisfiesrandom_access_range. (public member function ofstd::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)
- Default constructor. Value-initializes the stored range by its default member initializer (
= R()). - Move constructor. Move constructs the stored range from that of
other. - Move constructs the stored range from
t. - Copy constructor is deleted.
owning_viewis move-only.
Parameters
otherβ anotherowning_viewto move fromtβ 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)
- Move assignment operator. Move assigns the stored range from that of
other. - Copy assignment operator is deleted.
owning_viewis move-only.
Parameters
otherβ anotherowning_viewto 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 toreturn r_;.3,4)Equivalent toreturn 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 π₯
// Run this code
#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 π
ranges::ref_viewβ a view of the elements of some other range(C++20)(class template)views::all_t,views::allβ a view that includes all elements of a range(C++20);views::all_tis the alias template andviews::allis the range adaptor object.
Okay bestie, thatβs the whole page π«Ά Source: http://cppreference.com. Version/date: 2024.06.10. Page: std::ranges::owning_view(3). Now go own a range and slay π
β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms