(5) 在 chrono :: duration < int ,ratio<N,D> > 的源码定义里,使用了 common_type_t 这个类型定义,这是定义了什么类型呢? 测试以下:
++ 对应的源码为:
++ 接着也测试一下这两个奇特的运算符函数,是否真的是这么哥意思,反汇编一下 :
++ 以及 负号运算符 :
(6) duration(min)()不提倡 ,这样书写函数的返回值类型与函数名,根本不明显:
(7) _Lcm 是最小公倍数 Least Common Multiple, LCM , 还有最大公约数 GCD ,在代码里实现很神奇的数学计算 :
template < intmax_t _Ax, intmax_t _Bx, bool=( (_Ax / _Gcd<_Ax, _Bx>::value) <= INTMAX_MAX / _Bx ) >
struct _Lcm : integral_constant<intmax_t, (_Ax / _Gcd<_Ax, _Bx>::value) * _Bx> {};
template <intmax_t _Ax, intmax_t _Bx> // 模板类的泛化与特化
struct _Lcm<_Ax, _Bx, false> {}; // _Lcm 是最小公倍数 Least Common Multiple, LCM
//*************************************************************************************************
// (1)当 _Ax 与 _Bx 都为正数时,选择此模板 。 这个求最大公约数的方法很巧妙!!!!!!
template <intmax_t _Ax, intmax_t _Bx> // greatest common divisor 最大公约数
struct _GcdX : _GcdX<_Bx, _Ax % _Bx>::type {}; // computes GCD of _Ax and _Bx
// (2)计算 _Ax 与 0 的最大公约数,认为是 _Ax 本身
template <intmax_t _Ax>
struct _GcdX<_Ax, 0> : integral_constant<intmax_t, _Ax> {}; // computes GCD of _Ax and 0
//-------------------------------------
// (a)先统一转换为正数,再求公约数
template <intmax_t _Ax, intmax_t _Bx>
struct _Gcd : _GcdX<_Abs<_Ax>::value, _Abs<_Bx>::value>::type {}; // computes GCD of abs(_Ax) and abs(_Bx)
// (b)0 与 0 的最大公约数,认为是 1
template <> // contrary to mathematical convention; avoids division by 0 in ratio_less
struct _Gcd<0, 0> : integral_constant<intmax_t, 1> { }; // 与数学惯例相反,避免除以 0
(8)
谢谢