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 起) |
在两个 duration 之间或 duration 与 tick 计数之间执行基本算术运算。
rep
是 Rep1
和 Rep2
的公共类型的 duration,并将转换后的 tick 计数乘以 s。这些重载仅当 s 可转换为 typename std::common_type<Rep1, Rep2>::type 时才参与重载决议。rep
是 Rep1
和 Rep2
的公共类型的 duration,并将转换后的 tick 计数除以 s。此重载仅当 s 可转换为 typename std::common_type<Rep1, Rep2>::type 且 Rep2
不是 duration
的特化时才参与重载决议。rep
是 Rep1
和 Rep2
的公共类型的 duration,并创建一个 duration,其 tick 计数是转换后 tick 计数除以 s 的余数。此重载仅当 s 可转换为 typename std::common_type<Rep1, Rep2>::type 且 Rep2
不是 duration
的特化时才参与重载决议。目录 |
[编辑] 参数
lhs | - | 运算符左侧的 duration |
rhs | - | 运算符右侧的时长 |
d | - | 混合参数运算符的 duration 参数 |
s | - | 混合参数运算符的非 duration 参数 |
[编辑] 返回值
假设 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++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 3050 | C++11 | 可转换性约束使用了非 const xvalue | 改用 const lvalue |