From patchwork Wed Apr 10 08:45:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1921848 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=hZPxxa2i; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4VDxQJ1h49z1yYS for ; Wed, 10 Apr 2024 18:51:34 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 57C523858288 for ; Wed, 10 Apr 2024 08:51:32 +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 ESMTPS id 4AE233858D20 for ; Wed, 10 Apr 2024 08:50:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4AE233858D20 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 4AE233858D20 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=1712739048; cv=none; b=PWtmtwC0vZqoWHrKcsgGJf8stRx9vcjeFlqKo56CUVgu7GgBXn2l3yn0YlEHhXjvmlOs705afHT7RumH37cgRKZ32LPQbZMORGjP6okpodcQiy0AGFRoM8Jscf3yMrwRLRvHU0FPIUHECvZAC+Xuvg8Vg67nGNbq9VZ01iBvRrc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712739048; c=relaxed/simple; bh=+iLIc64PsN3chMfAca1MJ+BVIVSsCndDrsm2D5S8yhA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=LMqVK46tTtTHEE0S26LXBVzHcySUjC6ISXwgClHG9PVU525hm2g7mWGKQKhfLBPqy93NtliP32QdvPSIXOz7sEDuSakDpqTpt0x0oBnqWmRBsc/qXPd0wdtBSh3JcdUF0iNPo+QD9bzq4Ffv8ILAcVkH8LF9MShNvRzjG0Ud8cw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712739045; 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=hZPxxa2iBf18prkZIngpFMTop3EVxio8m01jfzt1hUorpmrfDt2lJo31GLF1z0P6N18ups KErinxrCHsUVqQkIQCWtRBHtfZZo+3u/QjOY5IjKeOhH121nXoucOHkSNS/0bWtu9g/XsE ifAbkPLnJldkq/YS1cAqhfJsYLSVgn0= 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 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=ham 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. 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 ); +}