std::ranges::rbegin(3)
π 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: ifTis an array type andstd::remove_all_extents_t<std::remove_reference_t<T>>is incomplete, then the call toranges::rbeginis 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:
decay-copy(t.rbegin())(until C++23) /auto(t.rbegin())(since C++23) β but only if that expression is valid and its type modelsstd::input_or_output_iterator. slay. β¨- otherwise,
decay-copy(rbegin(t))(until C++23) /auto(rbegin(t))(since C++23) β but only ifTis a class or enumeration type, that expression is valid, and its type modelsstd::input_or_output_iterator, where the meaning ofrbeginis established as if by performing argument-dependent lookup only. ADL queen π - otherwise,
std::make_reverse_iterator(ranges::end(t))β but only if bothranges::begin(t)andranges::end(t)are valid expressions, have the same type, and that type modelsstd::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.Tis an object type) andranges::enable_borrowed_range<std::remove_cv_t<T>>isfalse, the call toranges::rbeginis 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-memberrbeginfound 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
- 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