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.133.124]) by sourceware.org (Postfix) with ESMTPS id 900443861019 for ; Wed, 10 Apr 2024 10:48:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 900443861019 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 900443861019 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712746134; cv=none; b=Y0it+mZLxHJBpc9ZaW28/a3Zq10EQrEDsIsvIferPpmc4bVYaFkHizhEsoOPxmpeIY5j6qxv5TTmExTC3AzzIpvcafaV+V/dGH7qV3MXVlEusVu02XinOTYYqek1Dx2ew28oDCQNwykX6bSG00U8oBAWVwlj3qXP3gaBVsjwwTU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712746134; c=relaxed/simple; bh=0FFthcdDSqCpN7nVQ+gXYYNqGQWTYxdUOfxI19xHdt4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=EvesRuVUKBIjsPUtyKewwokALpWVGxyTMiI8fBiwpHMTcMqG80QZo9ZEdzv6TK3Ji/eEsQYA+uQVtpb9MT0i7/L7sBHee2B6YoSoGQR6pqvEzG7irm+7rZ6htMCrzJsaedNdTFpNxOTYfwyGAnMbV1GPrNa6UcctCUvSfn6NC6E= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712746130; 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: in-reply-to:in-reply-to:references:references; bh=utNYIArHCogPJ6qWbTDpM8hfJjYXt6kpZlCcDBrvgOk=; b=Fx4h9HCTRdFuP35MxFSpx7S1Vzlie6z6z2tLdhWMLfIw2DrdgKh9FP4L6/1PdagdmTHH1Y gkhFDB4Mnh66NClB3Xwl1NuTQ5lb4JX0zW+SarqITR4MwxK6VA9/dXDaWP85P9tqIwgulQ 1iXXBJrhVbXlJrAl66Qe8zOQ7Ven/Hw= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-588-IrPptdF2N3-G8L7SKRl11w-1; Wed, 10 Apr 2024 06:48:48 -0400 X-MC-Unique: IrPptdF2N3-G8L7SKRl11w-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (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 9697829AB402; Wed, 10 Apr 2024 10:48:48 +0000 (UTC) Received: from localhost (unknown [10.42.28.163]) by smtp.corp.redhat.com (Postfix) with ESMTP id 63B9F40AE7C8; Wed, 10 Apr 2024 10:48:48 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH 5/4] libstdc++: Rewrite std::variant comparisons without macros Date: Wed, 10 Apr 2024 11:45:26 +0100 Message-ID: <20240410104827.289023-1-jwakely@redhat.com> In-Reply-To: <20240410085039.267589-4-jwakely@redhat.com> References: <20240410085039.267589-4-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.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 think this is considerably nicer than the macro version, but it can definitely wait for stage 1. -- >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 87a119df8b5..c2277e8831a 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