std::common_type<std::chrono::duration>
来自cppreference.com
| 在标头 <chrono> 定义
|
||
| |
(C++11 起) | |
暴露名为 type 的类型,它是两个 std::chrono::duration 的公共类型,其周期为 Period1 与 Period2 的最大公约数。
成员类型
| 成员类型 | 定义 |
type
|
std::chrono::duration<typename std::common_type<Rep1, Rep2>::type, /* 见注解 */>
|
注解
所产生的时长周期能通过构成 Period1::num 和 Period2::num 的最大公约数与 Period1::den 和 Period2::den 的最小公倍数之间的比值计算而得。
示例
Run this code
#include <chrono>
#include <iostream>
#include <type_traits>
// std::chrono 已找到最大公约数,类似使用 std::common_type<>。
// 我们令类型推导外化。
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) |
确定一组类型的公共类型 (类模板) |