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) | (自 C++11 起) |
template< class Rep1, class Period1, class Rep2, class Period2 > typename std::common_type<duration<Rep1,Period1>, duration<Rep2,Period2>>::type |
(2) | (自 C++11 起) |
template< class Rep1, class Period, class Rep2 > duration<typename std::common_type<Rep1,Rep2>::type, Period> |
(3) | (自 C++11 起) |
template< class Rep1, class Rep2, class Period > duration<typename std::common_type<Rep1,Rep2>::type, Period> |
(4) | (自 C++11 起) |
template< class Rep1, class Period, class Rep2 > duration<typename std::common_type<Rep1,Rep2>::type, Period> |
(5) | (自 C++11 起) |
template< class Rep1, class Period1, class Rep2, class Period2 > typename std::common_type<Rep1,Rep2>::type |
(6) | (自 C++11 起) |
template< class Rep1, class Period, class Rep2 > duration<typename std::common_type<Rep1,Rep2>::type, Period> |
(7) | (自 C++11 起) |
template< class Rep1, class Period1, class Rep2, class Period2 > typename std::common_type<duration<Rep1,Period1>, duration<Rep2,Period2>>::type |
(8) | (自 C++11 起) |
执行两个持续时间之间或持续时间和滴答计数之间的基本算术运算。
rep
是 Rep1
和 Rep2
之间公共类型的持续时间,并将转换后的滴答计数乘以 s。 只有当 s 可转换为 typename std::common_type<Rep1, Rep2>::type 时,这些重载才会参与重载解析。rep
是 Rep1
和 Rep2
之间公共类型的持续时间,并将转换后的滴答计数除以 s。 只有当 s 可转换为 typename std::common_type<Rep1, Rep2>::type 且 Rep2
不是 duration
的特化时,此重载才会参与重载解析。内容 |
[编辑] 参数
lhs | - | 运算符左侧的时长 |
rhs | - | 运算符右侧的时长 |
d | - | 混合参数运算符的时长参数 |
s | - | 混合参数运算符的非时长参数 |
[编辑] 返回值
假设 CD 是函数返回值类型,且 CD<A, B> = std::common_type<A, B>::type,则
[编辑] 示例
#include <chrono> #include <iostream> int main() { // Simple arithmetic: 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 << " (seconds)\n"; using namespace std::chrono_literals; // Difference between dividing a duration by a number // and dividing a duration by another duration: std::cout << "Dividing that by 2 minutes gives " << s / 2min << '\n' << "Dividing that by 2 gives " << (s / 2).count() << " seconds\n"; // The remainder operator is useful in determining where // in a time frame is this particular duration, e.g. to // break it down into hours, minutes, and seconds: std::cout << s << " (seconds) = " << std::chrono::duration_cast<std::chrono::hours>( s) << " (hour) + " << std::chrono::duration_cast<std::chrono::minutes>( s % 1h) << " (minutes) + " << std::chrono::duration_cast<std::chrono::seconds>( s % 1min) << " (seconds)\n"; constexpr auto sun_earth_distance{150'000'000ULL}; // km constexpr auto speed_of_light{300000ULL}; // km/sec std::chrono::seconds t(sun_earth_distance / speed_of_light); // sec std::cout << "A photon flies from the Sun to the Earth in " << t / 1min << " minutes " << t % 1min << " (seconds)\n"; }
输出
1 hour + 2*10 min + 70/10 sec = 4807s (seconds) Dividing that by 2 minutes gives 40 Dividing that by 2 gives 2403 seconds 4807s (seconds) = 1h (hour) + 20min (minutes) + 7s (seconds) A photon flies from the Sun to the Earth in 8 minutes 20s (seconds)
[编辑] 缺陷报告
以下更改行为的缺陷报告被追溯应用于之前发布的 C++ 标准。
DR | 应用于 | 已发布的行为 | 正确的行为 |
---|---|---|---|
LWG 3050 | C++11 | 可转换性约束使用非 const xvalue | 使用 const 左值代替 |