|
Range primitives | |||||||
|
Range concepts | ||||||||||||||||||||||||||||||||||
|
|
Range factories | |||||||||
|
Range adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
Helper items | |||||||||||||||||
|
Defined in header <ranges> | ||
template<class R> concept borrowed_range= | (1) | (since C++20) |
template<class R> constexprbool enable_borrowed_range=false; | (2) | (since C++20) |
borrowed_range
defines the requirements of a range such that a function can take it by value and return iterators obtained from it without danger of dangling.enable_borrowed_range
variable template is used to indicate whether arange
is aborrowed_range
. The primary template is defined asfalse.Contents |
LetU
bestd::remove_reference_t<T> ifT
is an rvalue reference type, andT
otherwise. Given a variableu of typeU
,T
modelsborrowed_range
only if the validity of iterators obtained fromu is not tied to the lifetime of that variable.
A program may specializeenable_borrowed_range
totrue for cv-unqualifiedprogram-defined types which modelborrowed_range
, andfalse for types which do not. Such specializations shall be usable inconstant expression and have typeconstbool.
Specializations ofenable_borrowed_range
for all specializations of the following standard templates are defined astrue:
Specialization ofenable_borrowed_range
for the following standard range adaptors are defined astrue if and only ifstd::ranges::enable_borrowed_range<V> istrue, whereV
is the underlying view type:
(since C++23) |
(since C++23) |
(since C++23) |
(since C++26) |
V
must also satisfyforward_range
.Specialization of | (since C++23) |
Demonstrates the specializations ofenable_borrowed_range
for program defined types. Such specializations protect against potentially dangling results.
#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>constexprbool 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>constexprbool 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=[]{staticint 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}
Output:
42 42
(C++20) | a placeholder type indicating that an iterator or asubrange should not be returned since it would be dangling(class)[edit] |