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;

};
(C++11 起)

暴露名为 type 的类型,它是二个 std::chrono::duration 的共用类型。

注意

结果 duration 的周期是 Period1Period2 的最大公约数。

示例

#include <iostream>
#include <chrono>
 
// std::chrono 已找到最大公约数,类似使用 std::common_type<> 。
// 我们令类型推导外化。 
 
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

参阅

(C++11)
确定一组类型的共用类型
(类模板)

版本历史

  • (当前 | 先前 2017年8月23日 (三) 23:58Fruderica讨论 | 贡献. . (1,481字节) (+360). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 16:41P12bot讨论 | 贡献 . . (1,121字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 07:20P12bot讨论 | 贡献 . . (1,121字节) (+2). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 15:37P12bot讨论 | 贡献 . . (1,119字节) (+313). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 09:50P12讨论 | 贡献 . . (806字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (806字节) (-15). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 13:14P12讨论 | 贡献 . . (821字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (821字节) (+821). . (Translated from the English version using Google Translate)