Namespaces
Variants

operator+,-,*,/,% (std::chrono::duration)

From cppreference.net
(注:このHTML要素には翻訳対象のテキストコンテンツが含まれていないため、元の構造を保持したまま出力します)
template < class Rep1, class Period1, class Rep2, class Period2 >

typename std:: common_type < duration < Rep1,Period1 > , duration < Rep2,Period2 >> :: type
constexpr operator + ( const duration < Rep1,Period1 > & lhs,

const duration < Rep2,Period2 > & rhs ) ;
(1) (C++11以降)
template < class Rep1, class Period1, class Rep2, class Period2 >

typename std:: common_type < duration < Rep1,Period1 > , duration < Rep2,Period2 >> :: type
constexpr operator - ( const duration < Rep1,Period1 > & lhs,

const duration < Rep2,Period2 > & rhs ) ;
(2) (C++11以降)
template < class Rep1, class Period, class Rep2 >

duration < typename std:: common_type < Rep1,Rep2 > :: type , Period >
constexpr operator * ( const duration < Rep1,Period > & d,

const Rep2 & s ) ;
(3) (C++11以降)
template < class Rep1, class Rep2, class Period >

duration < typename std:: common_type < Rep1,Rep2 > :: type , Period >
constexpr operator * ( const Rep1 & s,

const duration < Rep2,Period > & d ) ;
(4) (C++11以降)
template < class Rep1, class Period, class Rep2 >

duration < typename std:: common_type < Rep1,Rep2 > :: type , Period >
constexpr operator / ( const duration < Rep1,Period > & d,

const Rep2 & s ) ;
(5) (C++11以降)
template < class Rep1, class Period1, class Rep2, class Period2 >

typename std:: common_type < Rep1,Rep2 > :: type
constexpr operator / ( const duration < Rep1,Period1 > & lhs,

const duration < Rep2,Period2 > & rhs ) ;
(6) (C++11以降)
template < class Rep1, class Period, class Rep2 >

duration < typename std:: common_type < Rep1,Rep2 > :: type , Period >
constexpr operator % ( const duration < Rep1, Period > & d,

const Rep2 & s ) ;
(7) (C++11以降)
template < class Rep1, class Period1, class Rep2, class Period2 >

typename std:: common_type < duration < Rep1,Period1 > , duration < Rep2,Period2 >> :: type
constexpr operator % ( const duration < Rep1,Period1 > & lhs,

const duration < Rep2,Period2 > & rhs ) ;
(8) (C++11以降)

2つの期間の間、または期間とティックカウントの間で基本的な算術演算を実行します。

1) 2つの期間を共通の型に変換し、変換後のティックカウントの合計をティックカウントとする期間を作成します。
2) 2つの期間を共通の型に変換し、変換後の lhs のティック数から rhs のティック数を減算したティック数を持つ期間を作成します。
3,4) 期間 d を、 rep Rep1 Rep2 の共通型であるものに変換し、変換後のティック数に s を乗算します。 これらのオーバーロードは、 s typename std:: common_type < Rep1, Rep2 > :: type に変換可能な場合にのみ、オーバーロード解決に参加します。
5) 期間 d Rep1 Rep2 の共通型である rep を持つものに変換し、変換後のティック数を s で除算します。このオーバーロードは、 s typename std:: common_type < Rep1, Rep2 > :: type に変換可能であり、かつ Rep2 duration の特殊化でない場合にのみ、オーバーロード解決に参加します。
6) 2つの期間を共通の型に変換し、変換後の lhs のティックカウントを変換後の rhs のティックカウントで除算します。この演算子の戻り値は期間ではないことに注意してください。
7) 期間 d Rep1 Rep2 の共通型である rep を持つ期間に変換し、変換後のティックカウントを s で除算した剰余をティックカウントとする期間を作成する。このオーバーロードは、 s typename std:: common_type < Rep1, Rep2 > :: type に変換可能であり、かつ Rep2 duration の特殊化でない場合にのみ、オーバーロード解決に参加する。
8) 2つの期間を共通の型に変換し、変換後のティックカウントの剰余をティックカウントとする期間を作成します。

目次

パラメータ

lhs - 演算子の左側のduration
rhs - 演算子の右側のduration
d - 混合引数演算子に対するduration引数
s - 混合引数演算子に対する非duration引数

戻り値

CD が関数の戻り値の型であり、 CD < A, B > = std:: common_type < A, B > :: type であると仮定すると、以下のようになります:

1) CD ( CD ( lhs ) . count ( ) + CD ( rhs ) . count ( ) )
2) CD ( CD ( lhs ) . count ( ) - CD ( rhs ) . count ( ) )
3,4) CD ( CD ( d ) . count ( ) * s )
5) CD ( CD ( d ) . count ( ) / s )
6) CD ( lhs ) . count ( ) / CD ( rhs ) . count ( ) (この演算子の戻り値の型はdurationではありません)
7) CD ( CD ( d ) . count ( ) % s )
8) CD ( CD ( lhs ) . count ( ) % CD ( rhs ) . count ( ) )

#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