“单位”比率便利类型定义在哪里?

问题描述

std::ratio为度量标准前缀(centi,deci,deca和hecto)提供了便利的typedef。

yocto   std::ratio<1,1000000000000000000000000>,if std::intmax_t can represent the denominator
zepto   std::ratio<1,1000000000000000000000>,if std::intmax_t can represent the denominator
atto    std::ratio<1,1000000000000000000>
femto   std::ratio<1,1000000000000000>
pico    std::ratio<1,1000000000000>
nano    std::ratio<1,1000000000>
micro   std::ratio<1,1000000>
milli   std::ratio<1,1000>
centi   std::ratio<1,100>
deci    std::ratio<1,10>
deca    std::ratio<10,1>
hecto   std::ratio<100,1>
kilo    std::ratio<1000,1>
mega    std::ratio<1000000,1>
giga    std::ratio<1000000000,1>
tera    std::ratio<1000000000000,1>
peta    std::ratio<1000000000000000,1>
exa     std::ratio<1000000000000000000,1>
zetta   std::ratio<1000000000000000000000,1>,if std::intmax_t can represent the numerator
yotta   std::ratio<1000000000000000000000000,if std::intmax_t can represent the numerator 

缺少什么?好吧...单位比率std::ratio<1,1>。 我知道该单位没有正式的度量标准前缀名称,但这并不意味着它不存在。 [ratio.si]中的任何地方都未提及单位前缀。所以我想知道:以“单位”比率工作的最典型的方法是什么?例如,duration_cast到整秒。

解决方法

以“单位”比率工作的最典型的方法是什么?

使用单位比例的最实用方法是不使用它。

有点像问乘1的最佳方法是什么。你没有。

例如,当duration_casting到整秒时。

您应该写std::chrono::duration_cast<std::chrono::seconds>

std::ratio<1,1>没有名称,因为您不需要它的名称。例如,std::duration的默认期限为std::ratio<1,1>

如果您仍然想给它起个名字,您可以这样做:

using unit_ratio = std::ratio<1>;