std::ranges::ref_view - cppreference.com (original) (raw)

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

Contents

[edit] Data members

Member Description
R* r_ a pointer to the underlying range(exposition-only member object*)

[edit] Member functions

(constructor) constructs a ref_view that references to the given range (public member function)
base returns the references to the referenced range (public member function)
begin returns the beginning iterator of the referenced range (public member function)
end returns the sentinel of the referenced range (public member function)
empty checks whether the referenced range is empty (public member function)
size returns the size of the referenced sized_range (public member function)
reserve_hint(C++26) returns the approximate size of the referenced approximately_sized_range (public member function)
data returns the pointer to the beginning of the referenced contiguous_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 of std::ranges::view_interface) [edit]
cend(C++23) returns a sentinel for the constant iterator of the range (public member function of std::ranges::view_interface) [edit]
operator bool returns whether the derived view is not empty, provided only if ranges::empty is applicable to it (public member function of std::ranges::view_interface) [edit]
front returns the first element in the derived view, provided if it satisfies forward_range (public member function of std::ranges::view_interface) [edit]
back returns the last element in the derived view, provided only if it satisfies bidirectional_range and common_range (public member function of std::ranges::view_interface) [edit]
operator[] returns the nth element in the derived view, provided only if it satisfies random_access_range (public member function of std::ranges::view_interface) [edit]

std::ranges::ref_view::ref_view

| 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](#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

std::ranges::ref_view::base

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

Returns *_[r](#r)_.

std::ranges::ref_view::empty

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

Returns ranges::empty(*_[r](#r)_ ).

std::ranges::ref_view::reserve_hint

| constexpr auto size() const requires ranges::approximately_sized_range<R>; | | (since C++26) | | ----------------------------------------------------------------------------- | | ------------- |

Returns ranges::reserve_hint(*_[r](#r)_ ).

[edit] Deduction guides

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

[edit] Helper templates

| template< class T > 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] Notes

Feature-test macro Value Std Feature
__cpp_lib_ranges_reserve_hint 202502L (C++26) ranges::approximately_sized_range and reserve_hint

[edit] Example

#include #include   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

[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 asview must be default_initializable removed along with the requirement

[edit] See also