operator+,-,*,/,%(std::chrono::duration)
template< class Rep1, class Period1, class Rep2, class Period2 > typename std::common_type<duration<Rep1,Period1>, duration<Rep2,Period2>>::type |
(1) | |
template< class Rep1, class Period1, class Rep2, class Period2 > typename std::common_type<duration<Rep1,Period1>, duration<Rep2,Period2>>::type |
(2) | |
template< class Rep1, class Period, class Rep2 > duration<typename std::common_type<Rep1,Rep2>::type, Period> |
(3) | |
template< class Rep1, class Rep2, class Period > duration<typename std::common_type<Rep1,Rep2>::type, Period> |
(4) | |
template< class Rep1, class Period, class Rep2 > duration<typename std::common_type<Rep1,Rep2>::type, Period> |
(5) | |
template< class Rep1, class Period1, class Rep2, class Period2 > typename std::common_type<Rep1,Rep2>::type |
(6) | |
template< class Rep1, class Period, class Rep2 > duration<typename std::common_type<Rep1,Rep2>::type, Period> |
(7) | |
template< class Rep1, class Period1, class Rep2, class Period2 > typename std::common_type<duration<Rep1,Period1>, duration<Rep2,Period2>>::type |
(8) | |
在二个 duration 或 duration 和计次数之间进行基本算术运算。
lhs
的计次数减去 rhs
的计次数的 duration 。d
到其 rep
为 Rep1
与 Rep2
之间的共用类型的 duration ,并将转换后的计次数乘 s
。d
到其 rep
为 Rep1
与 Rep2
之间的共用类型的 duration ,并将转换后的计次数除以 s
。lhs
转换后的计次数除以 rhs
转换后的计次数。注意此运算符的返回值不是 duration 。d
到其 rep
为 Rep1
与 Rep2
之间的共用类型的 duration ,并创建计次数为此时长的计次数除以 s
的 duration 。本节未完成 原因:列出“不参与重载决议,除非”的制约 |
参数
lhs | - | 运算符左侧的 duration |
rhs | - | 运算符右侧的 duration |
d | - | 混合参数运算符的时长参数 |
s | - | 混合参数运算符的计次参数 |
返回值
假设 CD 为函数返回类型且 CR<A, B> = std::common_type<A, B>::type ,则:
示例
#include <chrono> #include <iostream> int main() { // 简单算术 std::chrono::seconds s = std::chrono::hours(1) + 2*std::chrono::minutes(10) + std::chrono::seconds(70)/10; std::cout << "1 hour + 2*10 min + 70/10 sec = " << s.count() << " seconds\n"; // 时长除以一个数和时长除以另一时长的区别 std::cout << "Dividing that by 2 minutes gives " << s / std::chrono::minutes(2) << '\n'; std::cout << "Dividing that by 2 gives " << (s / 2).count() << " seconds\n"; // 余数运算符在确定此特定时长在时间框架的场合有用, // 例如,拆分它为时、分和秒: std::cout << s.count() << " seconds is " << std::chrono::duration_cast<std::chrono::hours>( s ).count() << " hours, " << std::chrono::duration_cast<std::chrono::minutes>( s % std::chrono::hours(1) ).count() << " minutes, " << std::chrono::duration_cast<std::chrono::seconds>( s % std::chrono::minutes(1) ).count() << " seconds\n"; }
输出:
1 hour + 2*10 min + 70/10 sec = 4807 seconds Dividing that by 2 minutes gives 40 Dividing that by 2 gives 2403 seconds 4807 seconds is 1 hours, 20 minutes, 7 seconds