From patchwork Tue May 7 13:46:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1932484 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=fHhA5gnL; 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 4VYfkB0k04z1xnT for ; Tue, 7 May 2024 23:48:18 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 486F6384B013 for ; Tue, 7 May 2024 13:48:16 +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 B77AC385EC56 for ; Tue, 7 May 2024 13:47:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B77AC385EC56 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 B77AC385EC56 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=1715089632; cv=none; b=c6RKf2rbLg6DktgFf+R7SvA2dq155HstZ5kvmoetz7nUUfiqfooVtzE53o18DhnmJbzhFngGVom/JSo5WAMXeC6YYV1mAjBPMrS10vmi/Nrc6VpLVPBcUl4173QcguHQNN5Jsx6883TarySoQ14WqD4/dyIRO/g4NP7DuLr6Eps= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715089632; c=relaxed/simple; bh=rCI4U6b9MJMJbjrG/5cdSkRbL8F1sTjzt1aYI7FzKhI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=klkjHnxF3MYYWQNkLOmOXjgBh5IwjeZrLYpAl8Y7ZiqCRPsz+Gjf1h5T0kz6wNdo0mFKoLk8RFQX6yInGqeQOLV5p6Omak0Osz/Dzzcr1BDAoP2L+gTp521EBYC9AloZr0URVEyfKUIRye/E7vJzMOZmefURcaE0U6XSD4O3JeM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715089630; 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=MRCvmi38YDqByxbK+0Iiv3VgiKI5Glj22en9gNHQUAM=; b=fHhA5gnLPliM4fwbadEgXqipR4Oku8isS2kRnM4neImIc4v2ykLQ8FIZfIkhXnhQHVmSt5 iPCTxwSLb6k9mZ0HJeJrW0C+6TV7S/HTf7K1QtWdv/LbCLJIUBAHJSnRu/M1tn/C82n+RV bUbECUO9o7dkrHlPkgspch5D4dCVuLk= 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-617-LJFSXxMtNLC0oEpeOcwLLg-1; Tue, 07 May 2024 09:47:07 -0400 X-MC-Unique: LJFSXxMtNLC0oEpeOcwLLg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (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 B6D8A185A780; Tue, 7 May 2024 13:47:06 +0000 (UTC) Received: from localhost (unknown [10.42.28.238]) by smtp.corp.redhat.com (Postfix) with ESMTP id 54199200A3BB; Tue, 7 May 2024 13:47:06 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: Rewrite std::variant comparisons without macros Date: Tue, 7 May 2024 14:46:02 +0100 Message-ID: <20240507134705.3819956-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 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 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 I don't think using a macro for these really saves us much, we can do this to avoid duplication instead. And now it's not a big, multi-line macro that's a pain to edit. Any objections? Tested x86_64-linux. -- >8 -- libstdc++-v3/ChangeLog: * include/std/variant (__detail::__variant::__compare): New function template. (operator==, operator!=, operator<, operator>, operator<=) (operator>=): Replace macro definition with handwritten function calling __detail::__variant::__compare. (operator<=>): Call __detail::__variant::__compare. --- libstdc++-v3/include/std/variant | 167 +++++++++++++++++++++---------- 1 file changed, 114 insertions(+), 53 deletions(-) diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant index bf05eec9a6b..cfb4bcdbcc9 100644 --- a/libstdc++-v3/include/std/variant +++ b/libstdc++-v3/include/std/variant @@ -48,6 +48,7 @@ #include #include // in_place_index_t #if __cplusplus >= 202002L +# include # include #endif @@ -1237,47 +1238,119 @@ namespace __variant struct monostate { }; -#if __cpp_lib_concepts -# define _VARIANT_RELATION_FUNCTION_CONSTRAINTS(TYPES, OP) \ - requires ((requires (const TYPES& __t) { \ - { __t OP __t } -> __detail::__boolean_testable; }) && ...) -#else -# define _VARIANT_RELATION_FUNCTION_CONSTRAINTS(TYPES, OP) -#endif +namespace __detail::__variant +{ + template + constexpr _Ret + __compare(_Ret __ret, const _Vp& __lhs, const _Vp& __rhs, _Op __op) + { + __variant::__raw_idx_visit( + [&__ret, &__lhs, __op] (auto&& __rhs_mem, auto __rhs_index) mutable + { + if constexpr (__rhs_index != variant_npos) + { + if (__lhs.index() == __rhs_index.value) + { + auto& __this_mem = std::get<__rhs_index>(__lhs); + __ret = __op(__this_mem, __rhs_mem); + return; + } + } + __ret = __op(__lhs.index() + 1, __rhs_index + 1); + }, __rhs); + return __ret; + } +} // namespace __detail::__variant -#define _VARIANT_RELATION_FUNCTION_TEMPLATE(__OP) \ - template \ - _VARIANT_RELATION_FUNCTION_CONSTRAINTS(_Types, __OP) \ - constexpr bool \ - operator __OP [[nodiscard]] (const variant<_Types...>& __lhs, \ - const variant<_Types...>& __rhs) \ - { \ - bool __ret = true; \ - __detail::__variant::__raw_idx_visit( \ - [&__ret, &__lhs] (auto&& __rhs_mem, auto __rhs_index) mutable \ - { \ - if constexpr (__rhs_index != variant_npos) \ - { \ - if (__lhs.index() == __rhs_index) \ - { \ - auto& __this_mem = std::get<__rhs_index>(__lhs); \ - __ret = __this_mem __OP __rhs_mem; \ - return; \ - } \ - } \ - __ret = (__lhs.index() + 1) __OP (__rhs_index + 1); \ - }, __rhs); \ - return __ret; \ + template +#if __cpp_lib_concepts + requires ((requires (const _Types& __t) { + { __t == __t } -> convertible_to; }) && ...) +#endif + constexpr bool + operator== [[nodiscard]] (const variant<_Types...>& __lhs, + const variant<_Types...>& __rhs) + { + return __detail::__variant::__compare(true, __lhs, __rhs, + [](auto&& __l, auto&& __r) { + return __l == __r; + }); } - _VARIANT_RELATION_FUNCTION_TEMPLATE(<) - _VARIANT_RELATION_FUNCTION_TEMPLATE(<=) - _VARIANT_RELATION_FUNCTION_TEMPLATE(==) - _VARIANT_RELATION_FUNCTION_TEMPLATE(!=) - _VARIANT_RELATION_FUNCTION_TEMPLATE(>=) - _VARIANT_RELATION_FUNCTION_TEMPLATE(>) + template +#if __cpp_lib_concepts + requires ((requires (const _Types& __t) { + { __t != __t } -> convertible_to; }) && ...) +#endif + constexpr bool + operator!= [[nodiscard]] (const variant<_Types...>& __lhs, + const variant<_Types...>& __rhs) + { + return __detail::__variant::__compare(true, __lhs, __rhs, + [](auto&& __l, auto&& __r) { + return __l != __r; + }); + } -#undef _VARIANT_RELATION_FUNCTION_TEMPLATE + template +#if __cpp_lib_concepts + requires ((requires (const _Types& __t) { + { __t < __t } -> convertible_to; }) && ...) +#endif + constexpr bool + operator< [[nodiscard]] (const variant<_Types...>& __lhs, + const variant<_Types...>& __rhs) + { + return __detail::__variant::__compare(true, __lhs, __rhs, + [](auto&& __l, auto&& __r) { + return __l < __r; + }); + } + + template +#if __cpp_lib_concepts + requires ((requires (const _Types& __t) { + { __t <= __t } -> convertible_to; }) && ...) +#endif + constexpr bool + operator<= [[nodiscard]] (const variant<_Types...>& __lhs, + const variant<_Types...>& __rhs) + { + return __detail::__variant::__compare(true, __lhs, __rhs, + [](auto&& __l, auto&& __r) { + return __l <= __r; + }); + } + + template +#if __cpp_lib_concepts + requires ((requires (const _Types& __t) { + { __t > __t } -> convertible_to; }) && ...) +#endif + constexpr bool + operator> [[nodiscard]] (const variant<_Types...>& __lhs, + const variant<_Types...>& __rhs) + { + return __detail::__variant::__compare(true, __lhs, __rhs, + [](auto&& __l, auto&& __r) { + return __l > __r; + }); + } + + template +#if __cpp_lib_concepts + requires ((requires (const _Types& __t) { + { __t >= __t } -> convertible_to; }) && ...) +#endif + constexpr bool + operator>= [[nodiscard]] (const variant<_Types...>& __lhs, + const variant<_Types...>& __rhs) + { + return __detail::__variant::__compare(true, __lhs, __rhs, + [](auto&& __l, auto&& __r) { + return __l >= __r; + }); + } constexpr bool operator==(monostate, monostate) noexcept { return true; } @@ -1290,22 +1363,10 @@ namespace __variant { common_comparison_category_t...> __ret = strong_ordering::equal; - - __detail::__variant::__raw_idx_visit( - [&__ret, &__v] (auto&& __w_mem, auto __w_index) mutable - { - if constexpr (__w_index != variant_npos) - { - if (__v.index() == __w_index) - { - auto& __this_mem = std::get<__w_index>(__v); - __ret = __this_mem <=> __w_mem; - return; - } - } - __ret = (__v.index() + 1) <=> (__w_index + 1); - }, __w); - return __ret; + return __detail::__variant::__compare(__ret, __v, __w, + [](auto&& __l, auto&& __r) { + return __l <=> __r; + }); } constexpr strong_ordering