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.129.124]) by sourceware.org (Postfix) with ESMTPS id E16283858D32 for ; Wed, 10 Apr 2024 08:50:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E16283858D32 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 E16283858D32 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=1712739050; cv=none; b=aLxKtbGQNTjk5B5llMi0XRxasOhLwx7Ll7o5myB5t/o7jMuUWU6MO/zeZUzDIt4CvqOlL1UK3O28lD47QXgPotg8jgKTHUQAXA34MRnni3aM6l+CMLsx9S5nBeiVnXXdkCFfWR2pJqdvLecyP41Yo0nnNzHzRjpWS92wrBZ+Idk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712739050; c=relaxed/simple; bh=+iLIc64PsN3chMfAca1MJ+BVIVSsCndDrsm2D5S8yhA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ChwWUc0VSaNn6uKbb/2k8srw4oKyWo7JGWpOLhk7k9hQ6k0+sSqJFuCtLtyorwPz7VkC/gbYFCpiHP9ZenYNADwgyIGUbElj7rkm1qe5g5YJupcKAVdtYyBWs4i9YwSi1w0eAEImtRmAd5ZcqjDI9MGJnu17QFuiAaXq87E/PxI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712739047; 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=kvKlvDxajaEx2rFFN9HVbyCSpxzHd5wTvw+WgoeecNs=; b=Dn89TLBMXBDJVYA2bp/gYZZn/FbxiYkSuo782Q/34EavasUvffzoMHbC/w3qEgV1g1V4XQ eBHTiFpT4etk3J9o176ARcrlMKvpL/2LU12s8GpTNClDkKxApfEH/zk+YOnq1Uf0V4ae+5 N5K6KHyIX8t9E8qsWzHraNeA+54XiEU= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-33-hYYdZQeMPpioIrEvWDHGQA-1; Wed, 10 Apr 2024 04:50:44 -0400 X-MC-Unique: hYYdZQeMPpioIrEvWDHGQA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 39AC0890520; Wed, 10 Apr 2024 08:50:44 +0000 (UTC) Received: from localhost (unknown [10.42.28.163]) by smtp.corp.redhat.com (Postfix) with ESMTP id 09EDA1C060A6; Wed, 10 Apr 2024 08:50:43 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH 1/4] libstdc++: Heterogeneous std::pair comparisons [PR113386] Date: Wed, 10 Apr 2024 09:45:53 +0100 Message-ID: <20240410085039.267589-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.9 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_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Tested x86_64-linux. Since this only affects C++20 and later it seems OK for trunk now. -- >8 -- I'm only treating this as a DR for C++20 for now, because it's less work and only requires changes to operator== and operator<=>. To do this for older standards would require changes to the six relational operators used pre-C++20. libstdc++-v3/ChangeLog: PR libstdc++/113386 * include/bits/stl_pair.h (operator==, operator<=>): Support heterogeneous comparisons, as per LWG 3865. * testsuite/20_util/pair/comparison_operators/lwg3865.cc: New test. --- libstdc++-v3/include/bits/stl_pair.h | 32 ++++++++++++++----- .../pair/comparison_operators/lwg3865.cc | 15 +++++++++ 2 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/pair/comparison_operators/lwg3865.cc diff --git a/libstdc++-v3/include/bits/stl_pair.h b/libstdc++-v3/include/bits/stl_pair.h index 4f5c8389fa6..45317417c9c 100644 --- a/libstdc++-v3/include/bits/stl_pair.h +++ b/libstdc++-v3/include/bits/stl_pair.h @@ -1000,23 +1000,39 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template pair(_T1, _T2) -> pair<_T1, _T2>; #endif - /// Two pairs of the same type are equal iff their members are equal. - template +#if __cpp_lib_three_way_comparison && __cpp_lib_concepts + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 3865. Sorting a range of pairs + + /// Two pairs are equal iff their members are equal. + template inline _GLIBCXX_CONSTEXPR bool - operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) + operator==(const pair<_T1, _T2>& __x, const pair<_U1, _U2>& __y) { return __x.first == __y.first && __x.second == __y.second; } -#if __cpp_lib_three_way_comparison && __cpp_lib_concepts - template - constexpr common_comparison_category_t<__detail::__synth3way_t<_T1>, - __detail::__synth3way_t<_T2>> - operator<=>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) + /** Defines a lexicographical order for pairs. + * + * For two pairs of comparable types, `P` is ordered before `Q` if + * `P.first` is less than `Q.first`, or if `P.first` and `Q.first` + * are equivalent (neither is less than the other) and `P.second` is + * less than `Q.second`. + */ + template + constexpr common_comparison_category_t<__detail::__synth3way_t<_T1, _U1>, + __detail::__synth3way_t<_T2, _U2>> + operator<=>(const pair<_T1, _T2>& __x, const pair<_U1, _U2>& __y) { if (auto __c = __detail::__synth3way(__x.first, __y.first); __c != 0) return __c; return __detail::__synth3way(__x.second, __y.second); } #else + /// Two pairs of the same type are equal iff their members are equal. + template + inline _GLIBCXX_CONSTEXPR bool + operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) + { return __x.first == __y.first && __x.second == __y.second; } + /** Defines a lexicographical order for pairs. * * For two pairs of the same type, `P` is ordered before `Q` if diff --git a/libstdc++-v3/testsuite/20_util/pair/comparison_operators/lwg3865.cc b/libstdc++-v3/testsuite/20_util/pair/comparison_operators/lwg3865.cc new file mode 100644 index 00000000000..2bbd54af192 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/pair/comparison_operators/lwg3865.cc @@ -0,0 +1,15 @@ +// { dg-do run { target c++20 } } + +// LWG 3865. Sorting a range of pairs + +#include +#include + +int main() +{ + std::pair p(1, 2); + std::pair p2(p.first, p.second); + VERIFY( p == p2 ); + VERIFY( p <= p2 ); + VERIFY( p >= p2 ); +} -- 2.44.0