From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2140) id 27DBF3985827; Thu, 3 Sep 2020 16:03:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 27DBF3985827 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1599148989; bh=T6FjOnaEaWtfeNUn5gu2rTdTOhusosRM39MRJWibbPY=; h=From:To:Subject:Date:From; b=PRM6fkYMRqZl5n0OLS1Cjx7Td+2vWa1No2LscIueAH6FHHyY7z4ENhgB6bzgEqjg1 QPBrVW83V13oZVH9KfxY4918IZFjkU9q8f+cMYjxaYYI4jisrxUAG3RiJSn/zwvm65 iCHP3ZV5On96XobXQCFCJYOp09Po0WjTfPl6CVoc= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Alexandre Oliva To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc(refs/users/aoliva/heads/testme)] libstdc++: elements_view's sentinel and iterator not comparable [LWG 3406] X-Act-Checkin: gcc X-Git-Author: Patrick Palka X-Git-Refname: refs/users/aoliva/heads/testme X-Git-Oldrev: 97ab5daa6c1186d3b10872cc1d5b05da247d102c X-Git-Newrev: 4be16d1c1cfa6d151d2853ce509c1a797189c9ad Message-Id: <20200903160309.27DBF3985827@sourceware.org> Date: Thu, 3 Sep 2020 16:03:09 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 03 Sep 2020 16:03:09 -0000 https://gcc.gnu.org/g:4be16d1c1cfa6d151d2853ce509c1a797189c9ad commit 4be16d1c1cfa6d151d2853ce509c1a797189c9ad Author: Patrick Palka Date: Wed Aug 26 21:52:58 2020 -0400 libstdc++: elements_view's sentinel and iterator not comparable [LWG 3406] This implements the proposed resolution for LWG 3406, and adds a testcase for the example from P1994R1. libstdc++-v3/ChangeLog: * include/std/ranges (elements_view::begin): Adjust constraints. (elements_view::end): Likewise. (elements_view::_Sentinel::operator==): Templatize to take both _Iterator and _Iterator. (elements_view::_Sentinel::operator-): Likewise. * testsuite/std/ranges/adaptors/elements.cc: Add testcase for the example from P1994R1. * testsuite/std/ranges/adaptors/lwg3406.cc: New test. Diff: --- libstdc++-v3/include/std/ranges | 39 ++++++++++++--------- .../testsuite/std/ranges/adaptors/elements.cc | 22 ++++++++++++ .../testsuite/std/ranges/adaptors/lwg3406.cc | 40 ++++++++++++++++++++++ 3 files changed, 85 insertions(+), 16 deletions(-) diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges index efa8d2cf9f4..42028354b81 100644 --- a/libstdc++-v3/include/std/ranges +++ b/libstdc++-v3/include/std/ranges @@ -3362,15 +3362,15 @@ namespace views { return _Iterator(ranges::begin(_M_base)); } constexpr auto - begin() const requires __detail::__simple_view<_Vp> + begin() const requires range { return _Iterator(ranges::begin(_M_base)); } constexpr auto - end() + end() requires (!__detail::__simple_view<_Vp> && !common_range<_Vp>) { return _Sentinel{ranges::end(_M_base)}; } constexpr auto - end() requires common_range<_Vp> + end() requires (!__detail::__simple_view<_Vp> && common_range<_Vp>) { return _Iterator{ranges::end(_M_base)}; } constexpr auto @@ -3576,19 +3576,26 @@ namespace views base() const { return _M_end; } - friend constexpr bool - operator==(const _Iterator<_Const>& __x, const _Sentinel& __y) - { return __y._M_equal(__x); } - - friend constexpr range_difference_t<_Base> - operator-(const _Iterator<_Const>& __x, const _Sentinel& __y) - requires sized_sentinel_for, iterator_t<_Base>> - { return __x._M_current - __y._M_end; } - - friend constexpr range_difference_t<_Base> - operator-(const _Sentinel& __x, const _Iterator<_Const>& __y) - requires sized_sentinel_for, iterator_t<_Base>> - { return __x._M_end - __y._M_current; } + template + requires sentinel_for, + iterator_t<__detail::__maybe_const_t<_Const2, _Vp>>> + friend constexpr bool + operator==(const _Iterator<_Const2>& __x, const _Sentinel& __y) + { return __y._M_equal(__x); } + + template> + requires sized_sentinel_for, iterator_t<_Base2>> + friend constexpr range_difference_t<_Base2> + operator-(const _Iterator<_Const2>& __x, const _Sentinel& __y) + { return __x._M_current - __y._M_end; } + + template> + requires sized_sentinel_for, iterator_t<_Base2>> + friend constexpr range_difference_t<_Base> + operator-(const _Sentinel& __x, const _Iterator<_Const2>& __y) + { return __x._M_end - __y._M_current; } friend _Sentinel; }; diff --git a/libstdc++-v3/testsuite/std/ranges/adaptors/elements.cc b/libstdc++-v3/testsuite/std/ranges/adaptors/elements.cc index d846c4cf33e..3026adf4f28 100644 --- a/libstdc++-v3/testsuite/std/ranges/adaptors/elements.cc +++ b/libstdc++-v3/testsuite/std/ranges/adaptors/elements.cc @@ -45,8 +45,30 @@ test01() VERIFY( ranges::equal(v1, x | views::values) ); } +struct S +{ + friend bool + operator==(std::input_iterator auto const& i, S) + { return std::get<1>(*i) == 0; } +}; + +void +test02() +{ + // This verifies that P1994R1 (and LWG3406) is implemented. + std::pair, long> x[] + = {{{1,2},3l}, {{1,0},2l}, {{1,2},0l}}; + ranges::subrange r{ranges::begin(x), S{}}; + + auto v = r | views::keys; + VERIFY( ranges::equal(v, (std::pair[]){{1,2},{1,0}}) ); + ranges::subrange v2{ranges::begin(v), S{}}; + VERIFY( ranges::equal(v2, (std::pair[]){{1,2}}) ); +} + int main() { test01(); + test02(); } diff --git a/libstdc++-v3/testsuite/std/ranges/adaptors/lwg3406.cc b/libstdc++-v3/testsuite/std/ranges/adaptors/lwg3406.cc new file mode 100644 index 00000000000..72190cc2e8a --- /dev/null +++ b/libstdc++-v3/testsuite/std/ranges/adaptors/lwg3406.cc @@ -0,0 +1,40 @@ +// 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 compile { target c++2a } } + +#include + +namespace ranges = std::ranges; +namespace views = std::views; + +void +test01() +{ + std::tuple x[] = {{0,0}}; + auto v = x | views::elements<0>; + bool b = ranges::cbegin(v) == ranges::end(v); +} + +void +test02() +{ + std::tuple x = {0, 1}; + auto v = views::single(x) | views::elements<0>; + auto i = ranges::cbegin(v); +}