命名空间
变体
操作

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

来自 cppreference.cn
< 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 的类型,它是两个 std::chrono::duration 的公共类型,其周期是 Period1Period2 的最大公约数。

目录

[编辑] 成员类型

成员类型 定义
type std::chrono::duration<typename std::common_type<Rep1, Rep2>::type, /* see note */>

[编辑] 注意

结果 duration 的周期可以通过计算 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 特性
(类模板特化) [编辑]
确定一组类型的公共类型
(类模板) [编辑]