(3)c++ __msvc_chrono.hpp 时间类:类 common_type_t<duration>,取负运算 operator-(),duration(min)()不提倡,缩写LCM GCD

(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)

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值