std::ranges::rend(3)

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

✨ NAME ✨

std::ranges::rend β€” std::ranges::rend. yes bestie, the name is the name, and it's serving self-titled icon energy πŸ’…. This is page std::ranges::rend(3) from the C++ Standard Libary β€” their spelling, not mine 🫢.

🌸 SYNOPSIS 🌸

okay so first things first: this lives in header <ranges> and header <iterator> ✨.

The inline namespace situation is highkey giving (since C++20):

inline namespace /* unspecified */ {
    inline constexpr /* unspecified */ rend = /* unspecified customization point object */;
}

(since C++20) β€” yes, again, because the standard loves a timestamp.

πŸ“ž Call signature πŸ“ž

Here's the signature, no cap:

template< class T >
    requires /* see below */
constexpr std::sentinel_for<
    decltype(ranges::rbegin(std::declval<T>()))> auto rend(
    T&& t );

(since C++20).

πŸ’« what it does πŸ’«

It gives you a sentinel that marks the end of a reversed range πŸŽ€.

There's also a diagram file called range-rbegin-rend.svg β€” visual vibe check, iykyk.

If T is an array type and std::remove_all_extents_t<std::remove_reference_t<T>> is incomplete, then the call to ranges::rend is ill-formed, no diagnostic required 😭. Like, the compiler doesn't even have to tell you. Rude.

If the argument is an lvalue or ranges::enable_borrowed_range<std::remove_cv_t<T>> is true, then a call to ranges::rend is expression-equivalent to:

  1. decay-copy(t.rend()) (until C++23) or auto(t.rend()) (since C++23) β€” if that expression is valid and its type models std::sentinel_for<decltype(ranges::rbegin(std::declval<T>()))>.
  2. Otherwise, decay-copy(rend(t)) (until C++23) or auto(rend(t)) (since C++23) β€” if T is a class or enumeration type, that expression is valid and its type models std::sentinel_for<decltype(ranges::rbegin(std::declval<T>()))>, where the meaning of rend is established as if by performing argument-dependent lookup only.
  3. Otherwise, std::make_reverse_iterator(ranges::begin(t)) β€” if both ranges::begin(t) and ranges::end(t) are valid expressions, have the same type, and that type models std::bidirectional_iterator.

Every other case? A call to ranges::rend is ill-formed, and that can cause substitution failure when ranges::rend(t) shows up in the immediate context of a template instantiation. πŸ’€

πŸ’œ NOTES πŸ’œ

  • If the argument is an rvalue (i.e. T is an object type) and ranges::enable_borrowed_range<std::remove_cv_t<T>> is false, or if it is of an array type of unknown bound, the call to ranges::rend is ill-formed, which also results in substitution failure. πŸ™ˆ
  • If ranges::rend(std::forward<T>(t)) is valid, then decltype(ranges::rend(std::forward<T>(t))) and decltype(ranges::begin(std::forward<T>(t))) model std::sentinel_for in all cases, while T models std::ranges::range. πŸ‘‘
  • C++20 standard tea β˜•: it requires that if the underlying rend function call returns a prvalue, the return value is move-constructed from the materialized temporary object. All implementations directly return the prvalue instead. The requirement is corrected by the post-C++20 proposal P0849R8 to match the implementations. fr fr.

πŸ“ EXAMPLE πŸ“

// Run this code

#include <algorithm>
#include <iostream>
#include <ranges>
#include <vector>

int main()
{
    std::vector<int> v = {3, 1, 4};
    namespace ranges = std::ranges;
    if (ranges::find(ranges::rbegin(v), ranges::rend(v), 5) != ranges::rend(v))
        std::cout << "found a 5 in vector v!\n";

    int a[] = {5, 10, 15};
    if (ranges::find(ranges::rbegin(a), ranges::rend(a), 5) != ranges::rend(a))
        std::cout << "found a 5 in array a!\n";
}

Output:

found a 5 in array a!

πŸ’€ DEFECT REPORTS πŸ’€

so these behavior-changing defect reports got applied retroactively to C++ standards that were already published, which is giving time-travel bureaucracy 🍡:

  • P2602R2 β€” applied to C++20. Behavior as published: "there's machinery to prohibit certain non-member rend found by ADL". Correct behavior: "removed such machinery".

πŸ¦‹ SEE ALSO πŸ¦‹

  • ranges::crend (C++20) β€” returns a reverse end iterator to a read-only range (customization point object).
  • ranges::rbegin (C++20) β€” returns a reverse iterator to a range (customization point object).
  • rend β€” returns a reverse end iterator for a container or array (function template).
  • crend β€” (C++14), (function template).

okay that's the whole page, bestie πŸ«ΆπŸ’… β€” source/version http://cppreference.com, dated 2024.06.10, and yes this was std::ranges::rend(3). periodt ✨

πŸ“¦ where this page comes from