Skip to content

Commit e3c958a

Browse files
authored
[libc++] Replace template structs with template variables in <ratio> (llvm#115782)
This avoids a bit of boilerplate.
1 parent 6d8d9fc commit e3c958a

File tree

2 files changed

+52
-66
lines changed

2 files changed

+52
-66
lines changed

libcxx/include/__chrono/duration.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,14 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _ToDuration round(const duration<
159159
template <class _Rep, class _Period>
160160
class _LIBCPP_TEMPLATE_VIS duration {
161161
static_assert(!__is_duration<_Rep>::value, "A duration representation can not be a duration");
162-
static_assert(__is_ratio<_Period>::value, "Second template parameter of duration must be a std::ratio");
162+
static_assert(__is_ratio_v<_Period>, "Second template parameter of duration must be a std::ratio");
163163
static_assert(_Period::num > 0, "duration period must be positive");
164164

165165
template <class _R1, class _R2>
166166
struct __no_overflow {
167167
private:
168-
static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
169-
static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
168+
static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>;
169+
static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>;
170170
static const intmax_t __n1 = _R1::num / __gcd_n1_n2;
171171
static const intmax_t __d1 = _R1::den / __gcd_d1_d2;
172172
static const intmax_t __n2 = _R2::num / __gcd_n1_n2;

libcxx/include/ratio

Lines changed: 49 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -99,38 +99,26 @@ _LIBCPP_BEGIN_NAMESPACE_STD
9999
// __static_gcd
100100

101101
template <intmax_t _Xp, intmax_t _Yp>
102-
struct __static_gcd {
103-
static const intmax_t value = __static_gcd<_Yp, _Xp % _Yp>::value;
104-
};
102+
inline const intmax_t __static_gcd = __static_gcd<_Yp, _Xp % _Yp>;
105103

106104
template <intmax_t _Xp>
107-
struct __static_gcd<_Xp, 0> {
108-
static const intmax_t value = _Xp;
109-
};
105+
inline const intmax_t __static_gcd<_Xp, 0> = _Xp;
110106

111107
template <>
112-
struct __static_gcd<0, 0> {
113-
static const intmax_t value = 1;
114-
};
108+
inline const intmax_t __static_gcd<0, 0> = 1;
115109

116110
// __static_lcm
117111

118112
template <intmax_t _Xp, intmax_t _Yp>
119-
struct __static_lcm {
120-
static const intmax_t value = _Xp / __static_gcd<_Xp, _Yp>::value * _Yp;
121-
};
113+
inline const intmax_t __static_lcm = _Xp / __static_gcd<_Xp, _Yp> * _Yp;
122114

123115
template <intmax_t _Xp>
124-
struct __static_abs {
125-
static const intmax_t value = _Xp < 0 ? -_Xp : _Xp;
126-
};
116+
inline const intmax_t __static_abs = _Xp < 0 ? -_Xp : _Xp;
127117

128118
template <intmax_t _Xp>
129-
struct __static_sign {
130-
static const intmax_t value = _Xp == 0 ? 0 : (_Xp < 0 ? -1 : 1);
131-
};
119+
inline const intmax_t __static_sign = _Xp == 0 ? 0 : (_Xp < 0 ? -1 : 1);
132120

133-
template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
121+
template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp> >
134122
class __ll_add;
135123

136124
template <intmax_t _Xp, intmax_t _Yp>
@@ -161,7 +149,7 @@ public:
161149
static const intmax_t value = _Xp + _Yp;
162150
};
163151

164-
template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
152+
template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp> >
165153
class __ll_sub;
166154

167155
template <intmax_t _Xp, intmax_t _Yp>
@@ -197,8 +185,8 @@ class __ll_mul {
197185
static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
198186
static const intmax_t min = nan + 1;
199187
static const intmax_t max = -min;
200-
static const intmax_t __a_x = __static_abs<_Xp>::value;
201-
static const intmax_t __a_y = __static_abs<_Yp>::value;
188+
static const intmax_t __a_x = __static_abs<_Xp>;
189+
static const intmax_t __a_y = __static_abs<_Yp>;
202190

203191
static_assert(_Xp != nan && _Yp != nan && __a_x <= max / __a_y, "overflow in __ll_mul");
204192

@@ -239,13 +227,13 @@ public:
239227

240228
template <intmax_t _Num, intmax_t _Den = 1>
241229
class _LIBCPP_TEMPLATE_VIS ratio {
242-
static_assert(__static_abs<_Num>::value >= 0, "ratio numerator is out of range");
230+
static_assert(__static_abs<_Num> >= 0, "ratio numerator is out of range");
243231
static_assert(_Den != 0, "ratio divide by 0");
244-
static_assert(__static_abs<_Den>::value > 0, "ratio denominator is out of range");
245-
static _LIBCPP_CONSTEXPR const intmax_t __na = __static_abs<_Num>::value;
246-
static _LIBCPP_CONSTEXPR const intmax_t __da = __static_abs<_Den>::value;
247-
static _LIBCPP_CONSTEXPR const intmax_t __s = __static_sign<_Num>::value * __static_sign<_Den>::value;
248-
static _LIBCPP_CONSTEXPR const intmax_t __gcd = __static_gcd<__na, __da>::value;
232+
static_assert(__static_abs<_Den> > 0, "ratio denominator is out of range");
233+
static _LIBCPP_CONSTEXPR const intmax_t __na = __static_abs<_Num>;
234+
static _LIBCPP_CONSTEXPR const intmax_t __da = __static_abs<_Den>;
235+
static _LIBCPP_CONSTEXPR const intmax_t __s = __static_sign<_Num> * __static_sign<_Den>;
236+
static _LIBCPP_CONSTEXPR const intmax_t __gcd = __static_gcd<__na, __da>;
249237

250238
public:
251239
static _LIBCPP_CONSTEXPR const intmax_t num = __s * __na / __gcd;
@@ -261,9 +249,10 @@ template <intmax_t _Num, intmax_t _Den>
261249
_LIBCPP_CONSTEXPR const intmax_t ratio<_Num, _Den>::den;
262250

263251
template <class _Tp>
264-
struct __is_ratio : false_type {};
252+
inline const bool __is_ratio_v = false;
253+
265254
template <intmax_t _Num, intmax_t _Den>
266-
struct __is_ratio<ratio<_Num, _Den> > : true_type {};
255+
inline const bool __is_ratio_v<ratio<_Num, _Den> > = true;
267256

268257
typedef ratio<1LL, 1000000000000000000LL> atto;
269258
typedef ratio<1LL, 1000000000000000LL> femto;
@@ -285,11 +274,11 @@ typedef ratio<1000000000000000000LL, 1LL> exa;
285274
template <class _R1, class _R2>
286275
struct __ratio_multiply {
287276
private:
288-
static const intmax_t __gcd_n1_d2 = __static_gcd<_R1::num, _R2::den>::value;
289-
static const intmax_t __gcd_d1_n2 = __static_gcd<_R1::den, _R2::num>::value;
277+
static const intmax_t __gcd_n1_d2 = __static_gcd<_R1::num, _R2::den>;
278+
static const intmax_t __gcd_d1_n2 = __static_gcd<_R1::den, _R2::num>;
290279

291-
static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
292-
static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
280+
static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
281+
static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
293282

294283
public:
295284
typedef typename ratio< __ll_mul<_R1::num / __gcd_n1_d2, _R2::num / __gcd_d1_n2>::value,
@@ -311,11 +300,11 @@ struct _LIBCPP_TEMPLATE_VIS ratio_multiply : public __ratio_multiply<_R1, _R2>::
311300
template <class _R1, class _R2>
312301
struct __ratio_divide {
313302
private:
314-
static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
315-
static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
303+
static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>;
304+
static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>;
316305

317-
static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
318-
static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
306+
static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
307+
static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
319308

320309
public:
321310
typedef typename ratio< __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
@@ -337,11 +326,11 @@ struct _LIBCPP_TEMPLATE_VIS ratio_divide : public __ratio_divide<_R1, _R2>::type
337326
template <class _R1, class _R2>
338327
struct __ratio_add {
339328
private:
340-
static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
341-
static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
329+
static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>;
330+
static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>;
342331

343-
static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
344-
static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
332+
static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
333+
static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
345334

346335
public:
347336
typedef typename ratio_multiply<
@@ -366,11 +355,11 @@ struct _LIBCPP_TEMPLATE_VIS ratio_add : public __ratio_add<_R1, _R2>::type {};
366355
template <class _R1, class _R2>
367356
struct __ratio_subtract {
368357
private:
369-
static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
370-
static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
358+
static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>;
359+
static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>;
371360

372-
static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
373-
static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
361+
static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
362+
static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
374363

375364
public:
376365
typedef typename ratio_multiply<
@@ -396,14 +385,14 @@ struct _LIBCPP_TEMPLATE_VIS ratio_subtract : public __ratio_subtract<_R1, _R2>::
396385

397386
template <class _R1, class _R2>
398387
struct _LIBCPP_TEMPLATE_VIS ratio_equal : _BoolConstant<(_R1::num == _R2::num && _R1::den == _R2::den)> {
399-
static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
400-
static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
388+
static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
389+
static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
401390
};
402391

403392
template <class _R1, class _R2>
404393
struct _LIBCPP_TEMPLATE_VIS ratio_not_equal : _BoolConstant<!ratio_equal<_R1, _R2>::value> {
405-
static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
406-
static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
394+
static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
395+
static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
407396
};
408397

409398
// ratio_less
@@ -439,10 +428,7 @@ struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, _M2> {
439428
static const bool value = __ratio_less1<ratio<_R1::den, _M1>, ratio<_R2::den, _M2>, !_Odd>::value;
440429
};
441430

442-
template <class _R1,
443-
class _R2,
444-
intmax_t _S1 = __static_sign<_R1::num>::value,
445-
intmax_t _S2 = __static_sign<_R2::num>::value>
431+
template <class _R1, class _R2, intmax_t _S1 = __static_sign<_R1::num>, intmax_t _S2 = __static_sign<_R2::num> >
446432
struct __ratio_less {
447433
static const bool value = _S1 < _S2;
448434
};
@@ -459,31 +445,31 @@ struct __ratio_less<_R1, _R2, -1LL, -1LL> {
459445

460446
template <class _R1, class _R2>
461447
struct _LIBCPP_TEMPLATE_VIS ratio_less : _BoolConstant<__ratio_less<_R1, _R2>::value> {
462-
static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
463-
static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
448+
static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
449+
static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
464450
};
465451

466452
template <class _R1, class _R2>
467453
struct _LIBCPP_TEMPLATE_VIS ratio_less_equal : _BoolConstant<!ratio_less<_R2, _R1>::value> {
468-
static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
469-
static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
454+
static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
455+
static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
470456
};
471457

472458
template <class _R1, class _R2>
473459
struct _LIBCPP_TEMPLATE_VIS ratio_greater : _BoolConstant<ratio_less<_R2, _R1>::value> {
474-
static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
475-
static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
460+
static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
461+
static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
476462
};
477463

478464
template <class _R1, class _R2>
479465
struct _LIBCPP_TEMPLATE_VIS ratio_greater_equal : _BoolConstant<!ratio_less<_R1, _R2>::value> {
480-
static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
481-
static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
466+
static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
467+
static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
482468
};
483469

484470
template <class _R1, class _R2>
485471
struct __ratio_gcd {
486-
typedef ratio<__static_gcd<_R1::num, _R2::num>::value, __static_lcm<_R1::den, _R2::den>::value> type;
472+
typedef ratio<__static_gcd<_R1::num, _R2::num>, __static_lcm<_R1::den, _R2::den> > type;
487473
};
488474

489475
#if _LIBCPP_STD_VER >= 17

0 commit comments

Comments
 (0)