[libcxx-commits] [libcxx] 013b335 - [libc++][NFC] Simplify duration comparisons a bit (#201788)
via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Jun 5 03:29:48 PDT 2026
Author: Nikolas Klauser
Date: 2026-06-05T12:29:42+02:00
New Revision: 013b33583b632f4c680db82f42223e3684da8cb2
URL: https://github.com/llvm/llvm-project/commit/013b33583b632f4c680db82f42223e3684da8cb2
DIFF: https://github.com/llvm/llvm-project/commit/013b33583b632f4c680db82f42223e3684da8cb2.diff
LOG: [libc++][NFC] Simplify duration comparisons a bit (#201788)
The comparisons have been delegated to a class which has been
specialized for the equality case. This has likely been done to avoid
`common_type` if possible. However, `common_type` got a lot cheaper, to
the point where the classes likely do more harm than good.
Added:
Modified:
libcxx/include/__chrono/duration.h
Removed:
################################################################################
diff --git a/libcxx/include/__chrono/duration.h b/libcxx/include/__chrono/duration.h
index b7762bd1203ad..92efd82f72145 100644
--- a/libcxx/include/__chrono/duration.h
+++ b/libcxx/include/__chrono/duration.h
@@ -301,25 +301,11 @@ typedef duration<int, ratio<static_cast<int>(365.2425 * 60 * 60 * 24) / 12>> mon
#endif
// Duration ==
-template <class _LhsDuration, class _RhsDuration>
-struct __duration_eq {
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const {
- typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
- return _Ct(__lhs).count() == _Ct(__rhs).count();
- }
-};
-
-template <class _LhsDuration>
-struct __duration_eq<_LhsDuration, _LhsDuration> {
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const {
- return __lhs.count() == __rhs.count();
- }
-};
-
template <class _Rep1, class _Period1, class _Rep2, class _Period2>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool
operator==(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {
- return __duration_eq<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
+ using _Ct = typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type;
+ return _Ct(__lhs).count() == _Ct(__rhs).count();
}
#if _LIBCPP_STD_VER <= 17
@@ -336,25 +322,11 @@ operator!=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period
// Duration <
-template <class _LhsDuration, class _RhsDuration>
-struct __duration_lt {
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const {
- typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
- return _Ct(__lhs).count() < _Ct(__rhs).count();
- }
-};
-
-template <class _LhsDuration>
-struct __duration_lt<_LhsDuration, _LhsDuration> {
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const {
- return __lhs.count() < __rhs.count();
- }
-};
-
template <class _Rep1, class _Period1, class _Rep2, class _Period2>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool
operator<(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {
- return __duration_lt<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
+ using _Ct = typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type;
+ return _Ct(__lhs).count() < _Ct(__rhs).count();
}
// Duration >
More information about the libcxx-commits
mailing list