From patchwork Wed Aug 21 20:27:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1975095 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=foNzCXe3; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Wpyb34GSYz1yXY for ; Thu, 22 Aug 2024 06:28:31 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 70889386F478 for ; Wed, 21 Aug 2024 20:28:29 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 173FF385DDFF for ; Wed, 21 Aug 2024 20:28:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 173FF385DDFF Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 173FF385DDFF Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724272088; cv=none; b=LSeagQ6UNCXUO4S4auvlRLbwMbO83VW17vFDETZ8A/O3qBzGZDoprfb882chiVufYUD8SoLXoflCpAQy8a7hkc0iOH5/MMcEJ3o/mrukE0GIlo+pwNVYKtLGLrbl/lxS+7aNDeIKepe3veuFDXIeD1fmcLvzaVkK0p4DiMcDGGY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724272088; c=relaxed/simple; bh=r1kDhnH8jC1sUtFvH/R5u21JpbmTbGgSf1pdB4jxXZQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=JT5hUiuox9/VCTAfck42kfCzQwy784GM9IL8TPJEoqkH6+oQ7o+LkFKV1pz4f2HznkR3lRrlKh65UEmJmCE7MExLgRHTyHTOEDqNLQN7sWcyR1OrlMWKovgqgvOdNK9eFlPEzFf9LmpezlYa+5QDFBJSxdqzR+yaPi8MzTAryqE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724272084; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=qmhXq6xKEq5VREKfXVxNa9TOddsnGDy75Z8ZhJdzpVI=; b=foNzCXe3Lqtm+3bWgkEwm30ZAOigfYwpCp+FnprZvoKgp+psI5kq5AnAp6+HDwf+EVpgXW cvJpnwE6hpRQJCgTDdNIeAs41w3q+bLAbOdjJyp6YjPIch/ZSGaapzXc4ydBWKrMCeoga8 rtReejoQaj1C/w23DjGR0S8cLMTfKu4= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-167-RrEINacEPlGUYI7FwlbE9A-1; Wed, 21 Aug 2024 16:28:01 -0400 X-MC-Unique: RrEINacEPlGUYI7FwlbE9A-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8D66F1955D54; Wed, 21 Aug 2024 20:27:59 +0000 (UTC) Received: from localhost (unknown [10.42.28.148]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id AA8E21955F23; Wed, 21 Aug 2024 20:27:58 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: Define operator== for hash table iterators [PR115939] Date: Wed, 21 Aug 2024 21:27:29 +0100 Message-ID: <20240821202757.570009-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE, URI_HEX autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Tested x86_64-linux. I plan to push this soon. -- >8 -- Currently iterators for unordered containers do not directly define operator== and operator!= overloads. Instead they rely on the base class defining them, which is done so that iterator and const_iterator comparisons work using the same overloads. However this means a derived-to-base conversion is needed to call those operators, and PR libstdc++/115939 shows that this can be ambiguous (for -pedantic) when another overloaded operator could be used after an implicit conversion. This change defines operator== and operator!= directly for _Node_iterator and _Node_const_iterator so that no derived-to-base conversions are needed. The new overloads just forward to the base class ones, so the implementation is still shared and doesn't need to be duplicated. libstdc++-v3/ChangeLog: PR libstdc++/115939 * include/bits/hashtable_policy.h (_Node_iterator): Add operator== and operator!=. (_Node_const_iterator): Likewise. * testsuite/23_containers/unordered_map/115939.cc: New test. --- libstdc++-v3/include/bits/hashtable_policy.h | 80 ++++++++++++++++++- .../23_containers/unordered_map/115939.cc | 29 +++++++ 2 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 libstdc++-v3/testsuite/23_containers/unordered_map/115939.cc diff --git a/libstdc++-v3/include/bits/hashtable_policy.h b/libstdc++-v3/include/bits/hashtable_policy.h index 26def24f24e..1b9d9aae1a7 100644 --- a/libstdc++-v3/include/bits/hashtable_policy.h +++ b/libstdc++-v3/include/bits/hashtable_policy.h @@ -464,6 +464,23 @@ namespace __detail this->_M_incr(); return __tmp; } + +#if __cpp_impl_three_way_comparison >= 201907L + friend bool + operator==(const _Node_iterator&, const _Node_iterator&) = default; +#else + friend bool + operator==(const _Node_iterator& __x, const _Node_iterator& __y) noexcept + { + const __base_type& __bx = __x; + const __base_type& __by = __y; + return __bx == __by; + } + + friend bool + operator!=(const _Node_iterator& __x, const _Node_iterator& __y) noexcept + { return !(__x == __y); } +#endif }; /// Node const_iterators, used to iterate through all the hashtable. @@ -475,6 +492,10 @@ namespace __detail using __base_type = _Node_iterator_base<_Value, __cache>; using __node_type = typename __base_type::__node_type; + // The corresponding non-const iterator. + using __iterator + = _Node_iterator<_Value, __constant_iterators, __cache>; + public: typedef _Value value_type; typedef std::ptrdiff_t difference_type; @@ -489,8 +510,7 @@ namespace __detail _Node_const_iterator(__node_type* __p) noexcept : __base_type(__p) { } - _Node_const_iterator(const _Node_iterator<_Value, __constant_iterators, - __cache>& __x) noexcept + _Node_const_iterator(const __iterator& __x) noexcept : __base_type(__x._M_cur) { } reference @@ -515,6 +535,62 @@ namespace __detail this->_M_incr(); return __tmp; } + +#if __cpp_impl_three_way_comparison >= 201907L + friend bool + operator==(const _Node_const_iterator&, + const _Node_const_iterator&) = default; + + friend bool + operator==(const _Node_const_iterator& __x, const __iterator& __y) + { + const __base_type& __bx = __x; + const __base_type& __by = __y; + return __bx == __by; + } +#else + friend bool + operator==(const _Node_const_iterator& __x, + const _Node_const_iterator& __y) noexcept + { + const __base_type& __bx = __x; + const __base_type& __by = __y; + return __bx == __by; + } + + friend bool + operator!=(const _Node_const_iterator& __x, + const _Node_const_iterator& __y) noexcept + { return !(__x == __y); } + + friend bool + operator==(const _Node_const_iterator& __x, + const __iterator& __y) noexcept + { + const __base_type& __bx = __x; + const __base_type& __by = __y; + return __bx == __by; + } + + friend bool + operator!=(const _Node_const_iterator& __x, + const __iterator& __y) noexcept + { return !(__x == __y); } + + friend bool + operator==(const __iterator& __x, + const _Node_const_iterator& __y) noexcept + { + const __base_type& __bx = __x; + const __base_type& __by = __y; + return __bx == __by; + } + + friend bool + operator!=(const __iterator& __x, + const _Node_const_iterator& __y) noexcept + { return !(__x == __y); } +#endif }; // Many of class template _Hashtable's template parameters are policy diff --git a/libstdc++-v3/testsuite/23_containers/unordered_map/115939.cc b/libstdc++-v3/testsuite/23_containers/unordered_map/115939.cc new file mode 100644 index 00000000000..3a68acaff77 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/unordered_map/115939.cc @@ -0,0 +1,29 @@ +// { dg-options "-pedantic" } +// { dg-do compile { target c++11 } } + +// Bug 115939 - Cannot unambiguously compare iterators in std::unordered_map + +#include + +struct any_conv +{ + template + any_conv(T&&) { } + + template + friend bool operator==(const any_conv&, const T&) { return true; } + + template + friend bool operator!=(const any_conv&, const T&) { return false; } +}; + +std::unordered_map::iterator i{}; +std::unordered_map::const_iterator j{}; +bool b1 = i == i; // { dg-bogus "ambiguous" } +bool b2 = j == j; // { dg-bogus "ambiguous" } +bool b3 = i == j; // { dg-bogus "ambiguous" } +bool b4 = j == i; // { dg-bogus "ambiguous" } +bool b5 = i != i; // { dg-bogus "ambiguous" } +bool b6 = j != j; // { dg-bogus "ambiguous" } +bool b7 = i != j; // { dg-bogus "ambiguous" } +bool b8 = j != i; // { dg-bogus "ambiguous" }