std::ranges::rend(3)
β¨ 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:
decay-copy(t.rend())(until C++23) orauto(t.rend())(since C++23) β if that expression is valid and its type modelsstd::sentinel_for<decltype(ranges::rbegin(std::declval<T>()))>.- Otherwise,
decay-copy(rend(t))(until C++23) orauto(rend(t))(since C++23) β ifTis a class or enumeration type, that expression is valid and its type modelsstd::sentinel_for<decltype(ranges::rbegin(std::declval<T>()))>, where the meaning ofrendis established as if by performing argument-dependent lookup only. - Otherwise,
std::make_reverse_iterator(ranges::begin(t))β if bothranges::begin(t)andranges::end(t)are valid expressions, have the same type, and that type modelsstd::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.
Tis an object type) andranges::enable_borrowed_range<std::remove_cv_t<T>>isfalse, or if it is of an array type of unknown bound, the call toranges::rendis ill-formed, which also results in substitution failure. π - If
ranges::rend(std::forward<T>(t))is valid, thendecltype(ranges::rend(std::forward<T>(t)))anddecltype(ranges::begin(std::forward<T>(t)))modelstd::sentinel_forin all cases, whileTmodelsstd::ranges::range. π - C++20 standard tea β: it requires that if the underlying
rendfunction 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 proposalP0849R8to 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-memberrendfound 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
- 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