std::ranges::ref_view(3)

NAME
std::ranges::ref_view - std::ranges::ref_view

Synopsis
   Defined in header <ranges>
   template< ranges::range R >
       requires std::is_object_v<R> (since C++20)
   class ref_view
       : public ranges::view_interface<ref_view<R>>
   ref_view is a view of the elements of some other range. It wraps a reference to that
   range.

Member functions
   constructor constructs a ref_view that references to the given range
   (C++20) (public member function)
   base returns the references to the referenced range
   (C++20) (public member function)
   begin returns the beginning iterator of the referenced range
   (C++20) (public member function)
   end returns the sentinel of the referenced range
   (C++20) (public member function)
   empty checks whether the referenced range is empty
   (C++20) (public member function)
   size returns the size of the referenced sized_range
   (C++20) (public member function)
   data returns the pointer to the beginning of the referenced
   (C++20) contiguous_range
                 (public member function)
         Inherited from std::ranges::view_interface
   cbegin returns a constant iterator to the beginning of the range.
   (C++23) (public member function of std::ranges::view_interface<D>)
   cend returns a sentinel for the constant iterator of the range.
   (C++23) (public member function of std::ranges::view_interface<D>)
   operator bool returns whether the derived view is not empty. Provided if
   (C++20) ranges::empty is applicable to it.
                 (public member function of std::ranges::view_interface<D>)
   front returns the first element in the derived view. Provided if it
   (C++20) satisfies forward_range.
                 (public member function of std::ranges::view_interface<D>)
   back returns the last element in the derived view. Provided if it satisfies
   (C++20) bidirectional_range and common_range.
                 (public member function of std::ranges::view_interface<D>)
   operator[] returns the n^th element in the derived view. Provided if it satisfies
   (C++20) random_access_range.
                 (public member function of std::ranges::view_interface<D>) std::ranges::ref_view::ref_view
   template< /*different-from*/<ref_view> T >
       requires std::convertible_to<T, R&> && requires { (since C++20)
   _FUN(std::declval<T>()); }
   constexpr ref_view( T&& t );
   Initializes r_ with std::addressof(static_cast<R&>(std::forward<T>(t))).
   /*different-from*/<T, U> is satisfied if and only if std::remove_cvref_t<T> and
   std::remove_cvref_t<U> are not the same type, and overloads of _FUN are declared as
   void _FUN(R&); void _FUN(R&&) = delete;.

Parameters
   t - range to reference std::ranges::ref_view::base
   constexpr R& base() const; (since C++20)
   Equivalent to return *r_;. std::ranges::ref_view::begin
   constexpr ranges::iterator_t<R> begin() const; (since C++20)
   Equivalent to return ranges::begin(*r_);. std::ranges::ref_view::end
   constexpr ranges::sentinel_t<R> end() const; (since C++20)
   Equivalent to return ranges::end(*r_);. std::ranges::ref_view::empty
   constexpr bool empty() const (since C++20)
       requires requires { ranges::empty(*r_); };
   Equivalent to return ranges::empty(*r_);. std::ranges::ref_view::size
   constexpr auto size() const (since C++20)
       requires ranges::sized_range<R>;
   Equivalent to return ranges::size(*r_);. std::ranges::ref_view::data
   constexpr auto data() const (since C++20)
       requires ranges::contiguous_range<R>;
   Equivalent to return ranges::data(*r_);.
   Deduction guides
   template< class R > (since C++20)
   ref_view( R& ) -> ref_view<R>;
   Helper templates
   template< class T >
   inline constexpr bool enable_borrowed_range<ranges::ref_view<T>> = (since C++20)
   true;
   This specialization of std::ranges::enable_borrowed_range makes ref_view satisfy
   borrowed_range.

Example
// Run this code
 #include <iostream>
 #include <ranges>
 int main()
 {
     const std::string s{"cosmos"};
     const std::ranges::take_view tv{s, 3};
     const std::ranges::ref_view rv{tv};
     std::cout
         << std::boolalpha
         << "call empty() : " << rv.empty() << '\n'
         << "call size() : " << rv.size() << '\n'
         << "call begin() : " << *rv.begin() << '\n'
         << "call end() : " << *(rv.end() - 1) << '\n'
         << "call data() : " << rv.data() << '\n'
         << "call base() : " << rv.base().size() << '\n' // ~> tv.size()
         << "range-for : ";
     for (const auto c : rv)
         std::cout << c;
     std::cout << '\n';
 }

Output:
 call empty() : false
 call size() : 3
 call begin() : c
 call end() : s
 call data() : cosmos
 call base() : 3
 range-for : cos
   Defect reports
   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.
     DR Applied to Behavior as published Correct behavior
                      default constructor was provided as removed along with the
   P2325R3 C++20 view requirement
                      must be default_initializable

See also
   reference_wrapper CopyConstructible and CopyAssignable reference wrapper
   (C++11) (class template)
   ranges::owning_view a view with unique ownership of some range
   (C++20) (class template)
   views::all_t a view that includes all elements of a range
   views::all (alias template) (range adaptor object)
   (C++20)