From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1725) id 3FB95386EC7E; Tue, 18 Aug 2020 18:03:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3FB95386EC7E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1597773801; bh=f2//+LrDPaW2lFgzVz/uPgjrWC8IAHB1HrMh8ou9cyg=; h=From:To:Subject:Date:From; b=JSa39e/BERfjXgsp+Jiu+CzNOoFTPjbSMJEZX9cZkBldgyIqpXrTr87vCTc6Y4PrN jIn0Y+weYC8I67ejZr20tuUN2G+owgiceawMNEwmc56n2IVvU6iDRXe5MW3pRADwL6 T3f+zO/1cDiMtcdo+Ho56fy39x76IliUaEz1dvC8= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: William Schmidt To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc(refs/users/wschmidt/heads/builtins3)] libstdc++: Constrain reverse_iterator and move_iterator conversions [LWG 3435] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/users/wschmidt/heads/builtins3 X-Git-Oldrev: 259c3965b1ba04f7ee022846af6173fb1c343bc8 X-Git-Newrev: a5a8a4e61565a2a66391e29eb80813c581b7dc52 Message-Id: <20200818180321.3FB95386EC7E@sourceware.org> Date: Tue, 18 Aug 2020 18:03:21 +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: Tue, 18 Aug 2020 18:03:21 -0000 https://gcc.gnu.org/g:a5a8a4e61565a2a66391e29eb80813c581b7dc52 commit a5a8a4e61565a2a66391e29eb80813c581b7dc52 Author: Jonathan Wakely Date: Wed Jul 22 13:25:11 2020 +0100 libstdc++: Constrain reverse_iterator and move_iterator conversions [LWG 3435] libstdc++-v3/ChangeLog: * include/bits/stl_iterator.h (reverse_iterator): Constrain converting constructor and converting assignment operator. Access source iterator's data member directly instead of calling base(). (move_iterator): Likewise. * testsuite/24_iterators/move_iterator/dr3435.cc: New test. * testsuite/24_iterators/reverse_iterator/dr3435.cc: New test. Diff: --- libstdc++-v3/include/bits/stl_iterator.h | 55 ++++++++++++++++++++-- .../testsuite/24_iterators/move_iterator/dr3435.cc | 37 +++++++++++++++ .../24_iterators/reverse_iterator/dr3435.cc | 36 ++++++++++++++ 3 files changed, 125 insertions(+), 3 deletions(-) diff --git a/libstdc++-v3/include/bits/stl_iterator.h b/libstdc++-v3/include/bits/stl_iterator.h index 6d2d19eb068..60bb40a659f 100644 --- a/libstdc++-v3/include/bits/stl_iterator.h +++ b/libstdc++-v3/include/bits/stl_iterator.h @@ -129,6 +129,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION typename iterator_traits<_Iterator>::pointer, typename iterator_traits<_Iterator>::reference> { +#if __cplusplus >= 201103L + template + friend class reverse_iterator; +#endif + +#if __cpp_lib_concepts + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 3435. three_way_comparable_with, [...]> + template + static constexpr bool __convertible = !is_same_v<_Iter, _Iterator> + && convertible_to; +#endif + protected: _Iterator current; @@ -182,9 +195,27 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * underlying %iterator can be converted to the type of @c current. */ template +#if __cpp_lib_concepts + requires __convertible<_Iter> +#endif _GLIBCXX17_CONSTEXPR reverse_iterator(const reverse_iterator<_Iter>& __x) - : current(__x.base()) { } + : current(__x.current) { } + +#if __cplusplus >= 201103L + template +#if __cpp_lib_concepts + requires __convertible<_Iter> + && assignable_from<_Iterator&, const _Iter&> +#endif + _GLIBCXX17_CONSTEXPR + reverse_iterator& + operator=(const reverse_iterator<_Iter>& __x) + { + current = __x.current; + return *this; + } +#endif /** * @return @c current, the %iterator used for underlying work. @@ -1270,6 +1301,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION using __base_ref = typename __traits_type::reference; #endif + template + friend class move_iterator; + +#if __cpp_lib_concepts + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 3435. three_way_comparable_with, [...]> + template + static constexpr bool __convertible = !is_same_v<_Iter2, _Iterator> + && convertible_to; +#endif + public: using iterator_type = _Iterator; @@ -1303,15 +1345,22 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION : _M_current(std::move(__i)) { } template +#if __cpp_lib_concepts + requires __convertible<_Iter> +#endif _GLIBCXX17_CONSTEXPR move_iterator(const move_iterator<_Iter>& __i) - : _M_current(__i.base()) { } + : _M_current(__i._M_current) { } template +#if __cpp_lib_concepts + requires __convertible<_Iter> + && assignable_from<_Iterator&, const _Iter&> +#endif _GLIBCXX17_CONSTEXPR move_iterator& operator=(const move_iterator<_Iter>& __i) { - _M_current = __i.base(); + _M_current = __i._M_current; return *this; } diff --git a/libstdc++-v3/testsuite/24_iterators/move_iterator/dr3435.cc b/libstdc++-v3/testsuite/24_iterators/move_iterator/dr3435.cc new file mode 100644 index 00000000000..170d4977df1 --- /dev/null +++ b/libstdc++-v3/testsuite/24_iterators/move_iterator/dr3435.cc @@ -0,0 +1,37 @@ +// 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 +#include + +// LWG 3435. three_way_comparable_with, reverse_iterator> +// and +// LWG 3265. move_iterator's conversions are more broken after P1207 + +using RI = std::move_iterator; +using CRI = std::move_iterator; + +static_assert( std::three_way_comparable_with ); + +static_assert( std::is_convertible_v ); +static_assert( ! std::is_convertible_v ); + +static_assert( std::is_assignable_v ); +static_assert( ! std::is_assignable_v ); diff --git a/libstdc++-v3/testsuite/24_iterators/reverse_iterator/dr3435.cc b/libstdc++-v3/testsuite/24_iterators/reverse_iterator/dr3435.cc new file mode 100644 index 00000000000..77b0f39c535 --- /dev/null +++ b/libstdc++-v3/testsuite/24_iterators/reverse_iterator/dr3435.cc @@ -0,0 +1,36 @@ +// 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 +#include + +// LWG 3435. +// three_way_comparable_with, reverse_iterator> + +using RI = std::reverse_iterator; +using CRI = std::reverse_iterator; + +static_assert( std::three_way_comparable_with ); + +static_assert( std::is_convertible_v ); +static_assert( ! std::is_convertible_v ); + +static_assert( std::is_assignable_v ); +static_assert( ! std::is_assignable_v );