From: Jonathan Wakely <jwakely@redhat.com>
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 [thread overview]
Message-ID: <20240507134705.3819956-1-jwakely@redhat.com> (raw)
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 <bits/stl_construct.h>
#include <bits/utility.h> // in_place_index_t
#if __cplusplus >= 202002L
+# include <concepts>
# include <compare>
#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<typename _Ret, typename _Vp, typename _Op>
+ 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<typename... _Types> \
- _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<typename... _Types>
+#if __cpp_lib_concepts
+ requires ((requires (const _Types& __t) {
+ { __t == __t } -> convertible_to<bool>; }) && ...)
+#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<typename... _Types>
+#if __cpp_lib_concepts
+ requires ((requires (const _Types& __t) {
+ { __t != __t } -> convertible_to<bool>; }) && ...)
+#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<typename... _Types>
+#if __cpp_lib_concepts
+ requires ((requires (const _Types& __t) {
+ { __t < __t } -> convertible_to<bool>; }) && ...)
+#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<typename... _Types>
+#if __cpp_lib_concepts
+ requires ((requires (const _Types& __t) {
+ { __t <= __t } -> convertible_to<bool>; }) && ...)
+#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<typename... _Types>
+#if __cpp_lib_concepts
+ requires ((requires (const _Types& __t) {
+ { __t > __t } -> convertible_to<bool>; }) && ...)
+#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<typename... _Types>
+#if __cpp_lib_concepts
+ requires ((requires (const _Types& __t) {
+ { __t >= __t } -> convertible_to<bool>; }) && ...)
+#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<compare_three_way_result_t<_Types>...> __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
next reply other threads:[~2024-05-07 13:47 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-07 13:46 Jonathan Wakely [this message]
2024-05-07 13:51 ` Ville Voutilainen
2024-05-15 9:21 ` Jonathan Wakely
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240507134705.3819956-1-jwakely@redhat.com \
--to=jwakely@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).