std::ranges::ref_view(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

🌸 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 a ref_view that 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 referenced sized_range. (C++20) (public member function) πŸ“
  • data β€” returns the pointer to the beginning of the referenced contiguous_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 of std::ranges::view_interface<D>) ✨
  • cend β€” returns a sentinel for the constant iterator of the range. (C++23) (public member function of std::ranges::view_interface<D>) ✨
  • operator bool β€” returns whether the derived view is not empty. Provided if ranges::empty is applicable to it. (C++20) (public member function of std::ranges::view_interface<D>) πŸ’…
  • front β€” returns the first element in the derived view. Provided if it satisfies forward_range. (C++20) (public member function of std::ranges::view_interface<D>) πŸ“
  • back β€” returns the last element in the derived view. Provided if it satisfies bidirectional_range and common_range. (C++20) (public member function of std::ranges::view_interface<D>) πŸ“
  • operator[] β€” returns the n^th element in the derived view. Provided if it satisfies random_access_range. (C++20) (public member function of std::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 as view requirement must be default_initializable. Correct behavior: removed along with the requirement. 🫢

πŸ‘‘ See also πŸ‘‘

  • reference_wrapper (C++11) β€” CopyConstructible and CopyAssignable reference 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