On this page
std::ranges::ref_view
| Defined in header <ranges> | ||
|---|---|---|
|  | (since C++20) | 
ref_view is a view of the elements of some other range. It wraps a reference to that range.
Data members
| Member name | Definition | 
|---|---|
| r_(private) | A pointer of type R*to the underlying range.(exposition-only member object*) | 
Member functions
| (C++20)
        | constructs a ref_viewthat references to the given range(public member function) | 
| (C++20)
        | returns the references to the referenced range (public member function) | 
| (C++20)
        | returns the beginning iterator of the referenced range (public member function) | 
| (C++20)
        | returns the sentinel of the referenced range (public member function) | 
| (C++20)
        | checks whether the referenced range is empty (public member function) | 
| (C++20)
        | returns the size of the referenced sized_range(public member function) | 
| (C++20)
        | returns the pointer to the beginning of the referenced contiguous_range(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>) | 
| (C++20)
        | returns whether the derived view is not empty. Provided if ranges::emptyis 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_rangeandcommon_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::ref_view::ref_view
|  | (since C++20) | 
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
|  | (since C++20) | 
Equivalent to return *r_;.
std::ranges::ref_view::begin
|  | (since C++20) | 
Equivalent to return ranges::begin(*r_);.
std::ranges::ref_view::end
|  | (since C++20) | 
Equivalent to return ranges::end(*r_);.
std::ranges::ref_view::empty
|  | (since C++20) | 
Equivalent to return ranges::empty(*r_);.
std::ranges::ref_view::size
|  | (since C++20) | 
Equivalent to return ranges::size(*r_);.
std::ranges::ref_view::data
|  | (since C++20) | 
Equivalent to return ranges::data(*r_);.
Deduction guides
|  | (since C++20) | 
Helper templates
|  | (since C++20) | 
This specialization of std::ranges::enable_borrowed_range makes ref_view satisfy borrowed_range.
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';
}Output:
call empty() : false
call size()  : 3
call begin() : c
call end()   : s
call data()  : cosmos
call base()  : 3
range-for    : cosDefect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior | 
|---|---|---|---|
| P2325R3 | C++20 | default constructor was provided as viewmust be default_initializable | removed along with the requirement | 
See also
| (C++11)
        | CopyConstructible and CopyAssignable reference wrapper (class template) | 
| (C++20)
        | a viewwith unique ownership of somerange(class template) | 
| (C++20)
        | a viewthat includes all elements of arange(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/ref_view