public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r10-9574] libstdc++: Implement P2017R1 "Conditionally borrowed ranges"
@ 2021-03-29 20:01 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2021-03-29 20:01 UTC (permalink / raw)
  To: gcc-cvs, libstdc++-cvs

https://gcc.gnu.org/g:f3302108ea88ca277bd6a0d5ebfac2f471b0ceb5

commit r10-9574-gf3302108ea88ca277bd6a0d5ebfac2f471b0ceb5
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Oct 30 18:39:43 2020 +0000

    libstdc++: Implement P2017R1 "Conditionally borrowed ranges"
    
    This makes some range adaptors model the borrowed_range concept if they
    are adapting a borrowed range. This hasn't been added to the C++23
    working paper yet, but it has been approved by LWG, and the
    recommendation is to treat it as a defect report for C++20 as well.
    
    libstdc++-v3/ChangeLog:
    
            * include/std/ranges (enable_borrowed_view<take_view<T>>)
            (enable_borrowed_view<drop_view<T>>)
            (enable_borrowed_view<drop_while_view<T>>)
            (enable_borrowed_view<reverse_view<T>>)
            (enable_borrowed_view<common_view<T>>)
            (enable_borrowed_view<elements_view<T>>): Add partial
            specializations as per P2017R1.
            * testsuite/std/ranges/adaptors/conditionally_borrowed.cc:
            New test.
    
    (cherry picked from commit 39bf4f14fc75e14aafc4ba8a53a34775f29b743a)

Diff:
---
 libstdc++-v3/include/std/ranges                    | 24 +++++++
 .../std/ranges/adaptors/conditionally_borrowed.cc  | 75 ++++++++++++++++++++++
 2 files changed, 99 insertions(+)

diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index 78dacbe5ec7..a8cc56eb0ec 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -2145,6 +2145,10 @@ namespace views
     take_view(_Range&&, range_difference_t<_Range>)
       -> take_view<views::all_t<_Range>>;
 
+  template<typename _Tp>
+    inline constexpr bool enable_borrowed_range<take_view<_Tp>>
+      = enable_borrowed_range<_Tp>;
+
   namespace views
   {
     inline constexpr __adaptor::_RangeAdaptor take
@@ -2346,6 +2350,10 @@ namespace views
     drop_view(_Range&&, range_difference_t<_Range>)
       -> drop_view<views::all_t<_Range>>;
 
+  template<typename _Tp>
+    inline constexpr bool enable_borrowed_range<drop_view<_Tp>>
+      = enable_borrowed_range<_Tp>;
+
   namespace views
   {
     inline constexpr __adaptor::_RangeAdaptor drop
@@ -2407,6 +2415,10 @@ namespace views
     drop_while_view(_Range&&, _Pred)
       -> drop_while_view<views::all_t<_Range>, _Pred>;
 
+  template<typename _Tp, typename _Pred>
+    inline constexpr bool enable_borrowed_range<drop_while_view<_Tp, _Pred>>
+      = enable_borrowed_range<_Tp>;
+
   namespace views
   {
     inline constexpr __adaptor::_RangeAdaptor drop_while
@@ -3231,6 +3243,10 @@ namespace views
   template<typename _Range>
     common_view(_Range&&) -> common_view<views::all_t<_Range>>;
 
+  template<typename _Tp>
+    inline constexpr bool enable_borrowed_range<common_view<_Tp>>
+      = enable_borrowed_range<_Tp>;
+
   namespace views
   {
     inline constexpr __adaptor::_RangeAdaptorClosure common
@@ -3316,6 +3332,10 @@ namespace views
   template<typename _Range>
     reverse_view(_Range&&) -> reverse_view<views::all_t<_Range>>;
 
+  template<typename _Tp>
+    inline constexpr bool enable_borrowed_range<reverse_view<_Tp>>
+      = enable_borrowed_range<_Tp>;
+
   namespace views
   {
     namespace __detail
@@ -3641,6 +3661,10 @@ namespace views
       _Vp _M_base = _Vp();
     };
 
+  template<typename _Tp, size_t _Nm>
+    inline constexpr bool enable_borrowed_range<elements_view<_Tp, _Nm>>
+      = enable_borrowed_range<_Tp>;
+
   template<typename _Range>
     using keys_view = elements_view<views::all_t<_Range>, 0>;
 
diff --git a/libstdc++-v3/testsuite/std/ranges/adaptors/conditionally_borrowed.cc b/libstdc++-v3/testsuite/std/ranges/adaptors/conditionally_borrowed.cc
new file mode 100644
index 00000000000..98398ff3b0a
--- /dev/null
+++ b/libstdc++-v3/testsuite/std/ranges/adaptors/conditionally_borrowed.cc
@@ -0,0 +1,75 @@
+// Copyright (C) 2020 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+#include <ranges>
+#include <algorithm>
+#include <string>
+#include <cctype>
+#include <testsuite_hooks.h>
+
+namespace ranges = std::ranges;
+namespace views = std::views;
+using namespace std::literals;
+
+// P2017R1 "Conditionally borrowed ranges"
+auto trim(std::string const& s) {
+    auto isalpha = [](unsigned char c){ return std::isalpha(c); };
+    auto b = ranges::find_if(s, isalpha);
+    auto e = ranges::find_if(s | views::reverse, isalpha).base();
+    return ranges::subrange(b, e);
+}
+
+void
+test01()
+{
+  VERIFY( ranges::equal( trim("  abc    "), "abc"sv ) );
+
+  std::string s = "0123456789";
+  auto odd = [](char c){ return (c - '0') % 2; };
+  using namespace std::views;
+  auto pos = ranges::find(s | take(5) | drop(1) | drop_while(odd), '2');
+  VERIFY( *pos == '2' );
+
+  static_assert( ranges::borrowed_range<decltype(s | reverse)> );
+  static_assert( ranges::borrowed_range<decltype(s | take(1))> );
+  static_assert( ranges::borrowed_range<decltype(s | drop(1))> );
+  static_assert( ranges::borrowed_range<decltype(s | drop_while(odd))> );
+
+  ranges::subrange r(s.begin(), s.cend());
+  static_assert( !ranges::common_range<decltype(r)> );
+  auto pos2 = ranges::find(r | views::common, '2');
+  VERIFY( *pos2 == '2' );
+}
+
+void
+test02()
+{
+  std::pair<int, std::string_view> a[2]{ {1,"two"}, {3,"four"}};
+  auto pos = ranges::find(a | views::values, "four");
+  VERIFY( *pos == "four" );
+
+  static_assert( ranges::borrowed_range<decltype(a | views::keys)> );
+}
+
+int main()
+{
+  test01();
+  test02();
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-03-29 20:01 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-29 20:01 [gcc r10-9574] libstdc++: Implement P2017R1 "Conditionally borrowed ranges" Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).