【问题标题】:Where is the 'unit'-ratio convenience typedef?“单位”比率便利类型定义在哪里?
【发布时间】:2020-10-20 08:06:55
【问题描述】:

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 

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

【问题讨论】:

  • 您列出的只是一些别名。没有人会阻止你使用 std::ratio&lt;1,1&gt; 或给它起个名字
  • 对不起,对我来说完全不清楚您要解决什么问题。您能否展示一些代码作为示例,您将使用 unit_ratio
  • 为什么要从秒转换为秒?因为你可以?
  • 但任何秒数都不是 1:1
  • 现在你必须提供它,因为你写了一个非常特殊的函数模板。该标准仅提供具有此默认值的函数模​​板,因此不需要std::ratio&lt;1, 1&gt;。该标准解决了其模板的问题。如果某些用户做了不寻常的事情,这些用户必须解决它。

标签: c++ numeric c++-standard-library


【解决方案1】:

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

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

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

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

你会写std::chrono::duration_cast&lt;std::chrono::seconds&gt;

std::ratio&lt;1,1&gt; 没有名字,因为你永远不需要它的名字。例如std::duration 已经有一个默认周期std::ratio&lt;1,1&gt;

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

using unit_ratio = std::ratio<1>;

【讨论】:

  • 极客琐事:将ratio 放入C++11 的提案的早期草案实际上确实有ratio&lt;1&gt; 的类型别名。我不记得我们叫它什么了。由于缺少指标名称,我们提前将其删除。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-02
  • 1970-01-01
  • 2018-03-14
  • 2016-10-27
  • 2015-10-28
  • 1970-01-01
相关资源
最近更新 更多