std::ranges...rowed_range(3)
π std::ranges::borrowed_range, std::ranges::enable_borrowed_range π
okay so real talk β this is the std::ranges...rowed_range(3) page from the C++ Standard Libary manual (yes that's how the header spells it, iconic typo tbh, we move), and it's about two things that are lowkey the same vibe: std::ranges::borrowed_range and std::ranges::enable_borrowed_range. Bestie, this one's a banger, let's go β¨
π NAME π
std::ranges::borrowed_rangestd::ranges::enable_borrowed_range
that's it, those two, they're a package deal π«Ά
β¨ Synopsis β¨
bestie it's literally defined in header <ranges>:
template< class R >
concept borrowed_range =
ranges::range<R> && (1)
(std::is_lvalue_reference_v<R> ||
ranges::enable_borrowed_range<std::remove_cvref_t<R>>);
template< class R > (2)
inline constexpr bool enable_borrowed_range = false;
both of these are (since C++20) π
- the concept
borrowed_rangedefines the requirements of a range such that a function can take it by value and return iterators obtained from it without danger of dangling β no cap, that's the whole point, it's the "trust me, the pointers stay valid after I'm gone" concept π¦ - the
enable_borrowed_rangevariable template is used to indicate whether a range is aborrowed_range. the primary template is defined asfalseβ so by default, bestie, you are not borrowed π
π Specializations π
okay this is the part where the standard library goes "these ones are safe fr fr". specializations of enable_borrowed_range for all specializations of the following standard templates are defined as true:
std::basic_string_viewstd::spanstd::ranges::subrangestd::ranges::ref_viewstd::ranges::empty_viewstd::ranges::iota_view
so iconic of them π
then, specialization of enable_borrowed_range for the following standard range adaptors are defined as true if and only if std::ranges::enable_borrowed_range<V> is true, where V is the underlying view type:
std::ranges::owning_viewstd::ranges::take_viewstd::ranges::drop_viewstd::ranges::drop_while_viewstd::ranges::common_viewstd::ranges::reverse_viewstd::ranges::elements_viewstd::ranges::adjacent_view(since C++23)
like, they inherit the borrowed-ness of whatever they wrap? the way they just pass the vibe check along π§
specialization for std::ranges::zip_view is defined as true if and only if (std::ranges::enable_borrowed_range<Vs> && ...) is true, (since C++23) where Vs... are all view types it adapts. so it's an all-or-nothing situation, every single one has to be borrowed or nobody is π₯
and now the part where YOU get to play god: a program may specializeenable_borrowed_rangetotruefor cv-unqualified program-defined types which modelborrowed_range, andfalsefor types which do not. Such specializations shall be usable in constant expression and have typeconst bool. β read that carefully bestie,const bool, notbool, and it has to work in a constant expression, don't @ me π€
πΈ Example πΈ
it demonstrates the specializations of enable_borrowed_range for program defined types. Such specializations protect against potentially dangling results. we love a safety feature π«Ά
(there's a // Run this code link on the page, go press it and watch it slay)
#include <algorithm>
#include <array>
#include <cstddef>
#include <iostream>
#include <ranges>
#include <span>
#include <type_traits>
template<typename T, std::size_t N>
struct MyRange : std::array<T, N> {};
template<typename T, std::size_t N>
inline constexpr bool std::ranges::enable_borrowed_range<MyRange<T, N>> = false;
template<typename T, std::size_t N>
struct MyBorrowedRange : std::span<T, N> {};
template<typename T, std::size_t N>
inline constexpr bool std::ranges::enable_borrowed_range<MyBorrowedRange<T, N>> = true;
int main()
{
static_assert(std::ranges::range<MyRange<int, 8>>);
static_assert(std::ranges::borrowed_range<MyRange<int, 8>> == false);
static_assert(std::ranges::range<MyBorrowedRange<int, 8>>);
static_assert(std::ranges::borrowed_range<MyBorrowedRange<int, 8>> == true);
auto getMyRangeByValue = []{ return MyRange<int, 4>{{1, 2, 42, 3}}; };
auto dangling_iter = std::ranges::max_element(getMyRangeByValue());
static_assert(std::is_same_v<std::ranges::dangling, decltype(dangling_iter)>);
// *dangling_iter; // compilation error (i.e. dangling protection works.)
auto my = MyRange<int, 4>{{1, 2, 42, 3}};
auto valid_iter = std::ranges::max_element(my);
std::cout << *valid_iter << ' '; // OK: 42
auto getMyBorrowedRangeByValue = []
{
static int sa[4]{1, 2, 42, 3};
return MyBorrowedRange<int, std::size(sa)>{sa};
};
auto valid_iter2 = std::ranges::max_element(getMyBorrowedRangeByValue());
std::cout << *valid_iter2 << '\n'; // OK: 42
}
and the commented-out *dangling_iter; line? that's the compilation error doing its job. dangling protection works, bestie. it ATE πβ¨
π Output π
42 42
both 42s. chef's kiss. no notes. π₯
π See also π
ranges::dangling(C++20) β a placeholder type indicating that an iterator or a subrange should not be returned since it would be dangling (class)
the bodyguard of the group chat honestly π«Ά
k that's the whole page, mwah π
shoutout to cppreference.com, dated 2024.06.10 β the source and version of this little masterpiece β catch you on the next manpage bestie, stay borrowed, stay safe, and never return an iterator to a temporary ππ
π¦ 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