public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] libstdc++: Rewrite std::variant comparisons without macros
@ 2024-05-07 13:46 Jonathan Wakely
  2024-05-07 13:51 ` Ville Voutilainen
  0 siblings, 1 reply; 3+ messages in thread
From: Jonathan Wakely @ 2024-05-07 13:46 UTC (permalink / raw)
  To: libstdc++, gcc-patches

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] libstdc++: Rewrite std::variant comparisons without macros
  2024-05-07 13:46 [PATCH] libstdc++: Rewrite std::variant comparisons without macros Jonathan Wakely
@ 2024-05-07 13:51 ` Ville Voutilainen
  2024-05-15  9:21   ` Jonathan Wakely
  0 siblings, 1 reply; 3+ messages in thread
From: Ville Voutilainen @ 2024-05-07 13:51 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: libstdc++, gcc-patches

On Tue, 7 May 2024 at 16:47, Jonathan Wakely <jwakely@redhat.com> wrote:
>
> 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?

No, that's beautiful, ship it.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] libstdc++: Rewrite std::variant comparisons without macros
  2024-05-07 13:51 ` Ville Voutilainen
@ 2024-05-15  9:21   ` Jonathan Wakely
  0 siblings, 0 replies; 3+ messages in thread
From: Jonathan Wakely @ 2024-05-15  9:21 UTC (permalink / raw)
  To: Ville Voutilainen; +Cc: libstdc++, gcc-patches

On Tue, 7 May 2024 at 14:51, Ville Voutilainen
<ville.voutilainen@gmail.com> wrote:
>
> On Tue, 7 May 2024 at 16:47, Jonathan Wakely <jwakely@redhat.com> wrote:
> >
> > 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?
>
> No, that's beautiful, ship it.

Pushed to trunk.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-05-15  9:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-07 13:46 [PATCH] libstdc++: Rewrite std::variant comparisons without macros Jonathan Wakely
2024-05-07 13:51 ` Ville Voutilainen
2024-05-15  9:21   ` Jonathan Wakely

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).