std::common_type<std::chrono::duration>
來自 cppreference.com
定義於標頭檔案 <chrono> |
||
template< class Rep1, class Period1, class Rep2, class Period2 > struct common_type<std::chrono::duration<Rep1, Period1>, |
(C++11 起) | |
公開名為 type
的型別,它是兩個 std::chrono::duration 的公共型別,其週期是 Period1
和 Period2
的最大公約數。
目錄 |
[編輯] 成員型別
成員型別 | 定義 |
型別
|
std::chrono::duration<typename std::common_type<Rep1, Rep2>::type, /* 參見說明 */> |
[編輯] 注意
結果 duration 的週期可以透過以下方式計算:形成 Period1::num 和 Period2::num 的最大公約數的比率,以及 Period1::den 和 Period2::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 特性 (類模板特化) | |
(C++11) |
確定一組型別的公共型別 (類模板) |