From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id B33A5385DC37 for ; Fri, 30 Oct 2020 23:57:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org B33A5385DC37 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-343-_6gobUMRP-ydQRrjBu-86g-1; Fri, 30 Oct 2020 19:57:31 -0400 X-MC-Unique: _6gobUMRP-ydQRrjBu-86g-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0A0A2802B46; Fri, 30 Oct 2020 23:57:30 +0000 (UTC) Received: from localhost (unknown [10.33.36.7]) by smtp.corp.redhat.com (Postfix) with ESMTP id 84E396716C; Fri, 30 Oct 2020 23:57:29 +0000 (UTC) Date: Fri, 30 Oct 2020 23:57:28 +0000 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Implement P2017R1 "Conditionally borrowed ranges" Message-ID: <20201030235728.GA2673862@redhat.com> MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="LZvS9be/3tNcYl/X" Content-Disposition: inline X-Spam-Status: No, score=-14.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 30 Oct 2020 23:57:35 -0000 --LZvS9be/3tNcYl/X Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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>) (enable_borrowed_view>) (enable_borrowed_view>) (enable_borrowed_view>) (enable_borrowed_view>) (enable_borrowed_view>): Add partial specializations as per P2017R1. * testsuite/std/ranges/adaptors/conditionally_borrowed.cc: New test. Tested powerpc64le-linux. Committed to trunk. --LZvS9be/3tNcYl/X Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="patch.txt" commit 39bf4f14fc75e14aafc4ba8a53a34775f29b743a Author: Jonathan Wakely Date: Fri Oct 30 18:39:43 2020 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>) (enable_borrowed_view>) (enable_borrowed_view>) (enable_borrowed_view>) (enable_borrowed_view>) (enable_borrowed_view>): Add partial specializations as per P2017R1. * testsuite/std/ranges/adaptors/conditionally_borrowed.cc: New test. diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges index 610083167d89..bc7bb05b0050 100644 --- a/libstdc++-v3/include/std/ranges +++ b/libstdc++-v3/include/std/ranges @@ -1810,6 +1810,10 @@ namespace views take_view(_Range&&, range_difference_t<_Range>) -> take_view>; + template + inline constexpr bool enable_borrowed_range> + = enable_borrowed_range<_Tp>; + namespace views { inline constexpr __adaptor::_RangeAdaptor take @@ -2010,6 +2014,10 @@ namespace views drop_view(_Range&&, range_difference_t<_Range>) -> drop_view>; + template + inline constexpr bool enable_borrowed_range> + = enable_borrowed_range<_Tp>; + namespace views { inline constexpr __adaptor::_RangeAdaptor drop @@ -2071,6 +2079,10 @@ namespace views drop_while_view(_Range&&, _Pred) -> drop_while_view, _Pred>; + template + inline constexpr bool enable_borrowed_range> + = enable_borrowed_range<_Tp>; + namespace views { inline constexpr __adaptor::_RangeAdaptor drop_while @@ -2891,6 +2903,10 @@ namespace views template common_view(_Range&&) -> common_view>; + template + inline constexpr bool enable_borrowed_range> + = enable_borrowed_range<_Tp>; + namespace views { inline constexpr __adaptor::_RangeAdaptorClosure common @@ -2976,6 +2992,10 @@ namespace views template reverse_view(_Range&&) -> reverse_view>; + template + inline constexpr bool enable_borrowed_range> + = enable_borrowed_range<_Tp>; + namespace views { namespace __detail @@ -3301,6 +3321,10 @@ namespace views _Vp _M_base = _Vp(); }; + template + inline constexpr bool enable_borrowed_range> + = enable_borrowed_range<_Tp>; + template using keys_view = elements_view, 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 000000000000..98398ff3b0a7 --- /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 +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } + +#include +#include +#include +#include +#include + +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 ); + static_assert( ranges::borrowed_range ); + static_assert( ranges::borrowed_range ); + static_assert( ranges::borrowed_range ); + + ranges::subrange r(s.begin(), s.cend()); + static_assert( !ranges::common_range ); + auto pos2 = ranges::find(r | views::common, '2'); + VERIFY( *pos2 == '2' ); +} + +void +test02() +{ + std::pair a[2]{ {1,"two"}, {3,"four"}}; + auto pos = ranges::find(a | views::values, "four"); + VERIFY( *pos == "four" ); + + static_assert( ranges::borrowed_range ); +} + +int main() +{ + test01(); + test02(); +} --LZvS9be/3tNcYl/X--