'unit'-ratio 方便 typedef 在哪里?

Tom*_*Tom 0 c++ numeric c++-standard-library

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, 1>, if std::intmax_t can represent the numerator 
Run Code Online (Sandbox Code Playgroud)

缺少了什么?嗯……单位比std::ratio<1,1>。我知道该单位没有官方的公制前缀名称,但这并不意味着它不存在。[ratio.si] 中没有提到单位前缀。所以我想知道:使用“单位”比率的最典型的方法是什么?例如,当duration_cast-ing 到整秒时。

for*_*818 7

使用“单位”比率的最典型方式是什么?

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

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

例如,当duration_cast-ing 为整秒时。

你会写std::chrono::duration_cast<std::chrono::seconds>.

std::ratio<1,1>没有名字,因为你永远不需要它的名字。例如std::duration已经有一个默认期限std::ratio<1,1>

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

using unit_ratio = std::ratio<1>;
Run Code Online (Sandbox Code Playgroud)

  • 极客琐事:将“ratio”放入 C++11 的提案的早期草案实际上确实有一个“ratio&lt;1&gt;”的类型别名。我不记得我们叫它什么了。由于缺乏度量名称,我们提前将其撤回。 (2认同)