用好运算重载符,让代码阅读可读性更高。
一个例子如下
namespace time_literals
{// User-defined integer literals for different time units.
// The base unit is hrt_abstime in microsecondsconstexpr hrt_abstime operator "" _s(unsigned long long seconds)
{return hrt_abstime(seconds * 1000000ULL);
}constexpr hrt_abstime operator "" _ms(unsigned long long milliseconds)
{return hrt_abstime(milliseconds * 1000ULL);
}constexpr hrt_abstime operator "" _us(unsigned long long microseconds)
{return hrt_abstime(microseconds);
}} /* namespace time_literals */
使用
(time_now_us - _min_thrust_start) > 8_s
一看就知道是8秒,否则直接写一个8,也不知道单位,就加大了代码阅读难度。