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 |