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つの期間の間、または期間とティックカウントの間で基本的な算術演算を実行します。
rep
が
Rep1
と
Rep2
の共通型であるものに変換し、変換後のティック数に
s
を乗算します。
これらのオーバーロードは、
s
が
typename
std::
common_type
<
Rep1, Rep2
>
::
type
に変換可能な場合にのみ、オーバーロード解決に参加します。
Rep1
と
Rep2
の共通型である
rep
を持つものに変換し、変換後のティック数を
s
で除算します。このオーバーロードは、
s
が
typename
std::
common_type
<
Rep1, Rep2
>
::
type
に変換可能であり、かつ
Rep2
が
duration
の特殊化でない場合にのみ、オーバーロード解決に参加します。
Rep1
と
Rep2
の共通型である
rep
を持つ期間に変換し、変換後のティックカウントを
s
で除算した剰余をティックカウントとする期間を作成する。このオーバーロードは、
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 であると仮定すると、以下のようになります:
例
#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 << " (seconds)\n"; using namespace std::chrono_literals; // 期間を数値で割る場合と、期間を別の期間で割る場合の違い: std::cout << "Dividing that by 2 minutes gives " << s / 2min << '\n' << "Dividing that by 2 gives " << (s / 2).count() << " seconds\n"; // 剰余演算子は、特定の期間が時間枠内のどこにあるかを // 決定するのに便利です。例えば、時間を時間、分、秒に // 分解する場合など: 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 |