C ++ 0x使用<ratio>表示更安全的Length类型

如何解决C ++ 0x使用<ratio>表示更安全的Length类型

|| 当读取
<ratio>
<chrono>
时,我试图想象一个防止意外转换错误的
Length
类型。 这就是我得到的:
#include <iostream>
#include <ratio>
using namespace std;

template<typename Scale>
struct Length  {
  long long val_;
  Length(long long val) : val_{val} {}
  Length() = default;
  Length(const Length&) = default;
  Length& operator=(const Length&) = default;
  // conversion
  template<typename Scale2>
  Length(const Length<Scale2> &other)
    : val_{ other.val_*(Scale2::num*Scale::den)/(Scale2::den*Scale::num) }
  { }
  // access
  long long value() const { return val_; }
}; 
typedef Length<ratio<1>> m;
typedef Length<kilo> km;
typedef Length<milli> mm;
typedef Length<ratio<1000,1094>> yard;
像这样使用
int main() {
  km len_km = 300;
  mm len_mm = len_km;
  cout << \" millimeter:\" << len_mm.value() << endl;
  cout << \" m:\" << m{len_km}.value() << endl;
  cout << \" yd:\" << yard{len_km}.value() << endl;
}
现在,我可以添加所有的
+
*
操作来变得非常舒适... :-) 我想知道: 无论如何,可以更容易地访问
<chrono>
中定义的
duration
time_point
的算术功能吗?我可以用那些减少2英镑的精力吗? 在转换构造函数中,编译时常量
(Scale2::num*Scale::den)/(Scale2::den*Scale::num)
似乎很危险(分数/下溢?),但是我无法找到更好的元编程方式,这里有任何提示吗?     

解决方法

              有没有更容易获得的   算术设施   
duration
time_point
在   
<chrono>
?我可以用那些吗   减少ѭ2的工作量?    对于“混合模式”算术和比较,您可以利用ѭ16来定义返回类型。
duration
专攻
common_type
是Period1和Period2的最大公约数,其中Period1和Period2是算术或比较运算中涉及的两个ѭ19。您可以像这样使用它:
template <typename Scale1,typename Scale2>
  typename std::common_type<Length<Scale1>,Length<Scale2>>::type
  operator+(Length<Scale1> x,Length<Scale2> y);
不幸的是,您将不得不重新发明如何在编译时获得两个19的最大公约数。从无符号long long的编译时gcd和lcm元函数开始。 嗯...或者您可能可以根据已经为
duration
做过的一项来使
common_type
的专业化为基础。您可以将结果
duration
\
period
重新解释为your2ѭ的比例因子。我还没有原型,只是一个想法。      编译时常数   conversion11ѭ在转换中似乎很危险   构造函数(分数/下溢?),   但我想不出更好的办法   元编程方式,这里有什么提示吗?    同意
duration
通过以下方式处理此问题:
template <class Rep2,class Period2>
  constexpr duration(const duration<Rep2,Period2>& d);
     备注:此构造函数不得   参加重载决议   除非“ 30”为真或   
ratio_divide<Period2,period>::den
是   1和
treat_as_floating_point<Rep2>::value
为假。   [注意:此要求可以防止   隐式截断错误   在积分基础之间转换   持续时间类型。这样的建筑   容易引起关于   持续时间的值。 —尾注] 即您需要在
Length
转换构造函数中使用
enable_if
,以便仅在精确转换时才存在(如果您希望将长度基于整数类型)。为了精确地进行转换,转换系数
(Scale2::num*Scale::den)/(Scale2::den*Scale::num)
必须是可除法的(除以1除外)。您可以使用
ratio_divide
进行该除法,然后分母必须为1(用于精确转换)。
enable_if<ratio_divide<Scale2,Scale1>::type::den == 1,...>
这是一个学习ѭ19great的好项目!玩得开心! :-)     ,        您使用整数类型表示物理量。这不是人们通常想要的。如果您坚持整数类型,则至少要以正确的顺序进行乘法和除法,即所有乘法均先进行除法(比较100 *(255/256)和(100 * 255)/ 256)。 在相关说明中,请记住,将“长度”乘以“长度”会得到一个面积,而不是“长度”。存在考虑到这一点的现实生活库,请参见例如siunits。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-