Skip to content

Conversation

philnik777
Copy link
Contributor

This specializes __lazy_synth_three_way_comparator to forward to __default_three_way_comparator if the comparator desugars to __greater_tag. This is the same as the desugaring to __less_tag except that the sign has to be inverted.

@ldionne ldionne marked this pull request as ready for review September 10, 2025 15:41
@ldionne ldionne requested a review from a team as a code owner September 10, 2025 15:41
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Sep 10, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 10, 2025

@llvm/pr-subscribers-libcxx

Author: Nikolas Klauser (philnik777)

Changes

This specializes __lazy_synth_three_way_comparator to forward to __default_three_way_comparator if the comparator desugars to __greater_tag. This is the same as the desugaring to __less_tag except that the sign has to be inverted.


Full diff: https://github.com/llvm/llvm-project/pull/157624.diff

1 Files Affected:

  • (modified) libcxx/include/__utility/lazy_synth_three_way_comparator.h (+17)
diff --git a/libcxx/include/__utility/lazy_synth_three_way_comparator.h b/libcxx/include/__utility/lazy_synth_three_way_comparator.h
index ca98845f04191..635dee1f7e4cb 100644
--- a/libcxx/include/__utility/lazy_synth_three_way_comparator.h
+++ b/libcxx/include/__utility/lazy_synth_three_way_comparator.h
@@ -85,6 +85,23 @@ struct __lazy_synth_three_way_comparator<_Comparator,
   }
 };
 
+template <class _Comparator, class _LHS, class _RHS>
+struct __lazy_synth_three_way_comparator<_Comparator,
+                                         _LHS,
+                                         _RHS,
+                                         __enable_if_t<__desugars_to_v<__greater_tag, _Comparator, _LHS, _RHS> &&
+                                                       __has_default_three_way_comparator_v<_LHS, _RHS> > > {
+  // This lifetimebound annotation is technically incorrect, but other specializations actually capture the lifetime of
+  // the comparator.
+  _LIBCPP_HIDE_FROM_ABI __lazy_synth_three_way_comparator(_LIBCPP_CTOR_LIFETIMEBOUND const _Comparator&) {}
+
+  // Same comment as above.
+  _LIBCPP_HIDE_FROM_ABI static __eager_compare_result
+  operator()(_LIBCPP_LIFETIMEBOUND const _LHS& __lhs, _LIBCPP_LIFETIMEBOUND const _RHS& __rhs) {
+    return __eager_compare_result(-__default_three_way_comparator<_LHS, _RHS>()(__lhs, __rhs));
+  }
+};
+
 _LIBCPP_END_NAMESPACE_STD
 
 #endif // _LIBCPP___UTILITY_LAZY_SYNTH_THREE_WAY_COMPARATOR_H

Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with added tests as suggested.

@philnik777 philnik777 force-pushed the extend_lazy_synth_three_way branch from 80a18e3 to 17270f6 Compare September 11, 2025 08:35
Copy link

github-actions bot commented Sep 11, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@philnik777 philnik777 force-pushed the extend_lazy_synth_three_way branch 2 times, most recently from a390093 to 33bd99b Compare September 12, 2025 09:11
@philnik777 philnik777 force-pushed the extend_lazy_synth_three_way branch from 33bd99b to 3d89d79 Compare September 15, 2025 09:21
@philnik777 philnik777 merged commit 53a4e4a into llvm:main Sep 15, 2025
69 of 80 checks passed
@philnik777 philnik777 deleted the extend_lazy_synth_three_way branch September 15, 2025 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants