Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::borrowed_range,std::ranges::enable_borrowed_range

      From cppreference.com
      <cpp‎ |ranges
       
       
      Ranges library
      Range adaptors
       
      Defined in header<ranges>
      template<class R>

      concept borrowed_range=
         ranges::range<R>&&
         (std::is_lvalue_reference_v<R>||

           ranges::enable_borrowed_range<std::remove_cvref_t<R>>);
      (1)(since C++20)
      template<class R>
      constexprbool enable_borrowed_range=false;
      (2)(since C++20)
      1) The conceptborrowed_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.
      2) Theenable_borrowed_range variable template is used to indicate whether arange is aborrowed_range. The primary template is defined asfalse.

      Contents

      [edit]Semantic requirements

      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.

      [edit]Specializations

      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.

      [edit]Unconditionally borrowed ranges in the standard library

      Specializations ofenable_borrowed_range for all specializations of the following standard templates are defined astrue:

      [edit]Conditionally borrowed ranges in the standard library

      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)
      1. The underlying viewV must also satisfyforward_range.

      Specialization ofenable_borrowed_range for the following standard range adaptors are defined astrue if and only if(std::ranges::enable_borrowed_range<Vs>&& ...) istrue, whereVs... are all view types it adapts:

      (since C++23)

      [edit]Example

      Demonstrates the specializations ofenable_borrowed_range for program defined types. Such specializations protect against potentially dangling results.

      Run this code
      #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

      [edit]See also

      a placeholder type indicating that an iterator or asubrange should not be returned since it would be dangling
      (class)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/ranges/borrowed_range&oldid=181452"

      [8]ページ先頭

      ©2009-2025 Movatter.jp