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 BD211384AB69 for ; Tue, 7 May 2024 13:47:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BD211384AB69 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 BD211384AB69 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=1715089631; cv=none; b=UzSRihMzHu8a8HbHmAYxymMwNkcTQ0YaA04ZARk+r+d56IFM8AzZiKC4xiempybg/U5Cv4dfOcyzylH3sbhxDsJHe8x2FzpM8rYl1DFGO/N9aaniSo/hWOelar9wIucIQTiCHGYzq7YPeWKpLuUL+NE9d4GeJWmtuNh2/QImeOc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715089631; c=relaxed/simple; bh=rCI4U6b9MJMJbjrG/5cdSkRbL8F1sTjzt1aYI7FzKhI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Khg1TtxMWaW1YIQcADxxag+THvrEDdJtQqpNcXeko5oszNND69LWnIlFM35506t6s/WQQf6EINwIQUbsuU0GGE+wVQBOnNlql1fiIZbki9Pyjhjdgt+/lykDrclE+uZMub3dLhFLkIh3HnM6+MVejSVAxUSwUFmUlIZM2jZOEi4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715089628; 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=S/Ph7EvwMntNP4VJtXFpaiEhA1wJNPiR2ExDZ4S7RyMp/4A8oOfSpSjyJLErHQLWrsWmO5 L5QxEpIdFMAQUwDDRvn0wRWB/l/bpx0tVcN4ar2Yp1D0D7dMH9Mk3ozg7pz/RpyeWy7ZE3 SCeqKLr88mnK7ivlDS1MiCoJV/tHJ9w= 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 Content-Type: text/plain Content-Transfer-Encoding: 8bit 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=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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 -- 2.44.0