首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

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

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; };

?

(since C++11)

公开名为type,这是两种常见的类型。std::chrono::durationS.

产生的持续时间的周期是Period1Period2...

二次

代码语言:javascript
复制
#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";
}

二次

产出:

二次

代码语言:javascript
复制
30ms - 1100us = 28900

二次

另见

common_type (C++11)

determines the common type of a group of types (class template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com