[libcxx-commits] [libcxx] [libc++] Remove some unnecessary boilerplate in <__chrono/duration.h> (PR #116238)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Nov 14 09:36:07 PST 2024
https://github.com/philnik777 updated https://github.com/llvm/llvm-project/pull/116238
>From 26d0bc171892d03efc1e155806002e9e784c63b0 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Thu, 14 Nov 2024 15:28:00 +0100
Subject: [PATCH] [libc++] Remove some unnecessary boilerplate in
<__chrono/duration_cast.h>
---
libcxx/include/__chrono/convert_to_tm.h | 2 +-
libcxx/include/__chrono/duration.h | 27 ++++++++++++-------------
libcxx/include/__chrono/formatter.h | 10 ++++-----
libcxx/include/__chrono/hh_mm_ss.h | 2 +-
libcxx/include/__chrono/time_point.h | 9 ++++-----
libcxx/include/__chrono/zoned_time.h | 2 +-
libcxx/include/ratio | 4 +---
7 files changed, 26 insertions(+), 30 deletions(-)
diff --git a/libcxx/include/__chrono/convert_to_tm.h b/libcxx/include/__chrono/convert_to_tm.h
index 5f1b2632f629a5..46e3650e390ea6 100644
--- a/libcxx/include/__chrono/convert_to_tm.h
+++ b/libcxx/include/__chrono/convert_to_tm.h
@@ -116,7 +116,7 @@ _LIBCPP_HIDE_FROM_ABI _Tm __convert_to_tm(const _ChronoT& __value) {
return std::__convert_to_tm<_Tm>(chrono::sys_time<typename _ChronoT::duration>{__value.time_since_epoch()});
else
static_assert(sizeof(_ChronoT) == 0, "TODO: Add the missing clock specialization");
- } else if constexpr (chrono::__is_duration<_ChronoT>::value) {
+ } else if constexpr (chrono::__is_duration_v<_ChronoT>) {
// [time.format]/6
// ... However, if a flag refers to a "time of day" (e.g. %H, %I, %p,
// etc.), then a specialization of duration is interpreted as the time of
diff --git a/libcxx/include/__chrono/duration.h b/libcxx/include/__chrono/duration.h
index 19662286f6556b..a401178b2a75c0 100644
--- a/libcxx/include/__chrono/duration.h
+++ b/libcxx/include/__chrono/duration.h
@@ -35,26 +35,25 @@ template <class _Rep, class _Period = ratio<1> >
class _LIBCPP_TEMPLATE_VIS duration;
template <class _Tp>
-struct __is_duration : false_type {};
+inline const bool __is_duration_v = false;
template <class _Rep, class _Period>
-struct __is_duration<duration<_Rep, _Period> > : true_type {};
+inline const bool __is_duration_v<duration<_Rep, _Period> > = true;
template <class _Rep, class _Period>
-struct __is_duration<const duration<_Rep, _Period> > : true_type {};
+inline const bool __is_duration_v<const duration<_Rep, _Period> > = true;
template <class _Rep, class _Period>
-struct __is_duration<volatile duration<_Rep, _Period> > : true_type {};
+inline const bool __is_duration_v<volatile duration<_Rep, _Period> > = true;
template <class _Rep, class _Period>
-struct __is_duration<const volatile duration<_Rep, _Period> > : true_type {};
+inline const bool __is_duration_v<const volatile duration<_Rep, _Period> > = true;
} // namespace chrono
template <class _Rep1, class _Period1, class _Rep2, class _Period2>
struct _LIBCPP_TEMPLATE_VIS common_type<chrono::duration<_Rep1, _Period1>, chrono::duration<_Rep2, _Period2> > {
- typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type, typename __ratio_gcd<_Period1, _Period2>::type>
- type;
+ typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type, __ratio_gcd<_Period1, _Period2> > type;
};
namespace chrono {
@@ -102,7 +101,7 @@ struct __duration_cast<_FromDuration, _ToDuration, _Period, false, false> {
}
};
-template <class _ToDuration, class _Rep, class _Period, __enable_if_t<__is_duration<_ToDuration>::value, int> = 0>
+template <class _ToDuration, class _Rep, class _Period, __enable_if_t<__is_duration_v<_ToDuration>, int> = 0>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _ToDuration duration_cast(const duration<_Rep, _Period>& __fd) {
return __duration_cast<duration<_Rep, _Period>, _ToDuration>()(__fd);
}
@@ -124,7 +123,7 @@ struct _LIBCPP_TEMPLATE_VIS duration_values {
};
#if _LIBCPP_STD_VER >= 17
-template <class _ToDuration, class _Rep, class _Period, enable_if_t<__is_duration<_ToDuration>::value, int> = 0>
+template <class _ToDuration, class _Rep, class _Period, enable_if_t<__is_duration_v<_ToDuration>, int> = 0>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _ToDuration floor(const duration<_Rep, _Period>& __d) {
_ToDuration __t = chrono::duration_cast<_ToDuration>(__d);
if (__t > __d)
@@ -132,7 +131,7 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _ToDuration floor(const duration<
return __t;
}
-template <class _ToDuration, class _Rep, class _Period, enable_if_t<__is_duration<_ToDuration>::value, int> = 0>
+template <class _ToDuration, class _Rep, class _Period, enable_if_t<__is_duration_v<_ToDuration>, int> = 0>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _ToDuration ceil(const duration<_Rep, _Period>& __d) {
_ToDuration __t = chrono::duration_cast<_ToDuration>(__d);
if (__t < __d)
@@ -140,7 +139,7 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _ToDuration ceil(const duration<_
return __t;
}
-template <class _ToDuration, class _Rep, class _Period, enable_if_t<__is_duration<_ToDuration>::value, int> = 0>
+template <class _ToDuration, class _Rep, class _Period, enable_if_t<__is_duration_v<_ToDuration>, int> = 0>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _ToDuration round(const duration<_Rep, _Period>& __d) {
_ToDuration __lower = chrono::floor<_ToDuration>(__d);
_ToDuration __upper = __lower + _ToDuration{1};
@@ -158,7 +157,7 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _ToDuration round(const duration<
template <class _Rep, class _Period>
class _LIBCPP_TEMPLATE_VIS duration {
- static_assert(!__is_duration<_Rep>::value, "A duration representation can not be a duration");
+ static_assert(!__is_duration_v<_Rep>, "A duration representation can not be a duration");
static_assert(__is_ratio_v<_Period>, "Second template parameter of duration must be a std::ratio");
static_assert(_Period::num > 0, "duration period must be positive");
@@ -434,7 +433,7 @@ operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d) {
template <class _Rep1,
class _Period,
class _Rep2,
- __enable_if_t<!__is_duration<_Rep2>::value &&
+ __enable_if_t<!__is_duration_v<_Rep2> &&
is_convertible<const _Rep2&, typename common_type<_Rep1, _Rep2>::type>::value,
int> = 0>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR duration<typename common_type<_Rep1, _Rep2>::type, _Period>
@@ -456,7 +455,7 @@ operator/(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2
template <class _Rep1,
class _Period,
class _Rep2,
- __enable_if_t<!__is_duration<_Rep2>::value &&
+ __enable_if_t<!__is_duration_v<_Rep2> &&
is_convertible<const _Rep2&, typename common_type<_Rep1, _Rep2>::type>::value,
int> = 0>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR duration<typename common_type<_Rep1, _Rep2>::type, _Period>
diff --git a/libcxx/include/__chrono/formatter.h b/libcxx/include/__chrono/formatter.h
index c1b57209b938d9..c579f03d729ce4 100644
--- a/libcxx/include/__chrono/formatter.h
+++ b/libcxx/include/__chrono/formatter.h
@@ -159,7 +159,7 @@ consteval bool __use_fraction() {
else if constexpr (__is_specialization_v<_Tp, chrono::zoned_time>)
return chrono::hh_mm_ss<typename _Tp::duration>::fractional_width;
# endif
- else if constexpr (chrono::__is_duration<_Tp>::value)
+ else if constexpr (chrono::__is_duration_v<_Tp>)
return chrono::hh_mm_ss<_Tp>::fractional_width;
else if constexpr (__is_hh_mm_ss<_Tp>)
return _Tp::fractional_width;
@@ -273,7 +273,7 @@ _LIBCPP_HIDE_FROM_ABI void __format_chrono_using_chrono_specs(
} break;
case _CharT('j'):
- if constexpr (chrono::__is_duration<_Tp>::value)
+ if constexpr (chrono::__is_duration_v<_Tp>)
// Converting a duration where the period has a small ratio to days
// may fail to compile. This due to loss of precision in the
// conversion. In order to avoid that issue convert to seconds as
@@ -285,7 +285,7 @@ _LIBCPP_HIDE_FROM_ABI void __format_chrono_using_chrono_specs(
break;
case _CharT('q'):
- if constexpr (chrono::__is_duration<_Tp>::value) {
+ if constexpr (chrono::__is_duration_v<_Tp>) {
__sstr << chrono::__units_suffix<_CharT, typename _Tp::period>();
break;
}
@@ -301,7 +301,7 @@ _LIBCPP_HIDE_FROM_ABI void __format_chrono_using_chrono_specs(
// MSVC STL ignores precision but uses separator
// FMT honours precision and has a bug for separator
// https://godbolt.org/z/78b7sMxns
- if constexpr (chrono::__is_duration<_Tp>::value) {
+ if constexpr (chrono::__is_duration_v<_Tp>) {
__sstr << std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{}"), __value.count());
break;
}
@@ -627,7 +627,7 @@ __format_chrono(const _Tp& __value,
if (__chrono_specs.empty())
__sstr << __value;
else {
- if constexpr (chrono::__is_duration<_Tp>::value) {
+ if constexpr (chrono::__is_duration_v<_Tp>) {
// A duration can be a user defined arithmetic type. Users may specialize
// numeric_limits, but they may not specialize is_signed.
if constexpr (numeric_limits<typename _Tp::rep>::is_signed) {
diff --git a/libcxx/include/__chrono/hh_mm_ss.h b/libcxx/include/__chrono/hh_mm_ss.h
index 57d2247fe6a3c8..c460b113050859 100644
--- a/libcxx/include/__chrono/hh_mm_ss.h
+++ b/libcxx/include/__chrono/hh_mm_ss.h
@@ -29,7 +29,7 @@ namespace chrono {
template <class _Duration>
class hh_mm_ss {
private:
- static_assert(__is_duration<_Duration>::value, "template parameter of hh_mm_ss must be a std::chrono::duration");
+ static_assert(__is_duration_v<_Duration>, "template parameter of hh_mm_ss must be a std::chrono::duration");
using __CommonType = common_type_t<_Duration, chrono::seconds>;
_LIBCPP_HIDE_FROM_ABI static constexpr uint64_t __pow10(unsigned __exp) {
diff --git a/libcxx/include/__chrono/time_point.h b/libcxx/include/__chrono/time_point.h
index aaf0b098f280e0..5e79fa5d257fa4 100644
--- a/libcxx/include/__chrono/time_point.h
+++ b/libcxx/include/__chrono/time_point.h
@@ -32,8 +32,7 @@ namespace chrono {
template <class _Clock, class _Duration = typename _Clock::duration>
class _LIBCPP_TEMPLATE_VIS time_point {
- static_assert(__is_duration<_Duration>::value,
- "Second template parameter of time_point must be a std::chrono::duration");
+ static_assert(__is_duration_v<_Duration>, "Second template parameter of time_point must be a std::chrono::duration");
public:
typedef _Clock clock;
@@ -91,17 +90,17 @@ time_point_cast(const time_point<_Clock, _Duration>& __t) {
}
#if _LIBCPP_STD_VER >= 17
-template <class _ToDuration, class _Clock, class _Duration, enable_if_t<__is_duration<_ToDuration>::value, int> = 0>
+template <class _ToDuration, class _Clock, class _Duration, enable_if_t<__is_duration_v<_ToDuration>, int> = 0>
inline _LIBCPP_HIDE_FROM_ABI constexpr time_point<_Clock, _ToDuration> floor(const time_point<_Clock, _Duration>& __t) {
return time_point<_Clock, _ToDuration>{chrono::floor<_ToDuration>(__t.time_since_epoch())};
}
-template <class _ToDuration, class _Clock, class _Duration, enable_if_t<__is_duration<_ToDuration>::value, int> = 0>
+template <class _ToDuration, class _Clock, class _Duration, enable_if_t<__is_duration_v<_ToDuration>, int> = 0>
inline _LIBCPP_HIDE_FROM_ABI constexpr time_point<_Clock, _ToDuration> ceil(const time_point<_Clock, _Duration>& __t) {
return time_point<_Clock, _ToDuration>{chrono::ceil<_ToDuration>(__t.time_since_epoch())};
}
-template <class _ToDuration, class _Clock, class _Duration, enable_if_t<__is_duration<_ToDuration>::value, int> = 0>
+template <class _ToDuration, class _Clock, class _Duration, enable_if_t<__is_duration_v<_ToDuration>, int> = 0>
inline _LIBCPP_HIDE_FROM_ABI constexpr time_point<_Clock, _ToDuration> round(const time_point<_Clock, _Duration>& __t) {
return time_point<_Clock, _ToDuration>{chrono::round<_ToDuration>(__t.time_since_epoch())};
}
diff --git a/libcxx/include/__chrono/zoned_time.h b/libcxx/include/__chrono/zoned_time.h
index 272fa047426758..f57e65c90a624e 100644
--- a/libcxx/include/__chrono/zoned_time.h
+++ b/libcxx/include/__chrono/zoned_time.h
@@ -58,7 +58,7 @@ struct zoned_traits<const time_zone*> {
template <class _Duration, class _TimeZonePtr = const time_zone*>
class zoned_time {
// [time.zone.zonedtime.ctor]/2
- static_assert(__is_duration<_Duration>::value,
+ static_assert(__is_duration_v<_Duration>,
"the program is ill-formed since _Duration is not a specialization of std::chrono::duration");
// The wording uses the constraints like
diff --git a/libcxx/include/ratio b/libcxx/include/ratio
index 7600e7c57ad74f..bd262437db34f6 100644
--- a/libcxx/include/ratio
+++ b/libcxx/include/ratio
@@ -462,9 +462,7 @@ struct _LIBCPP_TEMPLATE_VIS ratio_greater_equal : _BoolConstant<!ratio_less<_R1,
};
template <class _R1, class _R2>
-struct __ratio_gcd {
- typedef ratio<__static_gcd<_R1::num, _R2::num>, __static_lcm<_R1::den, _R2::den> > type;
-};
+using __ratio_gcd = ratio<__static_gcd<_R1::num, _R2::num>, __static_lcm<_R1::den, _R2::den> >;
#if _LIBCPP_STD_VER >= 17
template <class _R1, class _R2>
More information about the libcxx-commits
mailing list