< cpp‎ | ranges
Defined in header <ranges>
template<ranges::range R>

    requires std::is_object_v<R>

class ref_view : public ranges::view_interface<ref_view<R>>
(since C++20)

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


[edit] Data members

ref_view behaves as if it only stores a R* member subobject pointing to the referenced range. The member is shown as r_ here (the name is exposition-only).

[edit] Member functions

constructs a ref_view that references to the given range
(public member function)
returns the references to the referenced range
(public member function)
returns the beginning iterator of the referenced range
(public member function)
returns the sentinel of the referenced range
(public member function)
checks whether the referenced range is empty
(public member function)
returns the size of the referenced sized_range
(public member function)
returns the pointer to the beginning of the referenced contiguous_range
(public member function)
Inherited from std::ranges::view_interface
Returns whether the derived view is not empty. Provided if ranges::empty is applicable to it.
(public member function of std::ranges::view_interface<D>) [edit]
Returns the first element in the derived view. Provided if it satisfies forward_range.
(public member function of std::ranges::view_interface<D>) [edit]
Returns the last element in the derived view. Provided if it satisfies bidirectional_range and common_range.
(public member function of std::ranges::view_interface<D>) [edit]
Returns the nth element in the derived view. Provided if it satisfies random_access_range.
(public member function of std::ranges::view_interface<D>) [edit]


template</*different-from*/<ref_view> T>

    requires std::convertible_to<T, R&> && requires { _FUN(std::declval<T>()); }

constexpr ref_view(T&& t);
(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;.


t - range to reference


constexpr R& base() const;
(since C++20)

Equivalent to return *r_;


constexpr ranges::iterator_t<R> begin() const;
(since C++20)

Equivalent to return ranges::begin(*r_);


constexpr ranges::sentinel_t<R> end() const;
(since C++20)

Equivalent to return ranges::end(*r_);


constexpr bool empty() const
    requires requires { ranges::empty(*r_); };
(since C++20)

Equivalent to return ranges::empty(*r_);


constexpr auto size() const

    requires ranges::sized_range<R>

{ return ranges::size(*r_); }
(since C++20)


constexpr auto data() const

    requires ranges::contiguous_range<R>

{ return ranges::data(*r_); }
(since C++20)

[edit] Deduction guides

template<class R>
ref_view(R&) -> ref_view<R>;
(since C++20)

[edit] Helper templates

template<class T>
inline constexpr bool enable_borrowed_range<ranges::ref_view<T>> = true;
(since C++20)

This specialization of std::ranges::enable_borrowed_range makes ref_view satisfy borrowed_range.

[edit] Example

#include <ranges>
#include <iostream>
int main()
    const std::string s{"cosmos"};
    const std::ranges::take_view tv{s, 3};
    const std::ranges::ref_view rv{tv};
        << 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()  : " << << '\n'
        << "call base()  : " << rv.base().size() << '\n' // ~> tv.size()
        << "range-for    : ";
    for (const auto c: rv) { std::cout << c; }
    std::cout << '\n';


call empty() : false
call size()  : 3
call begin() : c
call end()   : s
call data()  : cosmos
call base()  : 3
range-for    : cos

[edit] Defect 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 view
must be default_initializable
removed along with the requirement

[edit] See also

CopyConstructible and CopyAssignable reference wrapper
(class template) [edit]
a view that includes all elements of a range
(alias template) (range adaptor object) [edit]