From: Jonathan Wakely <jwakely@redhat.com>
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 [thread overview]
Message-ID: <20240410104827.289023-1-jwakely@redhat.com> (raw)
In-Reply-To: <20240410085039.267589-4-jwakely@redhat.com>
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 <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 prev parent reply other threads:[~2024-04-10 10:48 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-10 8:45 [PATCH 1/4] libstdc++: Heterogeneous std::pair comparisons [PR113386] Jonathan Wakely
2024-04-10 8:45 ` [PATCH 2/4] libstdc++: Add std::reference_wrapper comparison operators for C++26 Jonathan Wakely
2024-04-15 18:30 ` Jonathan Wakely
2024-04-10 8:45 ` [PATCH 3/4] libstdc++: Constrain equality ops for std::pair, std::tuple, std::variant Jonathan Wakely
2024-04-10 8:45 ` [PATCH 4/4] libstdc++: Simplify std::variant comparison operators Jonathan Wakely
2024-04-10 10:45 ` Jonathan Wakely [this message]
2024-05-07 13:45 ` Jonathan Wakely
2024-04-15 18:29 ` [PATCH 1/4] libstdc++: Heterogeneous std::pair comparisons [PR113386] 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=20240410104827.289023-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).