std::ranges::rbegin(3)

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

πŸ’… std::ranges::rbegin(3) β€” the C++ Standard Library glow-up

okay bestie, so this is std::ranges::rbegin from the C++ Standard Library, and it's literally the cutest way to get an iterator pointing at the last element of your range. no cap. it lives in the <ranges> and <iterator> headers and it's been iconic since C++20. let's get into it ✨


✨ NAME ✨

std::ranges::rbegin β€” yes queen, the customization point object that hands you a reverse iterator to the end of your range. periodt.


πŸ¦‹ Synopsis πŸ¦‹

so here's the vibe. it's defined in header <ranges> and also in header <iterator>, and it's chilling inside an inline namespace /* unspecified */ like this:

inline constexpr /* unspecified */ rbegin = /* unspecified */;
(since C++20) β€” it's a customization point object, iykyk πŸ’…

the call signature πŸŽ€

template< class T >

    requires /* see below */                            (since C++20)

constexpr std::input_or_output_iterator auto rbegin(
T&& t );

it returns an iterator to the last element of the argument. that's the whole mood. 🌸

range-rbegin-rend.svg β€” yeah there's a diagram, it's giving visual learner energy.

⚠️ real talk though: if T is an array type and std::remove_all_extents_t<std::remove_reference_t<T>> is incomplete, then the call to ranges::rbegin is ill-formed, no diagnostic required. like, it just won't compile and nobody has to tell you why. rude but true. πŸ’€

🧚 the actual mechanics (aka the vibe check) 🧚

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

  1. decay-copy(t.rbegin()) (until C++23) / auto(t.rbegin()) (since C++23) β€” but only if that expression is valid and its type models std::input_or_output_iterator. slay. ✨
  2. otherwise, decay-copy(rbegin(t)) (until C++23) / auto(rbegin(t)) (since C++23) β€” but only if T is a class or enumeration type, that expression is valid, and its type models std::input_or_output_iterator, where the meaning of rbegin is established as if by performing argument-dependent lookup only. ADL queen πŸ‘‘
  3. otherwise, std::make_reverse_iterator(ranges::end(t)) β€” but only if both ranges::begin(t) and ranges::end(t) are valid expressions, have the same type, and that type models std::bidirectional_iterator. cute.

in all other cases, a call to ranges::rbegin is ill-formed, which can result in substitution failure when ranges::rbegin(t) appears in the immediate context of a template instantiation. no cap, that's just how it is. πŸ™ˆ


🌈 Notes (read these bestie) 🌈

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, the call to ranges::rbegin is ill-formed, which also results in substitution failure. so don't be passing temporaries unless you know they're borrowed. πŸ’…

the return type models std::input_or_output_iterator in all cases. always. every time. periodt.

and here's a lil lore drop: the C++20 standard requires that if the underlying rbegin 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. so like, the standard was being extra and the compilers were like "bestie no." 😭


πŸ“ Example πŸ“

// Run this code

#include <iostream>
#include <ranges>
#include <span>
#include <vector>

int main()
{
    std::vector<int> v = {3, 1, 4};
    auto vi = std::ranges::rbegin(v);
    std::cout << *vi << '\n';
    *vi = 42; // OK

    int a[] = {-5, 10, 15};
    auto ai = std::ranges::rbegin(a);
    std::cout << *ai << '\n';
    *ai = 42; // OK

    // auto x_x = std::ranges::rbegin(std::vector{6, 6, 6});
    // ill-formed: the argument is an rvalue (see Notes ↑)

    auto si = std::ranges::rbegin(std::span{a}); // OK
    static_assert(std::ranges::enable_borrowed_range<
        std::remove_cv_t<decltype(std::span{a})>>);
    *si = 42; // OK
}

Output:

4
15

see how it goes? *vi gives you 4, then *ai gives you 15. and you can assign through them because they're mutable. iconic behavior. πŸ’–


πŸ”₯ Defect reports πŸ”₯

the following behavior-changing defect reports were applied retroactively to previously published C++ standards. so like, they fixed the lore after the fact.

  • P2602R2 β€” applied to C++20. behavior as published: there's machinery to prohibit certain non-member rbegin found by ADL. correct behavior: removed such machinery. so they were like "actually no, let ADL cook." πŸ’€

🫢 See also 🫢

  • ranges::crbegin β€” returns a reverse iterator to a read-only range (C++20) (customization point object)
  • rbegin β€” returns a reverse iterator to the beginning of a container or array (function template)
  • crbegin β€” returns a reverse iterator to the beginning of a container or array (C++14) (function template)

okay that's the whole page bestie. go forth and reverse-iterate responsibly. and remember: if it's an rvalue and not a borrowed range, just don't. mwah πŸ’‹


source: http://cppreference.com Β· version date: 2024.06.10 Β· std::ranges::rbegin(3) β€” she ate, no crumbs πŸ‘‘βœ¨

πŸ“¦ where this page comes from