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以降) |
2つのduration、またはdurationとチック数の間の基本的な算術演算を実行します。
rep が `Rep1` と `Rep2` の共通型となるようなdurationに変換し、変換後のティック数を `s` で乗算します。これらのオーバーロードは、`s` が `typename std::common_type<Rep1, Rep2>::type` に変換可能である場合にのみオーバーロード解決に参加します。rep が `Rep1` と `Rep2` の共通型となるようなdurationに変換し、変換後のティック数を `s` で除算します。このオーバーロードは、`s` が `typename std::common_type<Rep1, Rep2>::type` に変換可能であり、かつ `Rep2` が `duration` の特殊化ではない場合にのみオーバーロード解決に参加します。rep が `Rep1` と `Rep2` の共通型となるようなdurationに変換し、変換後のティック数を `s` で割った余りをティック数とするdurationを作成します。このオーバーロードは、`s` が `typename std::common_type<Rep1, Rep2>::type` に変換可能であり、かつ `Rep2` が `duration` の特殊化ではない場合にのみオーバーロード解決に参加します。目次 |
[編集] パラメータ
| lhs | - | 演算子の左辺のduration |
| rhs | - | 演算子の右辺にあるduration |
| d | - | 混合引数演算子のduration引数 |
| s | - | 混合引数演算子のduration以外の引数 |
[編集] 戻り値
CD が関数戻り値の型であり、CD<A, B> = std::common_type<A, B>::type であると仮定すると、
CD(CD(lhs).count() + CD(rhs).count())CD(CD(lhs).count() - CD(rhs).count())CD(CD(d).count() * s)CD(CD(d).count() / s)CD(lhs).count() / CD(rhs).count() (この演算子の戻り値はdurationではありません)CD(CD(d).count() % s)CD(CD(lhs).count() % CD(rhs).count())[編集] 例
#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 | convertibility constraint used non-const xvalue | use const lvalues instead |