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 [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 97771385802A for ; Thu, 9 Dec 2021 23:31:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 97771385802A Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-590-FR2__HG6Mrix64_TGqAX6A-1; Thu, 09 Dec 2021 18:31:10 -0500 X-MC-Unique: FR2__HG6Mrix64_TGqAX6A-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7554E1023F4E; Thu, 9 Dec 2021 23:31:09 +0000 (UTC) Received: from localhost (unknown [10.33.36.71]) by smtp.corp.redhat.com (Postfix) with ESMTP id D85E819C59; Thu, 9 Dec 2021 23:31:08 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix ambiguous comparisons for iterators in C++20 Date: Thu, 9 Dec 2021 23:31:07 +0000 Message-Id: <20211209233107.1569706-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII" X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Thu, 09 Dec 2021 23:31:15 -0000 Tested powerpc64le-linux, pushed to trunk. Since r11-1571 (c++: Refinements to "more constrained") was changed in the front end, the following comment from stl_iterator.h stopped being true: // These extra overloads are not needed in C++20, because the ones above // are constrained with a requires-clause and so overload resolution will // prefer them to greedy unconstrained function templates. The requires-clause is no longer considered when comparing unrelated function templates. That means that the constrained operator== specified in the standard is no longer more constrained than the pathological comparison operators defined in the testsuite_greedy_ops.h header. This was causing several tests to FAIL in C++20 mode: FAIL: 23_containers/deque/types/1.cc (test for excess errors) FAIL: 23_containers/vector/types/1.cc (test for excess errors) FAIL: 24_iterators/move_iterator/greedy_ops.cc (test for excess errors) FAIL: 24_iterators/normal_iterator/greedy_ops.cc (test for excess errors) FAIL: 24_iterators/reverse_iterator/greedy_ops.cc (test for excess errors) The solution is to restore some of the non-standard comparison operators that are more specialized than the greedy operators in the testsuite. libstdc++-v3/ChangeLog: * include/bits/stl_iterator.h (operator==, operator<=>): Define overloads for homogeneous specializations of reverse_iterator, __normal_iterator and move_iterator. --- libstdc++-v3/include/bits/stl_iterator.h | 50 +++++++++++++++++++++--- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/libstdc++-v3/include/bits/stl_iterator.h b/libstdc++-v3/include/bits/stl_iterator.h index f6504ece560..6bd860b803e 100644 --- a/libstdc++-v3/include/bits/stl_iterator.h +++ b/libstdc++-v3/include/bits/stl_iterator.h @@ -574,6 +574,24 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION operator<=>(const reverse_iterator<_IteratorL>& __x, const reverse_iterator<_IteratorR>& __y) { return __y.base() <=> __x.base(); } + + // Additional, non-standard overloads to avoid ambiguities with greedy, + // unconstrained overloads in associated namespaces. + + template + [[nodiscard]] + constexpr bool + operator==(const reverse_iterator<_Iterator>& __x, + const reverse_iterator<_Iterator>& __y) + requires requires { { __x.base() == __y.base() } -> convertible_to; } + { return __x.base() == __y.base(); } + + template + [[nodiscard]] + constexpr compare_three_way_result_t<_Iterator, _Iterator> + operator<=>(const reverse_iterator<_Iterator>& __x, + const reverse_iterator<_Iterator>& __y) + { return __y.base() <=> __x.base(); } #endif // C++20 ///@} @@ -1161,6 +1179,25 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION const __normal_iterator<_IteratorR, _Container>& __rhs) noexcept(noexcept(std::__detail::__synth3way(__lhs.base(), __rhs.base()))) { return std::__detail::__synth3way(__lhs.base(), __rhs.base()); } + + template + [[nodiscard]] + constexpr bool + operator==(const __normal_iterator<_Iterator, _Container>& __lhs, + const __normal_iterator<_Iterator, _Container>& __rhs) + noexcept(noexcept(__lhs.base() == __rhs.base())) + requires requires { + { __lhs.base() == __rhs.base() } -> std::convertible_to; + } + { return __lhs.base() == __rhs.base(); } + + template + [[nodiscard]] + constexpr std::__detail::__synth3way_t<_Iterator> + operator<=>(const __normal_iterator<_Iterator, _Container>& __lhs, + const __normal_iterator<_Iterator, _Container>& __rhs) + noexcept(noexcept(std::__detail::__synth3way(__lhs.base(), __rhs.base()))) + { return std::__detail::__synth3way(__lhs.base(), __rhs.base()); } #else // Forward iterator requirements template @@ -1689,14 +1726,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #endif { return !(__x < __y); } -#if ! (__cplusplus > 201703L && __cpp_lib_concepts) // Note: See __normal_iterator operators note from Gaby to understand // why we have these extra overloads for some move_iterator operators. - // These extra overloads are not needed in C++20, because the ones above - // are constrained with a requires-clause and so overload resolution will - // prefer them to greedy unconstrained function templates. - template [[__nodiscard__]] inline _GLIBCXX17_CONSTEXPR bool @@ -1704,6 +1736,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION const move_iterator<_Iterator>& __y) { return __x.base() == __y.base(); } +#if __cpp_lib_three_way_comparison + template + [[__nodiscard__]] + constexpr compare_three_way_result_t<_Iterator> + operator<=>(const move_iterator<_Iterator>& __x, + const move_iterator<_Iterator>& __y) + { return __x.base() <=> __y.base(); } +#else template [[__nodiscard__]] inline _GLIBCXX17_CONSTEXPR bool -- 2.31.1