std::ranges::ref_view

Header: <ranges>

ref_view is a view of the elements of some other range. It wraps a reference to that range.

# Declarations

template< ranges::range R >
requires std::is_object_v<R>
class ref_view
: public ranges::view_interface<ref_view<R>>

(since C++20)

# Parameters

# Example

#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';
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
P2325R3C++20default constructor was provided as viewmust be default_initializableremoved along with the requirement

# See also