std::ranges::rbegin

Header: <ranges>

Returns an iterator to the last element of the argument.

# Declarations

inline namespace /* unspecified */ {
inline constexpr /* unspecified */ rbegin = /* unspecified */;
}

(since C++20) (customization point object)

Call signature
template< class T >
requires /* see below */
constexpr std::input_or_output_iterator auto rbegin( T&& t );

(since C++20)

# Notes

If the argument is an rvalue (i.e. T is an object type) and ranges::enable_borrowed_range<std::remove_cv_t> is false, the call to ranges::rbegin is ill-formed, which also results in substitution failure.

The return type models std::input_or_output_iterator in all cases.

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.

# Example

#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
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
P2602R2C++20there’s machinery to prohibit certain non-member rbegin found by ADLremoved such machinery

# See also