|
Range primitives | |||||||
|
Range concepts | |||||||||||||||||||
|
Range factories | |||||||||
|
Range adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
Helper items | |||||||||||||||||
|
Defined in header <ranges> | ||
Defined in header <iterator> | ||
inlinenamespace/* unspecified */{ inlineconstexpr/* unspecified */ rbegin=/* unspecified */; | (since C++20) (customization point object) | |
Call signature | ||
template<class T> requires/* see below */ | (since C++20) | |
Returns an iterator to the last element of the argument.
IfT
is an array type andstd::remove_all_extents_t<std::remove_reference_t<T>> is incomplete, then the call toranges::rbegin
is ill-formed, no diagnostic required.
If the argument is an lvalue orranges::enable_borrowed_range<std::remove_cv_t<T>> istrue, then a call toranges::rbegin
isexpression-equivalent to:
T
is a class or enumeration type, that expression is valid and its type modelsstd::input_or_output_iterator, where the meaning ofrbegin
is established as if by performingargument-dependent lookup only.In all other cases, a call toranges::rbegin
is ill-formed, which can result insubstitution failure whenranges::rbegin(t) appears in the immediate context of a template instantiation.
Contents |
The nameranges::rbegin
denotes acustomization point object, which is a constfunction object of aliteralsemiregular
class type. SeeCustomizationPointObject for details.
If the argument is an rvalue (i.e.T
is an object type) andranges::enable_borrowed_range<std::remove_cv_t<T>> isfalse, the call toranges::rbegin
is ill-formed, which also results in substitution failure.
The return type modelsstd::input_or_output_iterator in all cases.
The C++20 standard requires that if the underlyingrbegin
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 proposalP0849R8 to match the implementations.
#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:
415
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
P2602R2 | C++20 | there's machinery to prohibit certain non-memberrbegin found byADL | removed such machinery |
(C++20) | returns a reverse iterator to a read-only range (customization point object)[edit] |
(C++14) | returns a reverse iterator to the beginning of a container or array (function template)[edit] |