名前空間
変種

std::common_type(std::chrono::duration)

提供: cppreference.com
 
 
ユーティリティライブラリ
汎用ユーティリティ
日付と時間
関数オブジェクト
書式化ライブラリ (C++20)
(C++11)
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)
スワップと型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
一般的な語彙の型
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

初等文字列変換
(C++17)
(C++17)
 
日付と時間のユーティリティ
(C++11)
(C++11)
時刻
(C++20)



(C++20)(C++20)(C++20)(C++20)
時計
(C++20)
                                             
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
カレンダー
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
タイムゾーン
(C++20)
(C++20)
(C++20)
(C++20)
C スタイルの日付と時間
 
 
<tbody> </tbody>
template <class Rep1, class Period1, class Rep2, class Period2> struct common_type<std::chrono::duration<Rep1, Period1>, std::chrono::duration<Rep2, Period2>> { typedef std::chrono::duration< typename std::common_type<Rep1, Rep2>::type, /*see note*/> type; };
(C++11以上)

2つの std::chrono::duration の共通型となるメンバ型 type を提供します。

ノート

結果となる duration の period は Period1Period2 の最大公約数です。

#include <iostream>
#include <chrono>

// std::chrono already finds the greatest common divisor,
// likely using std::common_type<>. We make the type
// deduction externally. 

template <typename T,typename S>
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() 
{
    typedef std::chrono::milliseconds milliseconds;
    typedef std::chrono::microseconds microseconds;
    
    auto ms = milliseconds(30);
    auto us = microseconds(1100);
    
    std::cout << ms.count() << "ms - " << us.count() << "us = " 
              << durationDiff(ms,us).count() <<  "\n";
}

出力:

30ms - 1100us = 28900

関連項目

指定された型のグループの共通型を調べます
(クラステンプレート) [edit]
Morty Proxy This is a proxified and sanitized view of the page, visit original site.