std::ranges::ref_view(3)
πΈ NAME πΈ
Okay bestie, welcome to std::ranges::ref_view(3) from the C++ Standard Libary manual π
β the name is literally std::ranges::ref_view, and yes, thatβs the whole name. Iconic.
β¨ Synopsis β¨
Defined in header <ranges> π
template< ranges::range R >
requires std::is_object_v<R>
class ref_view
: public ranges::view_interface<ref_view<R>>
This is since C++20, no cap β¨
ref_view is a view of the elements of some other range. It wraps a reference to that range β itβs giving βIβm a view with a reference to somebody elseβs rangeβ energy π«Ά.
π Member functions π
constructorβ constructs aref_viewthat 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 referencedsized_range.(C++20)(public member function) πdataβ returns the pointer to the beginning of the referencedcontiguous_range.(C++20)(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 ofstd::ranges::view_interface<D>) β¨cendβ returns a sentinel for the constant iterator of the range.(C++23)(public member function ofstd::ranges::view_interface<D>) β¨operator boolβ returns whether the derived view is not empty. Provided ifranges::emptyis applicable to it.(C++20)(public member function ofstd::ranges::view_interface<D>) πfrontβ returns the first element in the derived view. Provided if it satisfiesforward_range.(C++20)(public member function ofstd::ranges::view_interface<D>) πbackβ returns the last element in the derived view. Provided if it satisfiesbidirectional_rangeandcommon_range.(C++20)(public member function ofstd::ranges::view_interface<D>) πoperator[]β returns then^thelement in the derived view. Provided if it satisfiesrandom_access_range.(C++20)(public member function ofstd::ranges::view_interface<D>) π₯
π 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 );
This one is since C++20 π
It 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
requires requires { ranges::empty(*r_); };
since C++20 β¨
Equivalent to return ranges::empty(*r_);.
π std::ranges::ref_view::size π
constexpr auto size() const
requires ranges::sized_range<R>;
since C++20 β¨
Equivalent to return ranges::size(*r_);.
π₯ std::ranges::ref_view::data π₯
constexpr auto data() const
requires ranges::contiguous_range<R>;
since C++20 β¨
Equivalent to return ranges::data(*r_);.
β¨ Deduction guides β¨
template< class R >
ref_view( R& ) -> ref_view<R>;
since C++20 π
π§ 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.
π 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
P2325R3β applied to C++20. Behavior as published: default constructor was provided asviewrequirement must bedefault_initializable. Correct behavior: removed along with the requirement. π«Ά
π See also π
reference_wrapper(C++11)βCopyConstructibleandCopyAssignablereference wrapper (class template) πranges::owning_view(C++20)β a view with unique ownership of some range (class template) πviews::all_t/views::all(C++20)β a view that includes all elements of a range (alias template) (range adaptor object) π
Thatβs the whole page, bestie π«Ά β source: http://cppreference.com, version/date: 2024.06.10, page: std::ranges::ref_view(3) β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms