名前空間
変種
操作

std::common_type<std::chrono::duration>

From cppreference.com
< cpp‎ | chrono‎ | duration
 
 
 
 
ヘッダー <chrono> で定義
template< class Rep1, class Period1, class Rep2, class Period2 >

struct common_type<std::chrono::duration<Rep1, Period1>,

                   std::chrono::duration<Rep2, Period2>>;
(C++11以降)

type という名前の型を公開します。これは、2つの std::chrono::duration の共通型であり、その期間は Period1Period2 の最大公約数です。

目次

[編集] メンバ型

メンバ型 定義
type std::chrono::duration<typename std::common_type<Rep1, Rep2>::type, /* 注を参照 */>

[編集] 注記

結果の期間の期間は、Period1::numPeriod2::num の最大公約数と、Period1::denPeriod2::den の最小公倍数の比を形成することによって計算できます。

[編集]

#include <chrono>
#include <iostream>
#include <type_traits>
 
// std::chrono already finds the greatest common divisor,
// likely using std::common_type<>. We make the type
// deduction externally. 
 
template<typename T,typename S>
constexpr auto durationDiff(const T& t, const S& s)
    -> typename std::common_type<T,S>::type
{
    typedef typename std::common_type<T,S>::type Common;
    return Common(t) - Common(s);
}
 
int main() 
{
    using namespace std::literals;
 
    constexpr auto ms = 30ms;
    constexpr auto us = 1100us;
    constexpr auto diff = durationDiff(ms, us);
 
    std::cout << ms << " - " << us << " = " << diff << '\n';
}

出力

30ms - 1100us = 28900us

[編集] 関連項目

std::common_type 特性を特殊化する
(クラス テンプレートの特殊化) [edit]
型のグループの共通の型を決定する
(クラステンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)